Add Workspace context

This commit is contained in:
2024-10-19 13:48:37 +02:00
parent 3b7bf83073
commit 6eb3eecb24
16 changed files with 356 additions and 210 deletions

View File

@@ -2,38 +2,45 @@ import { useState, useCallback, useEffect } from 'react';
import { fetchFileContent } from '../services/api';
import { isImageFile } from '../utils/fileHelpers';
import { DEFAULT_FILE } from '../utils/constants';
import { useWorkspace } from '../contexts/WorkspaceContext';
export const useFileContent = (selectedFile) => {
const { currentWorkspace } = useWorkspace();
const [content, setContent] = useState(DEFAULT_FILE.content);
const [originalContent, setOriginalContent] = useState(DEFAULT_FILE.content);
const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false);
const loadFileContent = useCallback(async (filePath) => {
try {
let newContent;
if (filePath === DEFAULT_FILE.path) {
newContent = DEFAULT_FILE.content;
} else if (!isImageFile(filePath)) {
newContent = await fetchFileContent(filePath);
} else {
newContent = ''; // Set empty content for image files
const loadFileContent = useCallback(
async (filePath) => {
if (!currentWorkspace) return;
try {
let newContent;
if (filePath === DEFAULT_FILE.path) {
newContent = DEFAULT_FILE.content;
} else if (!isImageFile(filePath)) {
newContent = await fetchFileContent(currentWorkspace.id, filePath);
} else {
newContent = ''; // Set empty content for image files
}
setContent(newContent);
setOriginalContent(newContent);
setHasUnsavedChanges(false);
} catch (err) {
console.error('Error loading file content:', err);
setContent(''); // Set empty content on error
setOriginalContent('');
setHasUnsavedChanges(false);
}
setContent(newContent);
setOriginalContent(newContent);
setHasUnsavedChanges(false);
} catch (err) {
console.error('Error loading file content:', err);
setContent(''); // Set empty content on error
setOriginalContent('');
setHasUnsavedChanges(false);
}
}, []);
},
[currentWorkspace]
);
useEffect(() => {
if (selectedFile) {
if (selectedFile && currentWorkspace) {
loadFileContent(selectedFile);
}
}, [selectedFile, loadFileContent]);
}, [selectedFile, currentWorkspace, loadFileContent]);
const handleContentChange = useCallback(
(newContent) => {

View File

@@ -1,12 +1,16 @@
import { useState, useCallback } from 'react';
import { fetchFileList } from '../services/api';
import { useWorkspace } from '../contexts/WorkspaceContext';
export const useFileList = () => {
const [files, setFiles] = useState([]);
const { currentWorkspace } = useWorkspace();
const loadFileList = useCallback(async () => {
if (!currentWorkspace) return;
try {
const fileList = await fetchFileList();
const fileList = await fetchFileList(currentWorkspace.id);
if (Array.isArray(fileList)) {
setFiles(fileList);
} else {
@@ -15,7 +19,7 @@ export const useFileList = () => {
} catch (error) {
console.error('Failed to load file list:', error);
}
}, []);
}, [currentWorkspace]);
return { files, loadFileList };
};

View File

@@ -2,10 +2,12 @@ import { useState, useCallback } from 'react';
import { notifications } from '@mantine/notifications';
import { lookupFileByName } from '../services/api';
import { DEFAULT_FILE } from '../utils/constants';
import { useWorkspace } from '../contexts/WorkspaceContext';
export const useFileNavigation = () => {
const [selectedFile, setSelectedFile] = useState(DEFAULT_FILE.path);
const [isNewFile, setIsNewFile] = useState(true);
const { currentWorkspace } = useWorkspace();
const handleFileSelect = useCallback((filePath) => {
setSelectedFile(filePath);
@@ -14,8 +16,10 @@ export const useFileNavigation = () => {
const handleLinkClick = useCallback(
async (filename) => {
if (!currentWorkspace) return;
try {
const filePaths = await lookupFileByName(filename);
const filePaths = await lookupFileByName(currentWorkspace.id, filename);
if (filePaths.length >= 1) {
handleFileSelect(filePaths[0]);
} else {
@@ -34,7 +38,7 @@ export const useFileNavigation = () => {
});
}
},
[handleFileSelect]
[currentWorkspace, handleFileSelect]
);
return { handleLinkClick, selectedFile, isNewFile, handleFileSelect };

View File

@@ -1,12 +1,12 @@
import { useCallback } from 'react';
import { notifications } from '@mantine/notifications';
import { saveFileContent, deleteFile } from '../services/api';
import { useSettings } from '../contexts/SettingsContext';
import { useWorkspace } from '../contexts/WorkspaceContext';
import { useGitOperations } from './useGitOperations';
export const useFileOperations = () => {
const { settings } = useSettings();
const { handleCommitAndPush } = useGitOperations(settings.gitEnabled);
const { currentWorkspace, settings } = useWorkspace();
const { handleCommitAndPush } = useGitOperations();
const autoCommit = useCallback(
async (filePath, action) => {
@@ -15,7 +15,6 @@ export const useFileOperations = () => {
.replace('${filename}', filePath)
.replace('${action}', action);
// Capitalize the first letter of the commit message
commitMessage =
commitMessage.charAt(0).toUpperCase() + commitMessage.slice(1);
@@ -27,8 +26,10 @@ export const useFileOperations = () => {
const handleSave = useCallback(
async (filePath, content) => {
if (!currentWorkspace) return false;
try {
await saveFileContent(filePath, content);
await saveFileContent(currentWorkspace.id, filePath, content);
notifications.show({
title: 'Success',
message: 'File saved successfully',
@@ -46,13 +47,15 @@ export const useFileOperations = () => {
return false;
}
},
[autoCommit]
[currentWorkspace, autoCommit]
);
const handleDelete = useCallback(
async (filePath) => {
if (!currentWorkspace) return false;
try {
await deleteFile(filePath);
await deleteFile(currentWorkspace.id, filePath);
notifications.show({
title: 'Success',
message: 'File deleted successfully',
@@ -70,13 +73,15 @@ export const useFileOperations = () => {
return false;
}
},
[autoCommit]
[currentWorkspace, autoCommit]
);
const handleCreate = useCallback(
async (fileName, initialContent = '') => {
if (!currentWorkspace) return false;
try {
await saveFileContent(fileName, initialContent);
await saveFileContent(currentWorkspace.id, fileName, initialContent);
notifications.show({
title: 'Success',
message: 'File created successfully',
@@ -94,7 +99,7 @@ export const useFileOperations = () => {
return false;
}
},
[autoCommit]
[currentWorkspace, autoCommit]
);
return { handleSave, handleDelete, handleCreate };

View File

@@ -1,12 +1,16 @@
import { useCallback } from 'react';
import { notifications } from '@mantine/notifications';
import { pullChanges, commitAndPush } from '../services/api';
import { useWorkspace } from '../contexts/WorkspaceContext';
export const useGitOperations = () => {
const { currentWorkspace, settings } = useWorkspace();
export const useGitOperations = (gitEnabled) => {
const handlePull = useCallback(async () => {
if (!gitEnabled) return false;
if (!currentWorkspace || !settings.gitEnabled) return false;
try {
await pullChanges();
await pullChanges(currentWorkspace.id);
notifications.show({
title: 'Success',
message: 'Successfully pulled latest changes',
@@ -22,13 +26,14 @@ export const useGitOperations = (gitEnabled) => {
});
return false;
}
}, [gitEnabled]);
}, [currentWorkspace, settings.gitEnabled]);
const handleCommitAndPush = useCallback(
async (message) => {
if (!gitEnabled) return false;
if (!currentWorkspace || !settings.gitEnabled) return false;
try {
await commitAndPush(message);
await commitAndPush(currentWorkspace.id, message);
notifications.show({
title: 'Success',
message: 'Successfully committed and pushed changes',
@@ -45,7 +50,7 @@ export const useGitOperations = (gitEnabled) => {
return false;
}
},
[gitEnabled]
[currentWorkspace, settings.gitEnabled]
);
return { handlePull, handleCommitAndPush };