Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(web-app): toast a project move only when it happened, via i18n

Akarshan Biswas499caa36deca
diff --git a/web-app/src/containers/ThreadList.tsx b/web-app/src/containers/ThreadList.tsx
index af9d0a335..5d34814e5 100644
--- a/web-app/src/containers/ThreadList.tsx
+++ b/web-app/src/containers/ThreadList.tsx
@@ -143,14 +143,21 @@ export const ThreadItem = memo(
const assignThreadToProject = (threadId: string, projectId: string) => {
const project = getFolderById(projectId)
if (project) {
- // Cowork persists from its session, so the move goes through it.
- updateThreadProject(threadId, {
+ // Cowork persists from its session, so the move goes through it and
+ // can be refused; only a move that happened is reported as success.
+ const moved = updateThreadProject(threadId, {
id: project.id,
name: project.name,
updated_at: project.updated_at,
})
- toast.success(`Thread assigned to "${project.name}" successfully`)
+ if (moved) {
+ toast.success(t('common:toast.threadAssignedToProject.description', {
+ projectName: project.name,
+ }))
+ } else {
+ toast.error(t('common:toast.threadProjectMoveFailed.description'))
+ }
}
}
@@ -259,14 +266,21 @@ export const ThreadItem = memo(
onClick={(e) => {
e.stopPropagation()
const projectName = thread.metadata?.project?.name
- updateThreadProject(thread.id, undefined)
- toast.success(
- `Thread removed from "${projectName}" successfully`
- )
+ if (updateThreadProject(thread.id, undefined)) {
+ toast.success(
+ t('common:toast.threadRemovedFromProject.description', {
+ projectName,
+ })
+ )
+ } else {
+ toast.error(
+ t('common:toast.threadProjectMoveFailed.description')
+ )
+ }
}}
>
<X className="size-4" />
- <span>Remove from project</span>
+ <span>{t('common:projects.removeFromProject')}</span>
</DropdownMenuItem>
</>
)}
diff --git a/web-app/src/containers/__tests__/ThreadList.projectMove.test.tsx b/web-app/src/containers/__tests__/ThreadList.projectMove.test.tsx
new file mode 100644
index 000000000..3c030f19e
--- /dev/null
+++ b/web-app/src/containers/__tests__/ThreadList.projectMove.test.tsx
@@ -0,0 +1,123 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { fireEvent, render, screen } from '@testing-library/react'
+import '@testing-library/jest-dom'
+
+const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }))
+vi.mock('sonner', () => ({ toast }))
+
+const updateThreadProject = vi.hoisted(() => vi.fn())
+vi.mock('@/lib/threadProject', () => ({ updateThreadProject }))
+
+vi.mock('@tanstack/react-router', () => ({
+ Link: ({ children }: { children: React.ReactNode }) => <a>{children}</a>,
+ useParams: ({ select }: { select?: (p: object) => unknown } = {}) =>
+ select ? select({ threadId: undefined }) : { threadId: undefined },
+}))
+
+vi.mock('@/i18n/react-i18next-compat', () => ({
+ useTranslation: () => ({
+ t: (key: string, opts?: { projectName?: string }) =>
+ opts?.projectName ? `${key}:${opts.projectName}` : key,
+ }),
+}))
+
+vi.mock('@/hooks/useThreads', () => ({
+ useThreads: (selector: (s: object) => unknown) =>
+ selector({ deleteThread: vi.fn(), renameThread: vi.fn(), updateThread: vi.fn() }),
+}))
+
+vi.mock('@/hooks/useMessages', () => ({
+ useMessages: (selector: (s: object) => unknown) =>
+ selector({ messages: {}, getMessages: () => [], setMessages: vi.fn() }),
+}))
+
+const folder = { id: 'p1', name: 'Alpha', updated_at: 1 }
+vi.mock('@/hooks/useThreadManagement', () => ({
+ useThreadManagement: () => ({
+ folders: [folder],
+ getFolderById: () => folder,
+ }),
+}))
+
+vi.mock('@/components/ui/sidebar', () => ({
+ SidebarMenuItem: ({ children }: { children: React.ReactNode }) => <li>{children}</li>,
+ SidebarMenuButton: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
+ SidebarMenuAction: ({ children }: { children: React.ReactNode }) => <button>{children}</button>,
+ useSidebar: () => ({ isMobile: false }),
+}))
+
+vi.mock('@/components/ui/dropdown-menu', () => {
+ const Passthrough = ({ children }: { children: React.ReactNode }) => <>{children}</>
+ return {
+ DropdownMenu: Passthrough,
+ DropdownMenuContent: Passthrough,
+ DropdownMenuItem: ({
+ children,
+ onClick,
+ }: {
+ children: React.ReactNode
+ onClick?: (e: { stopPropagation: () => void }) => void
+ }) => (
+ <button onClick={() => onClick?.({ stopPropagation: () => {} })}>{children}</button>
+ ),
+ DropdownMenuSeparator: () => null,
+ DropdownMenuTrigger: Passthrough,
+ DropdownMenuSub: Passthrough,
+ DropdownMenuSubContent: Passthrough,
+ DropdownMenuSubTrigger: Passthrough,
+ }
+})
+
+vi.mock('@/containers/dialogs', () => ({
+ RenameThreadDialog: () => null,
+ DeleteThreadDialog: () => null,
+}))
+
+import ThreadList from '../ThreadList'
+
+const thread = (assigned = false) =>
+ ({
+ id: 't1',
+ title: 'T',
+ updated: 0,
+ metadata: assigned ? { project: { id: 'p0', name: 'Old', updated_at: 1 } } : {},
+ }) as Thread
+
+describe('ThreadList project moves', () => {
+ beforeEach(() => vi.clearAllMocks())
+
+ it('toasts success only when the assignment was applied', () => {
+ updateThreadProject.mockReturnValue(true)
+ render(<ThreadList threads={[thread()]} />)
+ fireEvent.click(screen.getByText('Alpha'))
+ expect(toast.success).toHaveBeenCalledWith(
+ 'common:toast.threadAssignedToProject.description:Alpha'
+ )
+ expect(toast.error).not.toHaveBeenCalled()
+ })
+
+ it('toasts an error, not success, when the assignment is refused', () => {
+ updateThreadProject.mockReturnValue(false)
+ render(<ThreadList threads={[thread()]} />)
+ fireEvent.click(screen.getByText('Alpha'))
+ expect(toast.success).not.toHaveBeenCalled()
+ expect(toast.error).toHaveBeenCalledWith(
+ 'common:toast.threadProjectMoveFailed.description'
+ )
+ })
+
+ it('reports removal from a project only when it happened', () => {
+ updateThreadProject.mockReturnValue(false)
+ render(<ThreadList threads={[thread(true)]} />)
+ fireEvent.click(screen.getByText('common:projects.removeFromProject'))
+ expect(updateThreadProject).toHaveBeenCalledWith('t1', undefined)
+ expect(toast.success).not.toHaveBeenCalled()
+ expect(toast.error).toHaveBeenCalled()
+
+ updateThreadProject.mockReturnValue(true)
+ fireEvent.click(screen.getByText('common:projects.removeFromProject'))
+ expect(toast.success).toHaveBeenCalledWith(
+ 'common:toast.threadRemovedFromProject.description:Old'
+ )
+ })
+})
diff --git a/web-app/src/lib/__tests__/threadProject.test.ts b/web-app/src/lib/__tests__/threadProject.test.ts
index b51e1cf86..c86672277 100644
--- a/web-app/src/lib/__tests__/threadProject.test.ts
+++ b/web-app/src/lib/__tests__/threadProject.test.ts
@@ -25,14 +25,14 @@ describe('updateThreadProject', () => {
it('updates a chat thread through the thread list', () => {
state.threads = { t: { id: 't', metadata: { a: 1 } } }
- updateThreadProject('t', project)
+ expect(updateThreadProject('t', project)).toBe(true)
expect(updateThread).toHaveBeenCalledWith('t', { metadata: { a: 1, project } })
})
it('persists a Cowork conversation from its session and updates the mirror', () => {
state.threads = { c: { id: 'c', metadata: { mode: 'cowork' } } }
useCoworkSessions.setState({ sessions: [session('c')] })
- updateThreadProject('c', project)
+ expect(updateThreadProject('c', project)).toBe(true)
expect(useCoworkSessions.getState().sessions[0].project).toEqual(project)
expect(updateThread).not.toHaveBeenCalled()
expect(setThreads).toHaveBeenCalled()
@@ -41,7 +41,7 @@ describe('updateThreadProject', () => {
it('removes the project from a Cowork session with an explicit clear', () => {
state.threads = { c: { id: 'c', metadata: { mode: 'cowork', project } } }
useCoworkSessions.setState({ sessions: [session('c', { project })] })
- updateThreadProject('c', undefined)
+ expect(updateThreadProject('c', undefined)).toBe(true)
const s = useCoworkSessions.getState().sessions[0]
expect('project' in s).toBe(true)
expect(s.project).toBeUndefined()
@@ -50,13 +50,19 @@ describe('updateThreadProject', () => {
it('refuses an unavailable session: its assignment would be undone by persistence', () => {
state.threads = { c: { id: 'c', metadata: { mode: 'cowork' } } }
useCoworkSessions.setState({ sessions: [session('c', { unavailable: 'missing-thread' })] })
- updateThreadProject('c', project)
+ expect(updateThreadProject('c', project)).toBe(false)
expect(useCoworkSessions.getState().sessions[0].project).toBeUndefined()
expect(updateThread).not.toHaveBeenCalled()
})
+ it('refuses a Cowork thread whose session is missing', () => {
+ state.threads = { c: { id: 'c', metadata: { mode: 'cowork' } } }
+ expect(updateThreadProject('c', project)).toBe(false)
+ expect(setThreads).not.toHaveBeenCalled()
+ })
+
it('does nothing for an unknown id', () => {
- updateThreadProject('nope', project)
+ expect(updateThreadProject('nope', project)).toBe(false)
expect(updateThread).not.toHaveBeenCalled()
expect(setThreads).not.toHaveBeenCalled()
})
diff --git a/web-app/src/lib/threadProject.ts b/web-app/src/lib/threadProject.ts
index 69acc61a8..c9430177f 100644
--- a/web-app/src/lib/threadProject.ts
+++ b/web-app/src/lib/threadProject.ts
@@ -2,17 +2,21 @@ import { useCoworkSessions, type CoworkSession } from '@/hooks/useCoworkSessions
import { useThreads } from '@/hooks/useThreads'
import { isCoworkThread } from '@/lib/threadMode'
-/** Cowork persists from its session, never from the sidebar's thread mirror. */
+/**
+ * Cowork persists from its session, never from the sidebar's thread mirror.
+ * Returns whether the assignment was applied, so a caller only reports success
+ * for a move that happened.
+ */
export function updateThreadProject(
id: string,
project: CoworkSession['project']
-): void {
+): boolean {
const thread = useThreads.getState().threads[id]
const session = useCoworkSessions.getState().sessions.find((s) => s.id === id)
if (isCoworkThread(thread) || (!thread && session)) {
// Missing/unavailable sessions must not acquire a mirror-only assignment
// that a later persistence pass would silently undo.
- if (!session || session.unavailable) return
+ if (!session || session.unavailable) return false
useCoworkSessions.getState().setProject(id, project)
const state = useThreads.getState()
const mirror = state.threads[id]
@@ -22,11 +26,11 @@ export function updateThreadProject(
[id]: { ...mirror, metadata: { ...mirror.metadata, project } },
}))
}
- return
- }
- if (thread) {
- useThreads.getState().updateThread(id, {
- metadata: { ...thread.metadata, project },
- })
+ return true
}
+ if (!thread) return false
+ useThreads.getState().updateThread(id, {
+ metadata: { ...thread.metadata, project },
+ })
+ return true
}
diff --git a/web-app/src/locales/de-DE/common.json b/web-app/src/locales/de-DE/common.json
index d64189115..80a29eae8 100644
--- a/web-app/src/locales/de-DE/common.json
+++ b/web-app/src/locales/de-DE/common.json
@@ -343,6 +343,10 @@
"title": "Thread entfernt",
"description": "Thread erfolgreich von \"{{projectName}}\" entfernt"
},
+ "threadProjectMoveFailed": {
+ "title": "Verschieben fehlgeschlagen",
+ "description": "Diese Unterhaltung konnte nicht verschoben werden. Ihre Sitzung ist nicht verfügbar."
+ },
"deleteAllThreads": {
"title": "Alle Threads löschen",
"description": "Alle deine Threads wurden permanent gelöscht."
diff --git a/web-app/src/locales/en/common.json b/web-app/src/locales/en/common.json
index f8f3fa4af..49e83f285 100644
--- a/web-app/src/locales/en/common.json
+++ b/web-app/src/locales/en/common.json
@@ -605,6 +605,10 @@
"title": "Thread Removed",
"description": "Thread removed from \"{{projectName}}\" successfully"
},
+ "threadProjectMoveFailed": {
+ "title": "Move Failed",
+ "description": "This conversation could not be moved. Its session is unavailable."
+ },
"fileUploaded": {
"title": "File Processed",
"description": "File has been processed and indexed successfully"
diff --git a/web-app/src/locales/fr/common.json b/web-app/src/locales/fr/common.json
index 2886e1eb6..7df3e02b0 100644
--- a/web-app/src/locales/fr/common.json
+++ b/web-app/src/locales/fr/common.json
@@ -398,6 +398,10 @@
"title": "Discussion retirée",
"description": "Discussion retirée de \"{{projectName}}\" avec succès"
},
+ "threadProjectMoveFailed": {
+ "title": "Échec du déplacement",
+ "description": "Cette conversation n'a pas pu être déplacée. Sa session est indisponible."
+ },
"deleteAllChats": {
"title": "Conversations supprimées",
"description": "Les conversations hors projets ont été supprimées. Les conversations de projet et les tâches ont été conservées."
diff --git a/web-app/src/locales/ja/common.json b/web-app/src/locales/ja/common.json
index e3a9c0854..dae317ba8 100644
--- a/web-app/src/locales/ja/common.json
+++ b/web-app/src/locales/ja/common.json
@@ -471,6 +471,10 @@
"title": "スレッドが削除されました",
"description": "スレッドは「{{projectName}}」から正常に削除されました"
},
+ "threadProjectMoveFailed": {
+ "title": "移動に失敗しました",
+ "description": "この会話を移動できませんでした。セッションを利用できません。"
+ },
"fileUploaded": {
"title": "ファイルを処理しました",
"description": "ファイルは正常に処理され、インデックス化されました"
diff --git a/web-app/src/locales/zh-CN/common.json b/web-app/src/locales/zh-CN/common.json
index 76666c7ad..0052f3a9c 100644
--- a/web-app/src/locales/zh-CN/common.json
+++ b/web-app/src/locales/zh-CN/common.json
@@ -454,6 +454,10 @@
"title": "对话已移除",
"description": "对话已成功从\"{{projectName}}\"中移除"
},
+ "threadProjectMoveFailed": {
+ "title": "移动失败",
+ "description": "无法移动此对话,其会话不可用。"
+ },
"deleteAllChats": {
"title": "对话已删除",
"description": "已删除项目外的对话,项目中的对话和任务已保留。"