feat(renderer): file-store toasts on save/open/folder

This commit is contained in:
2026-06-05 22:59:54 +05:30
parent 54615649f4
commit 338669f2db
2 changed files with 115 additions and 3 deletions
+15 -3
View File
@@ -3,6 +3,7 @@ import { persist, createJSONStorage } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';
import { enableMapSet } from 'immer';
import { ipc } from '@/lib/ipc';
import { toast } from '@/lib/toast';
import { useEditorStore } from '@/stores/editor-store';
import type { FileEntry } from '@/types/ipc';
@@ -79,7 +80,10 @@ export const useFileStore = create<FileState>()(
openFolder: async (path) => {
const result = await ipc.file.list(path);
if (!result.ok) return;
if (!result.ok) {
toast.error(`Failed to open folder: ${result.error.message}`);
return;
}
const children: FileNode[] = result.data!.map(entryToNode);
@@ -144,7 +148,10 @@ export const useFileStore = create<FileState>()(
}
const result = await ipc.file.read(filePath);
if (!result.ok) return;
if (!result.ok) {
toast.error(`Failed to open file: ${result.error.message}`);
return;
}
const content = result.data!;
const title = filePath.split('/').pop() ?? filePath;
@@ -217,10 +224,15 @@ export const useFileStore = create<FileState>()(
if (!buffer) return false;
const writeResult = await ipc.file.write(activeTabId, buffer.content);
if (!writeResult.ok) return false;
if (!writeResult.ok) {
toast.error(`Failed to save: ${writeResult.error.message}`);
return false;
}
useEditorStore.getState().markSaved(activeTabId);
useFileStore.getState().markTabClean(activeTabId);
const title = useFileStore.getState().openTabs.find(t => t.id === activeTabId)?.title ?? activeTabId.split('/').pop() ?? activeTabId;
toast.success(`Saved ${title}`);
return true;
},
})),
+100
View File
@@ -0,0 +1,100 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { useEditorStore } from '@/stores/editor-store';
import { useFileStore } from '@/stores/file-store';
vi.mock('@/lib/ipc', () => ({
ipc: {
file: {
list: vi.fn(),
read: vi.fn(),
pickFolder: vi.fn(),
pickFile: vi.fn(),
write: vi.fn(),
},
},
}));
vi.mock('@/lib/toast', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
info: vi.fn(),
warning: vi.fn(),
promise: vi.fn(),
dismiss: vi.fn(),
},
}));
import { ipc } from '@/lib/ipc';
import { toast } from '@/lib/toast';
const fakeRead = ipc.file.read as ReturnType<typeof vi.fn>;
const fakeWrite = ipc.file.write as ReturnType<typeof vi.fn>;
const fakeList = ipc.file.list as ReturnType<typeof vi.fn>;
describe('file-store toasts', () => {
beforeEach(() => {
localStorage.clear();
vi.clearAllMocks();
useFileStore.setState({
tree: null,
rootPath: null,
expanded: new Set<string>(),
openTabs: [],
activeTabId: null,
} as any);
useEditorStore.setState({ buffers: new Map(), activeId: null } as any);
});
describe('saveActiveBuffer', () => {
it('calls toast.success when save succeeds', async () => {
fakeWrite.mockResolvedValue({ ok: true, data: undefined });
useFileStore.setState({
activeTabId: '/test.md',
openTabs: [{ id: '/test.md', path: '/test.md', title: 'test.md', dirty: false }],
} as any);
useEditorStore.setState({
buffers: new Map([['/test.md', { id: '/test.md', path: '/test.md', content: '# hi', dirty: true }]]),
activeId: '/test.md',
} as any);
const result = await useFileStore.getState().saveActiveBuffer();
expect(result).toBe(true);
expect(toast.success).toHaveBeenCalledWith('Saved test.md');
});
it('calls toast.error when save fails', async () => {
fakeWrite.mockResolvedValue({ ok: false, error: { code: 'EACCES', message: 'Permission denied' } });
useFileStore.setState({
activeTabId: '/test.md',
openTabs: [{ id: '/test.md', path: '/test.md', title: 'test.md', dirty: false }],
} as any);
useEditorStore.setState({
buffers: new Map([['/test.md', { id: '/test.md', path: '/test.md', content: '# hi', dirty: true }]]),
activeId: '/test.md',
} as any);
const result = await useFileStore.getState().saveActiveBuffer();
expect(result).toBe(false);
expect(toast.error).toHaveBeenCalledWith('Failed to save: Permission denied');
});
});
describe('openFile', () => {
it('calls toast.error when IPC read fails', async () => {
fakeRead.mockResolvedValue({ ok: false, error: { code: 'ENOENT', message: 'No such file' } });
await useFileStore.getState().openFile('/missing.md');
expect(toast.error).toHaveBeenCalledWith('Failed to open file: No such file');
});
});
describe('openFolder', () => {
it('calls toast.error when IPC list fails', async () => {
fakeList.mockResolvedValue({ ok: false, error: { code: 'EACCES', message: 'Permission denied' } });
await useFileStore.getState().openFolder('/forbidden');
expect(toast.error).toHaveBeenCalledWith('Failed to open folder: Permission denied');
});
});
});