Skip to content

Commit 4ab392f

Browse files
authored
refactor(chat): simplify capability checks and state management in ChatPage (#3786)
1 parent f737104 commit 4ab392f

File tree

1 file changed

+10
-65
lines changed

1 file changed

+10
-65
lines changed

Diff for: ee/tabby-ui/app/chat/page.tsx

+10-65
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,6 @@ export default function ChatPage() {
7272
const isInEditor = !!client || undefined
7373
const useMacOSKeyboardEventHandler = useRef<boolean>()
7474

75-
// server feature support check
76-
const [supportsOnApplyInEditorV2, setSupportsOnApplyInEditorV2] =
77-
useState(false)
78-
const [supportsOnLookupSymbol, setSupportsOnLookupSymbol] = useState(false)
79-
const [
80-
supportsReadWorkspaceGitRepoInfo,
81-
setSupportsReadWorkspaceGitRepoInfo
82-
] = useState(false)
83-
const [
84-
supportsStoreAndFetchSessionState,
85-
setSupportsStoreAndFetchSessionState
86-
] = useState(false)
87-
const [supportsListFileInWorkspace, setSupportProvideFileAtInfo] =
88-
useState(false)
89-
const [supportsReadFileContent, setSupportsReadFileContent] = useState(false)
90-
9175
const executeCommand = (command: ChatCommand) => {
9276
if (chatRef.current) {
9377
chatRef.current.executeCommand(command)
@@ -244,34 +228,7 @@ export default function ChatPage() {
244228
apiVersion: TABBY_CHAT_PANEL_API_VERSION
245229
})
246230

247-
const checkCapabilities = async () => {
248-
server
249-
?.hasCapability('onApplyInEditorV2')
250-
.then(setSupportsOnApplyInEditorV2)
251-
server?.hasCapability('lookupSymbol').then(setSupportsOnLookupSymbol)
252-
server
253-
?.hasCapability('readWorkspaceGitRepositories')
254-
.then(setSupportsReadWorkspaceGitRepoInfo)
255-
server
256-
?.hasCapability('listFileInWorkspace')
257-
.then(setSupportProvideFileAtInfo)
258-
server
259-
?.hasCapability('readFileContent')
260-
.then(setSupportsReadFileContent)
261-
262-
Promise.all([
263-
server?.hasCapability('fetchSessionState'),
264-
server?.hasCapability('storeSessionState')
265-
]).then(results => {
266-
setSupportsStoreAndFetchSessionState(
267-
results.every(result => !!result)
268-
)
269-
})
270-
}
271-
272-
checkCapabilities().then(() => {
273-
setIsServerLoaded(true)
274-
})
231+
setIsServerLoaded(true)
275232
}
276233
}, [server])
277234

@@ -429,6 +386,9 @@ export default function ChatPage() {
429386
)
430387
}
431388

389+
const supportsStoreAndFetchSessionState =
390+
server?.storeSessionState && server?.fetchSessionState
391+
432392
return (
433393
<ErrorBoundary FallbackComponent={ErrorBoundaryFallback}>
434394
<Chat
@@ -441,39 +401,24 @@ export default function ChatPage() {
441401
onCopyContent={isInEditor && server?.onCopy}
442402
onApplyInEditor={
443403
isInEditor &&
444-
(supportsOnApplyInEditorV2
404+
(server?.onApplyInEditorV2
445405
? server?.onApplyInEditorV2
446406
: server?.onApplyInEditor)
447407
}
448-
supportsOnApplyInEditorV2={supportsOnApplyInEditorV2}
449-
onLookupSymbol={
450-
isInEditor &&
451-
(supportsOnLookupSymbol ? server?.lookupSymbol : undefined)
452-
}
408+
supportsOnApplyInEditorV2={!!server?.onApplyInEditorV2}
409+
onLookupSymbol={isInEditor && server?.lookupSymbol}
453410
openInEditor={openInEditor}
454411
openExternal={openExternal}
455-
readWorkspaceGitRepositories={
456-
supportsReadWorkspaceGitRepoInfo
457-
? server?.readWorkspaceGitRepositories
458-
: undefined
459-
}
412+
readWorkspaceGitRepositories={server?.readWorkspaceGitRepositories}
460413
getActiveEditorSelection={getActiveEditorSelection}
461414
fetchSessionState={
462415
supportsStoreAndFetchSessionState ? fetchSessionState : undefined
463416
}
464417
storeSessionState={
465418
supportsStoreAndFetchSessionState ? storeSessionState : undefined
466419
}
467-
listFileInWorkspace={
468-
isInEditor && supportsListFileInWorkspace
469-
? server?.listFileInWorkspace
470-
: undefined
471-
}
472-
readFileContent={
473-
isInEditor && supportsReadFileContent
474-
? server?.readFileContent
475-
: undefined
476-
}
420+
listFileInWorkspace={isInEditor && server?.listFileInWorkspace}
421+
readFileContent={isInEditor && server?.readFileContent}
477422
/>
478423
</ErrorBoundary>
479424
)

0 commit comments

Comments
 (0)