forked from TabbyML/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): add chat panel to code browser (TabbyML#1748)
* call action * rebase * action * iframe * [autofix.ci] apply automated fixes * toggle panel icon * [autofix.ci] apply automated fixes * language * [autofix.ci] apply automated fixes * breadcrumb style * ask tabby * [autofix.ci] apply automated fixes * clear and rename * nanoid * remove useless icons * remove useless props * reset * [autofix.ci] apply automated fixes * remove * trigger visible * cleanup * revert * cleanup * revert * revert * ResizableHandle bg * breadcrumb style * [autofix.ci] apply automated fixes * header bg --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
955089b
commit 206afe9
Showing
11 changed files
with
304 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react' | ||
|
||
import { useStore } from '@/lib/hooks/use-store' | ||
import { useChatStore } from '@/lib/stores/chat-store' | ||
import { cn } from '@/lib/utils' | ||
import { Button } from '@/components/ui/button' | ||
import { IconClose } from '@/components/ui/icons' | ||
|
||
import { QuickActionEventPayload } from '../lib/event-emitter' | ||
import { SourceCodeBrowserContext } from './source-code-browser' | ||
|
||
interface ChatSideBarProps | ||
extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {} | ||
|
||
export const ChatSideBar: React.FC<ChatSideBarProps> = ({ | ||
className, | ||
...props | ||
}) => { | ||
const { pendingEvent, setPendingEvent } = React.useContext( | ||
SourceCodeBrowserContext | ||
) | ||
const activeChatId = useStore(useChatStore, state => state.activeChatId) | ||
const iframeRef = React.useRef<HTMLIFrameElement>(null) | ||
|
||
const getPrompt = ({ action, code, language }: QuickActionEventPayload) => { | ||
let builtInPrompt = '' | ||
switch (action) { | ||
case 'explain': | ||
builtInPrompt = 'Explain the following code:' | ||
break | ||
case 'generate_unittest': | ||
builtInPrompt = 'Generate a unit test for the following code:' | ||
break | ||
case 'generate_doc': | ||
builtInPrompt = 'Generate documentation for the following code:' | ||
break | ||
default: | ||
break | ||
} | ||
return `${builtInPrompt}\n${'```'}${language ?? ''}\n${code}\n${'```'}\n` | ||
} | ||
|
||
React.useEffect(() => { | ||
const contentWindow = iframeRef.current?.contentWindow | ||
|
||
if (pendingEvent) { | ||
contentWindow?.postMessage({ | ||
action: 'append', | ||
payload: getPrompt(pendingEvent) | ||
}) | ||
setPendingEvent(undefined) | ||
} | ||
}, [pendingEvent, iframeRef.current?.contentWindow]) | ||
|
||
return ( | ||
<div className={cn('flex h-full flex-col', className)} {...props}> | ||
<Header /> | ||
<iframe | ||
src={`/playground`} | ||
className="w-full flex-1 border-0" | ||
key={activeChatId} | ||
ref={iframeRef} | ||
/> | ||
</div> | ||
) | ||
} | ||
|
||
function Header() { | ||
const { setChatSideBarVisible } = React.useContext(SourceCodeBrowserContext) | ||
|
||
return ( | ||
<div className="sticky top-0 flex items-center justify-end px-2 py-1"> | ||
<Button | ||
size="icon" | ||
variant="ghost" | ||
onClick={e => setChatSideBarVisible(false)} | ||
> | ||
<IconClose /> | ||
</Button> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.