mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Refactor WorkspaceDataContext and useFileOperations to remove settings
This commit is contained in:
@@ -126,7 +126,6 @@ describe('WorkspaceDataContext', () => {
|
||||
expect(result.current.currentWorkspace).toBeNull();
|
||||
expect(result.current.loading).toBe(true);
|
||||
expect(result.current.workspaces).toEqual([]);
|
||||
expect(result.current.settings).toEqual(DEFAULT_WORKSPACE_SETTINGS);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.loading).toBe(false);
|
||||
@@ -171,7 +170,6 @@ describe('WorkspaceDataContext', () => {
|
||||
|
||||
expect(result.current.currentWorkspace).toEqual(mockWorkspace);
|
||||
expect(result.current.workspaces).toEqual(mockWorkspaceList);
|
||||
expect(result.current.settings).toEqual(mockWorkspace);
|
||||
expect(mockGetLastWorkspaceName).toHaveBeenCalledTimes(1);
|
||||
expect(mockGetWorkspace).toHaveBeenCalledWith('test-workspace');
|
||||
expect(mockListWorkspaces).toHaveBeenCalledTimes(1);
|
||||
@@ -258,7 +256,6 @@ describe('WorkspaceDataContext', () => {
|
||||
|
||||
expect(result.current.currentWorkspace).toBeNull();
|
||||
expect(result.current.workspaces).toEqual([]);
|
||||
expect(result.current.settings).toEqual(DEFAULT_WORKSPACE_SETTINGS);
|
||||
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
@@ -420,7 +417,6 @@ describe('WorkspaceDataContext', () => {
|
||||
});
|
||||
|
||||
expect(result.current.currentWorkspace).toEqual(mockWorkspace);
|
||||
expect(result.current.settings).toEqual(mockWorkspace);
|
||||
expect(mockGetWorkspace).toHaveBeenCalledWith('test-workspace');
|
||||
expect(mockUpdateColorScheme).toHaveBeenCalledWith('dark');
|
||||
});
|
||||
@@ -500,7 +496,6 @@ describe('WorkspaceDataContext', () => {
|
||||
});
|
||||
|
||||
expect(result.current.currentWorkspace).toEqual(mockWorkspace);
|
||||
expect(result.current.settings).toEqual(mockWorkspace);
|
||||
});
|
||||
|
||||
it('sets workspace to null', async () => {
|
||||
@@ -524,7 +519,6 @@ describe('WorkspaceDataContext', () => {
|
||||
});
|
||||
|
||||
expect(result.current.currentWorkspace).toBeNull();
|
||||
expect(result.current.settings).toEqual(DEFAULT_WORKSPACE_SETTINGS);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -603,7 +597,6 @@ describe('WorkspaceDataContext', () => {
|
||||
|
||||
expect(result.current.currentWorkspace).toBeNull();
|
||||
expect(result.current.workspaces).toEqual([]);
|
||||
expect(result.current.settings).toEqual(DEFAULT_WORKSPACE_SETTINGS);
|
||||
expect(result.current.loading).toBe(false);
|
||||
|
||||
expect(typeof result.current.loadWorkspaces).toBe('function');
|
||||
@@ -631,7 +624,6 @@ describe('WorkspaceDataContext', () => {
|
||||
|
||||
expect(result.current.currentWorkspace).toEqual(mockWorkspace);
|
||||
expect(result.current.workspaces).toEqual(mockWorkspaceList);
|
||||
expect(result.current.settings).toEqual(mockWorkspace);
|
||||
expect(result.current.loading).toBe(false);
|
||||
|
||||
expect(typeof result.current.loadWorkspaces).toBe('function');
|
||||
|
||||
@@ -7,7 +7,7 @@ import React, {
|
||||
useCallback,
|
||||
} from 'react';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { DEFAULT_WORKSPACE_SETTINGS, type Workspace } from '@/types/models';
|
||||
import { type Workspace } from '@/types/models';
|
||||
import {
|
||||
getWorkspace,
|
||||
listWorkspaces,
|
||||
@@ -19,7 +19,6 @@ import { useTheme } from './ThemeContext';
|
||||
interface WorkspaceDataContextType {
|
||||
currentWorkspace: Workspace | null;
|
||||
workspaces: Workspace[];
|
||||
settings: Workspace | typeof DEFAULT_WORKSPACE_SETTINGS;
|
||||
loading: boolean;
|
||||
loadWorkspaces: () => Promise<Workspace[]>;
|
||||
loadWorkspaceData: (workspaceName: string) => Promise<void>;
|
||||
@@ -121,7 +120,6 @@ export const WorkspaceDataProvider: React.FC<WorkspaceDataProviderProps> = ({
|
||||
const value: WorkspaceDataContextType = {
|
||||
currentWorkspace,
|
||||
workspaces,
|
||||
settings: currentWorkspace || DEFAULT_WORKSPACE_SETTINGS,
|
||||
loading,
|
||||
loadWorkspaces,
|
||||
loadWorkspaceData,
|
||||
|
||||
@@ -13,21 +13,20 @@ vi.mock('@mantine/notifications', () => ({
|
||||
|
||||
// Mock the workspace context and git operations
|
||||
const mockWorkspaceData: {
|
||||
currentWorkspace: { id: number; name: string } | null;
|
||||
settings: {
|
||||
gitAutoCommit: boolean;
|
||||
gitEnabled: boolean;
|
||||
gitCommitMsgTemplate: string;
|
||||
};
|
||||
currentWorkspace: {
|
||||
id: number;
|
||||
name: string;
|
||||
gitAutoCommit?: boolean;
|
||||
gitEnabled?: boolean;
|
||||
gitCommitMsgTemplate?: string;
|
||||
} | null;
|
||||
} = {
|
||||
currentWorkspace: {
|
||||
id: 1,
|
||||
name: 'test-workspace',
|
||||
},
|
||||
settings: {
|
||||
gitAutoCommit: false,
|
||||
gitEnabled: false,
|
||||
gitCommitMsgTemplate: '${action} ${filename}',
|
||||
gitCommitMsgTemplate: '${action}: ${filename}',
|
||||
},
|
||||
};
|
||||
|
||||
@@ -53,8 +52,6 @@ describe('useFileOperations', () => {
|
||||
mockWorkspaceData.currentWorkspace = {
|
||||
id: 1,
|
||||
name: 'test-workspace',
|
||||
};
|
||||
mockWorkspaceData.settings = {
|
||||
gitAutoCommit: false,
|
||||
gitEnabled: false,
|
||||
gitCommitMsgTemplate: '${action} ${filename}',
|
||||
@@ -155,8 +152,8 @@ describe('useFileOperations', () => {
|
||||
});
|
||||
|
||||
// Enable auto-commit
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -178,9 +175,9 @@ describe('useFileOperations', () => {
|
||||
});
|
||||
|
||||
// Enable auto-commit with custom template
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.settings.gitCommitMsgTemplate =
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitCommitMsgTemplate =
|
||||
'Modified ${filename} - ${action}';
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
@@ -264,8 +261,8 @@ describe('useFileOperations', () => {
|
||||
mockDeleteFile.mockResolvedValue(undefined);
|
||||
|
||||
// Enable auto-commit
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -382,8 +379,8 @@ describe('useFileOperations', () => {
|
||||
});
|
||||
|
||||
// Enable auto-commit
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -407,8 +404,8 @@ describe('useFileOperations', () => {
|
||||
});
|
||||
|
||||
// Enable auto-commit but disable git
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = false;
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = false;
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -428,8 +425,8 @@ describe('useFileOperations', () => {
|
||||
});
|
||||
|
||||
// Enable git but disable auto-commit
|
||||
mockWorkspaceData.settings.gitAutoCommit = false;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = false;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -449,9 +446,10 @@ describe('useFileOperations', () => {
|
||||
});
|
||||
|
||||
// Enable auto-commit with lowercase template
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.settings.gitCommitMsgTemplate = 'updated ${filename}';
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitCommitMsgTemplate =
|
||||
'updated ${filename}';
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -476,9 +474,9 @@ describe('useFileOperations', () => {
|
||||
mockDeleteFile.mockResolvedValue(undefined);
|
||||
|
||||
// Enable auto-commit
|
||||
mockWorkspaceData.settings.gitAutoCommit = true;
|
||||
mockWorkspaceData.settings.gitEnabled = true;
|
||||
mockWorkspaceData.settings.gitCommitMsgTemplate =
|
||||
mockWorkspaceData.currentWorkspace!.gitAutoCommit = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = true;
|
||||
mockWorkspaceData.currentWorkspace!.gitCommitMsgTemplate =
|
||||
'${action}: ${filename}';
|
||||
|
||||
const { result } = renderHook(() => useFileOperations());
|
||||
|
||||
@@ -19,13 +19,14 @@ interface UseFileOperationsResult {
|
||||
}
|
||||
|
||||
export const useFileOperations = (): UseFileOperationsResult => {
|
||||
const { currentWorkspace, settings } = useWorkspaceData();
|
||||
const { currentWorkspace } = useWorkspaceData();
|
||||
const { handleCommitAndPush } = useGitOperations();
|
||||
|
||||
const autoCommit = useCallback(
|
||||
async (filePath: string, action: FileAction): Promise<void> => {
|
||||
if (settings.gitAutoCommit && settings.gitEnabled) {
|
||||
let commitMessage = settings.gitCommitMsgTemplate
|
||||
if (!currentWorkspace || !currentWorkspace.gitEnabled) return;
|
||||
if (currentWorkspace.gitAutoCommit && currentWorkspace.gitEnabled) {
|
||||
let commitMessage = currentWorkspace.gitCommitMsgTemplate
|
||||
.replace('${filename}', filePath)
|
||||
.replace('${action}', action);
|
||||
|
||||
@@ -35,7 +36,7 @@ export const useFileOperations = (): UseFileOperationsResult => {
|
||||
await handleCommitAndPush(commitMessage);
|
||||
}
|
||||
},
|
||||
[settings, handleCommitAndPush]
|
||||
[currentWorkspace, handleCommitAndPush]
|
||||
);
|
||||
|
||||
const handleSave = useCallback(
|
||||
@@ -123,15 +124,6 @@ export const useFileOperations = (): UseFileOperationsResult => {
|
||||
|
||||
try {
|
||||
// TODO: Implement your file upload API call
|
||||
// Example:
|
||||
// const formData = new FormData();
|
||||
// Array.from(files).forEach((file, index) => {
|
||||
// formData.append(`file${index}`, file);
|
||||
// });
|
||||
// if (targetPath) {
|
||||
// formData.append('targetPath', targetPath);
|
||||
// }
|
||||
// await uploadFiles(currentWorkspace.name, formData);
|
||||
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
@@ -165,12 +157,6 @@ export const useFileOperations = (): UseFileOperationsResult => {
|
||||
|
||||
try {
|
||||
// TODO: Implement your file move API call
|
||||
// Example:
|
||||
// await moveFiles(currentWorkspace.name, {
|
||||
// sourceIds: dragIds,
|
||||
// targetParentId: parentId,
|
||||
// targetIndex: index
|
||||
// });
|
||||
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
|
||||
@@ -13,14 +13,11 @@ vi.mock('@mantine/notifications', () => ({
|
||||
|
||||
// Mock the workspace context
|
||||
const mockWorkspaceData: {
|
||||
currentWorkspace: { id: number; name: string } | null;
|
||||
settings: { gitEnabled: boolean };
|
||||
currentWorkspace: { id: number; name: string; gitEnabled: boolean } | null;
|
||||
} = {
|
||||
currentWorkspace: {
|
||||
id: 1,
|
||||
name: 'test-workspace',
|
||||
},
|
||||
settings: {
|
||||
gitEnabled: true,
|
||||
},
|
||||
};
|
||||
@@ -39,8 +36,6 @@ describe('useGitOperations', () => {
|
||||
mockWorkspaceData.currentWorkspace = {
|
||||
id: 1,
|
||||
name: 'test-workspace',
|
||||
};
|
||||
mockWorkspaceData.settings = {
|
||||
gitEnabled: true,
|
||||
};
|
||||
});
|
||||
@@ -114,7 +109,7 @@ describe('useGitOperations', () => {
|
||||
});
|
||||
|
||||
it('returns false when git is disabled', async () => {
|
||||
mockWorkspaceData.settings.gitEnabled = false;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = false;
|
||||
|
||||
const { result } = renderHook(() => useGitOperations());
|
||||
|
||||
@@ -208,7 +203,7 @@ describe('useGitOperations', () => {
|
||||
});
|
||||
|
||||
it('does nothing when git is disabled', async () => {
|
||||
mockWorkspaceData.settings.gitEnabled = false;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = false;
|
||||
|
||||
const { result } = renderHook(() => useGitOperations());
|
||||
|
||||
@@ -306,6 +301,7 @@ describe('useGitOperations', () => {
|
||||
mockWorkspaceData.currentWorkspace = {
|
||||
id: 2,
|
||||
name: 'different-workspace',
|
||||
gitEnabled: true,
|
||||
};
|
||||
|
||||
rerender();
|
||||
@@ -321,10 +317,10 @@ describe('useGitOperations', () => {
|
||||
const { result, rerender } = renderHook(() => useGitOperations());
|
||||
|
||||
// Initially git is enabled
|
||||
expect(mockWorkspaceData.settings.gitEnabled).toBe(true);
|
||||
expect(mockWorkspaceData.currentWorkspace!.gitEnabled).toBe(true);
|
||||
|
||||
// Disable git
|
||||
mockWorkspaceData.settings.gitEnabled = false;
|
||||
mockWorkspaceData.currentWorkspace!.gitEnabled = false;
|
||||
rerender();
|
||||
|
||||
let pullResult: boolean | undefined;
|
||||
@@ -381,6 +377,7 @@ describe('useGitOperations', () => {
|
||||
mockWorkspaceData.currentWorkspace = {
|
||||
id: 1,
|
||||
name: undefined!,
|
||||
gitEnabled: true,
|
||||
};
|
||||
|
||||
const { result } = renderHook(() => useGitOperations());
|
||||
@@ -395,7 +392,9 @@ describe('useGitOperations', () => {
|
||||
});
|
||||
|
||||
it('handles missing settings gracefully', async () => {
|
||||
mockWorkspaceData.settings = {
|
||||
mockWorkspaceData.currentWorkspace = {
|
||||
id: 1,
|
||||
name: 'test-workspace',
|
||||
gitEnabled: undefined!,
|
||||
};
|
||||
|
||||
|
||||
@@ -10,10 +10,14 @@ interface UseGitOperationsResult {
|
||||
}
|
||||
|
||||
export const useGitOperations = (): UseGitOperationsResult => {
|
||||
const { currentWorkspace, settings } = useWorkspaceData();
|
||||
const { currentWorkspace } = useWorkspaceData();
|
||||
|
||||
const handlePull = useCallback(async (): Promise<boolean> => {
|
||||
if (!currentWorkspace || !settings.gitEnabled || !currentWorkspace.name)
|
||||
if (
|
||||
!currentWorkspace ||
|
||||
!currentWorkspace.gitEnabled ||
|
||||
!currentWorkspace.name
|
||||
)
|
||||
return false;
|
||||
|
||||
try {
|
||||
@@ -33,11 +37,11 @@ export const useGitOperations = (): UseGitOperationsResult => {
|
||||
});
|
||||
return false;
|
||||
}
|
||||
}, [currentWorkspace, settings.gitEnabled]);
|
||||
}, [currentWorkspace]);
|
||||
|
||||
const handleCommitAndPush = useCallback(
|
||||
async (message: string): Promise<void> => {
|
||||
if (!currentWorkspace || !settings.gitEnabled) return;
|
||||
if (!currentWorkspace || !currentWorkspace.gitEnabled) return;
|
||||
|
||||
try {
|
||||
const commitHash: CommitHash = await commitAndPush(
|
||||
@@ -60,7 +64,7 @@ export const useGitOperations = (): UseGitOperationsResult => {
|
||||
return;
|
||||
}
|
||||
},
|
||||
[currentWorkspace, settings.gitEnabled]
|
||||
[currentWorkspace]
|
||||
);
|
||||
|
||||
return { handlePull, handleCommitAndPush };
|
||||
|
||||
Reference in New Issue
Block a user