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(cowork): notify when workspace folder is unavailable

thinhlpg5989a7008db1
diff --git a/web-app/src/containers/CoworkWorkspacePill.tsx b/web-app/src/containers/CoworkWorkspacePill.tsx
index d2e897a4b..f89cfca67 100644
--- a/web-app/src/containers/CoworkWorkspacePill.tsx
+++ b/web-app/src/containers/CoworkWorkspacePill.tsx
@@ -1,4 +1,5 @@
import { Folder, FolderPlus, GitBranch, PencilLine } from 'lucide-react'
+import { toast } from 'sonner'
import { Button } from '@/components/ui/button'
import {
Popover,
@@ -41,6 +42,33 @@ export function CoworkWorkspacePill({
const serviceHub = useServiceHub()
const folderName = folder ? basenameOf(folder) : null
+ const notifyMissingWorkspace = () => {
+ toast.error(t('common:workspace.missing'), {
+ action: {
+ label: t('common:workspace.change'),
+ onClick: onAttach,
+ },
+ })
+ }
+
+ const handleOpen = async () => {
+ if (!folder) return
+ try {
+ await serviceHub.opener().openPath(folder)
+ } catch {
+ notifyMissingWorkspace()
+ }
+ }
+
+ const handleReveal = async () => {
+ if (!folder) return
+ try {
+ await serviceHub.opener().revealItemInDir(folder)
+ } catch {
+ notifyMissingWorkspace()
+ }
+ }
+
return (
<Popover>
<PopoverTrigger asChild>
@@ -99,7 +127,7 @@ export function CoworkWorkspacePill({
variant="ghost"
size="sm"
className="ml-auto h-7"
- onClick={() => void serviceHub.opener().openPath(folder)}
+ onClick={handleOpen}
>
{t('common:workspace.open')}
</Button>
@@ -107,7 +135,7 @@ export function CoworkWorkspacePill({
variant="ghost"
size="sm"
className="h-7"
- onClick={() => void serviceHub.opener().revealItemInDir(folder)}
+ onClick={handleReveal}
>
{t('common:workspace.reveal')}
</Button>
diff --git a/web-app/src/containers/__tests__/CoworkWorkspacePill.test.tsx b/web-app/src/containers/__tests__/CoworkWorkspacePill.test.tsx
index 9459cbba5..e7c38ea5a 100644
--- a/web-app/src/containers/__tests__/CoworkWorkspacePill.test.tsx
+++ b/web-app/src/containers/__tests__/CoworkWorkspacePill.test.tsx
@@ -9,6 +9,11 @@ vi.mock('@/i18n/react-i18next-compat', () => ({
}),
}))
+const { toastError } = vi.hoisted(() => ({ toastError: vi.fn() }))
+vi.mock('sonner', () => ({
+ toast: { error: toastError },
+}))
+
const openPath = vi.fn()
const revealItemInDir = vi.fn()
vi.mock('@/hooks/useServiceHub', () => ({
@@ -19,8 +24,9 @@ import { CoworkWorkspacePill } from '../CoworkWorkspacePill'
describe('CoworkWorkspacePill', () => {
beforeEach(() => {
- openPath.mockReset()
- revealItemInDir.mockReset()
+ openPath.mockReset().mockResolvedValue(undefined)
+ revealItemInDir.mockReset().mockResolvedValue(undefined)
+ toastError.mockReset()
})
it('invites attaching a folder when none is attached', async () => {
@@ -78,6 +84,58 @@ describe('CoworkWorkspacePill', () => {
expect(revealItemInDir).toHaveBeenCalledWith('/home/u/Projects/jan-app')
})
+ it('notifies and offers a replacement when the folder cannot be opened', async () => {
+ openPath.mockRejectedValueOnce(new Error('path does not exist'))
+ const onAttach = vi.fn()
+ render(
+ <CoworkWorkspacePill
+ folder="/home/u/Projects/jan-app"
+ onAttach={onAttach}
+ onDetach={vi.fn()}
+ />
+ )
+ await userEvent.click(screen.getByRole('button', { name: /a11yWithFolder/ }))
+ await userEvent.click(screen.getAllByText('common:workspace.open')[0])
+
+ await vi.waitFor(() => {
+ expect(toastError).toHaveBeenCalledWith(
+ 'common:workspace.missing',
+ expect.objectContaining({
+ action: expect.objectContaining({
+ label: 'common:workspace.change',
+ onClick: onAttach,
+ }),
+ })
+ )
+ })
+ })
+
+ it('notifies and offers a replacement when the folder cannot be revealed', async () => {
+ revealItemInDir.mockRejectedValueOnce(new Error('path does not exist'))
+ const onAttach = vi.fn()
+ render(
+ <CoworkWorkspacePill
+ folder="/home/u/Projects/jan-app"
+ onAttach={onAttach}
+ onDetach={vi.fn()}
+ />
+ )
+ await userEvent.click(screen.getByRole('button', { name: /a11yWithFolder/ }))
+ await userEvent.click(screen.getByText('common:workspace.reveal'))
+
+ await vi.waitFor(() => {
+ expect(toastError).toHaveBeenCalledWith(
+ 'common:workspace.missing',
+ expect.objectContaining({
+ action: expect.objectContaining({
+ label: 'common:workspace.change',
+ onClick: onAttach,
+ }),
+ })
+ )
+ })
+ })
+
it('detaches the folder', async () => {
const onDetach = vi.fn()
render(