mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 18:10:18 +05:30
feat(renderer): file-store toasts on save/open/folder
This commit is contained in:
@@ -3,6 +3,7 @@ import { persist, createJSONStorage } from 'zustand/middleware';
|
|||||||
import { immer } from 'zustand/middleware/immer';
|
import { immer } from 'zustand/middleware/immer';
|
||||||
import { enableMapSet } from 'immer';
|
import { enableMapSet } from 'immer';
|
||||||
import { ipc } from '@/lib/ipc';
|
import { ipc } from '@/lib/ipc';
|
||||||
|
import { toast } from '@/lib/toast';
|
||||||
import { useEditorStore } from '@/stores/editor-store';
|
import { useEditorStore } from '@/stores/editor-store';
|
||||||
import type { FileEntry } from '@/types/ipc';
|
import type { FileEntry } from '@/types/ipc';
|
||||||
|
|
||||||
@@ -79,7 +80,10 @@ export const useFileStore = create<FileState>()(
|
|||||||
|
|
||||||
openFolder: async (path) => {
|
openFolder: async (path) => {
|
||||||
const result = await ipc.file.list(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);
|
const children: FileNode[] = result.data!.map(entryToNode);
|
||||||
|
|
||||||
@@ -144,7 +148,10 @@ export const useFileStore = create<FileState>()(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await ipc.file.read(filePath);
|
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 content = result.data!;
|
||||||
const title = filePath.split('/').pop() ?? filePath;
|
const title = filePath.split('/').pop() ?? filePath;
|
||||||
@@ -217,10 +224,15 @@ export const useFileStore = create<FileState>()(
|
|||||||
if (!buffer) return false;
|
if (!buffer) return false;
|
||||||
|
|
||||||
const writeResult = await ipc.file.write(activeTabId, buffer.content);
|
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);
|
useEditorStore.getState().markSaved(activeTabId);
|
||||||
useFileStore.getState().markTabClean(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;
|
return true;
|
||||||
},
|
},
|
||||||
})),
|
})),
|
||||||
|
|||||||
@@ -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');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user