Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chat-panel): react rendering #2236

Merged
merged 5 commits into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions clients/tabby-chat-panel/src/react.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
import type { RefObject } from 'react'
import { useEffect, useRef } from 'react'
import { useEffect, useState } from 'react'

import type { ClientApi, ServerApi } from './index'
import { createClient, createServer } from './index'

function useClient(iframeRef: RefObject<HTMLIFrameElement>, api: ClientApi) {
const clientRef = useRef<ServerApi | null>(null)
const [client, setClient] = useState<ServerApi | null>(null)
let isCreated = false

useEffect(() => {
if (iframeRef.current && !clientRef.current) {
clientRef.current = createClient(iframeRef.current, api)
if (iframeRef.current && !isCreated) {
isCreated = true
setClient(createClient(iframeRef.current!, api))
}
}, [iframeRef.current])

return clientRef.current
return client
}

function useServer(api: ServerApi) {
const serverRef = useRef<ClientApi | null>(null)
const [server, setServer] = useState<ClientApi | null>(null)
let isCreated = false

useEffect(() => {
const isInIframe = window.self !== window.top
if (isInIframe && !serverRef.current) {
serverRef.current = createServer(api)
if (isInIframe && !isCreated) {
isCreated = true
setServer(createServer(api))
}
}, [])

return serverRef.current
return server
}

export {
Expand Down