Skip to content

Commit

Permalink
fix(chat-panel): react rendering (#2236)
Browse files Browse the repository at this point in the history
* fix(chat-panel): react rendering

* update

* update

* update

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wwayne and autofix-ci[bot] authored May 28, 2024
1 parent 750d362 commit c654270
Showing 1 changed file with 13 additions and 9 deletions.
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

0 comments on commit c654270

Please sign in to comment.