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

feat: move icon create new thread into top panel #3346

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
74 changes: 72 additions & 2 deletions web/containers/Layout/TopPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment } from 'react'
import { Fragment, useCallback, useEffect } from 'react'

import { Button } from '@janhq/joi'
import { useAtom, useAtomValue, useSetAtom } from 'jotai'
Expand All @@ -12,22 +12,35 @@ import {
SquareIcon,
PaletteIcon,
XIcon,
PenSquareIcon,
} from 'lucide-react'
import { twMerge } from 'tailwind-merge'

import LogoMark from '@/containers/Brand/Logo/Mark'

import { toaster } from '@/containers/Toast'

import useAssistantQuery from '@/hooks/useAssistantQuery'
import useThreadCreateMutation from '@/hooks/useThreadCreateMutation'
import useThreads from '@/hooks/useThreads'

import { copyOverInstructionEnabledAtom } from '@/screens/Thread/ThreadRightPanel/AssistantSettingContainer/components/CopyOverInstruction'

import {
MainViewState,
mainViewStateAtom,
showLeftPanelAtom,
showRightPanelAtom,
} from '@/helpers/atoms/App.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import {
downloadedModelsAtom,
getSelectedModelAtom,
} from '@/helpers/atoms/Model.atom'
import {
reduceTransparentAtom,
selectedSettingAtom,
} from '@/helpers/atoms/Setting.atom'
import { threadsAtom, activeThreadAtom } from '@/helpers/atoms/Thread.atom'

const TopPanel = () => {
const [showLeftPanel, setShowLeftPanel] = useAtom(showLeftPanelAtom)
Expand All @@ -37,6 +50,51 @@ const TopPanel = () => {
const reduceTransparent = useAtomValue(reduceTransparentAtom)
const downloadedModels = useAtomValue(downloadedModelsAtom)

const { setActiveThread } = useThreads()
const createThreadMutation = useThreadCreateMutation()

const selectedModel = useAtomValue(getSelectedModelAtom)
const threads = useAtomValue(threadsAtom)

const activeThread = useAtomValue(activeThreadAtom)
const { data: assistants } = useAssistantQuery()
const copyOverInstructionEnabled = useAtomValue(
copyOverInstructionEnabledAtom
)

useEffect(() => {
if (activeThread?.id) return
if (threads.length === 0) return
setActiveThread(threads[0].id)
}, [activeThread?.id, setActiveThread, threads])

const onCreateThreadClicked = useCallback(async () => {
if (!assistants || assistants.length) {
toaster({
title: 'No assistant available.',
description: `Could not create a new thread. Please add an assistant.`,
type: 'error',
})
return
}
if (!selectedModel) return
let instructions: string | undefined = undefined
urmauur marked this conversation as resolved.
Show resolved Hide resolved
if (copyOverInstructionEnabled) {
instructions = activeThread?.assistants[0]?.instructions ?? undefined
}
await createThreadMutation.mutateAsync({
modelId: selectedModel.model,
assistant: assistants[0],
instructions,
})
}, [
createThreadMutation,
selectedModel,
assistants,
activeThread,
copyOverInstructionEnabled,
])

return (
<div
className={twMerge(
Expand Down Expand Up @@ -73,6 +131,18 @@ const TopPanel = () => {
)}
</Fragment>
)}
{mainViewState === MainViewState.Thread && (
<Button
data-testid="btn-create-thread"
onClick={onCreateThreadClicked}
theme="icon"
>
<PenSquareIcon
size={16}
className="cursor-pointer text-[hsla(var(--text-secondary))]"
/>
</Button>
)}
</div>
<div className="unset-drag flex items-center gap-x-2">
{mainViewState !== MainViewState.Hub &&
Expand Down
70 changes: 1 addition & 69 deletions web/screens/Thread/ThreadLeftPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,89 +1,21 @@
import { useCallback, useEffect } from 'react'

import { Button } from '@janhq/joi'
import { AnimatePresence } from 'framer-motion'

import { useAtomValue } from 'jotai'
import { PenSquareIcon } from 'lucide-react'

import { twMerge } from 'tailwind-merge'

import LeftPanelContainer from '@/containers/LeftPanelContainer'
import { toaster } from '@/containers/Toast'

import useAssistantQuery from '@/hooks/useAssistantQuery'

import useThreadCreateMutation from '@/hooks/useThreadCreateMutation'

import useThreads from '@/hooks/useThreads'

import { copyOverInstructionEnabledAtom } from '../ThreadRightPanel/AssistantSettingContainer/components/CopyOverInstruction'

import ThreadItem from './ThreadItem'

import { getSelectedModelAtom } from '@/helpers/atoms/Model.atom'
import { activeThreadAtom, threadsAtom } from '@/helpers/atoms/Thread.atom'
import { threadsAtom } from '@/helpers/atoms/Thread.atom'

const ThreadLeftPanel: React.FC = () => {
const { setActiveThread } = useThreads()
const createThreadMutation = useThreadCreateMutation()

const selectedModel = useAtomValue(getSelectedModelAtom)
const threads = useAtomValue(threadsAtom)

const activeThread = useAtomValue(activeThreadAtom)
const { data: assistants } = useAssistantQuery()
const copyOverInstructionEnabled = useAtomValue(
copyOverInstructionEnabledAtom
)

useEffect(() => {
if (activeThread?.id) return
if (threads.length === 0) return
setActiveThread(threads[0].id)
}, [activeThread?.id, setActiveThread, threads])

const onCreateThreadClicked = useCallback(async () => {
if (!assistants || assistants.length === 0) {
toaster({
title: 'No assistant available.',
description: `Could not create a new thread. Please add an assistant.`,
type: 'error',
})
return
}
if (!selectedModel) return
let instructions: string | undefined = undefined
if (copyOverInstructionEnabled) {
instructions = activeThread?.assistants[0]?.instructions ?? undefined
}
await createThreadMutation.mutateAsync({
modelId: selectedModel.model,
assistant: assistants[0],
instructions,
})
}, [
createThreadMutation,
selectedModel,
assistants,
activeThread,
copyOverInstructionEnabled,
])

return (
<LeftPanelContainer>
<div className={twMerge('pl-1.5 pt-3')}>
<Button
className="mb-2"
data-testid="btn-create-thread"
onClick={onCreateThreadClicked}
theme="icon"
>
<PenSquareIcon
size={16}
className="cursor-pointer text-[hsla(var(--text-secondary))]"
/>
</Button>
<AnimatePresence>
{threads.map((thread) => (
<ThreadItem key={thread.id} thread={thread} />
Expand Down
Loading