mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 15:44:21 +00:00
Update Workspace Settings on frontend
This commit is contained in:
@@ -44,7 +44,7 @@ function settingsReducer(state, action) {
|
||||
}
|
||||
|
||||
const Settings = () => {
|
||||
const { settings, updateSettings } = useWorkspace();
|
||||
const { currentWorkspace, updateSettings } = useWorkspace();
|
||||
const { settingsModalVisible, setSettingsModalVisible } = useModalContext();
|
||||
const [state, dispatch] = useReducer(settingsReducer, initialState);
|
||||
const isInitialMount = useRef(true);
|
||||
@@ -52,9 +52,19 @@ const Settings = () => {
|
||||
useEffect(() => {
|
||||
if (isInitialMount.current) {
|
||||
isInitialMount.current = false;
|
||||
const settings = {
|
||||
theme: currentWorkspace.theme,
|
||||
autoSave: currentWorkspace.autoSave,
|
||||
gitEnabled: currentWorkspace.gitEnabled,
|
||||
gitUrl: currentWorkspace.gitUrl,
|
||||
gitUser: currentWorkspace.gitUser,
|
||||
gitToken: currentWorkspace.gitToken,
|
||||
gitAutoCommit: currentWorkspace.gitAutoCommit,
|
||||
gitCommitMsgTemplate: currentWorkspace.gitCommitMsgTemplate,
|
||||
};
|
||||
dispatch({ type: 'INIT_SETTINGS', payload: settings });
|
||||
}
|
||||
}, [settings]);
|
||||
}, [currentWorkspace]);
|
||||
|
||||
const handleInputChange = useCallback((key, value) => {
|
||||
dispatch({ type: 'UPDATE_LOCAL_SETTINGS', payload: { [key]: value } });
|
||||
|
||||
@@ -9,18 +9,16 @@ import { useMantineColorScheme } from '@mantine/core';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import {
|
||||
fetchLastWorkspaceId,
|
||||
fetchWorkspaceSettings,
|
||||
saveWorkspaceSettings,
|
||||
getWorkspace,
|
||||
updateWorkspace,
|
||||
updateLastWorkspace,
|
||||
} from '../services/api';
|
||||
import { DEFAULT_SETTINGS } from '../utils/constants';
|
||||
import { DEFAULT_WORKSPACE_SETTINGS } from '../utils/constants';
|
||||
|
||||
const WorkspaceContext = createContext();
|
||||
|
||||
export const WorkspaceProvider = ({ children }) => {
|
||||
const [currentWorkspace, setCurrentWorkspace] = useState(null);
|
||||
const [settings, setSettings] = useState(DEFAULT_SETTINGS);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const { colorScheme, setColorScheme } = useMantineColorScheme();
|
||||
|
||||
@@ -28,9 +26,7 @@ export const WorkspaceProvider = ({ children }) => {
|
||||
try {
|
||||
const workspace = await getWorkspace(workspaceId);
|
||||
setCurrentWorkspace(workspace);
|
||||
const workspaceSettings = await fetchWorkspaceSettings(workspaceId);
|
||||
setSettings(workspaceSettings.settings);
|
||||
setColorScheme(workspaceSettings.settings.theme);
|
||||
setColorScheme(workspace.theme);
|
||||
} catch (error) {
|
||||
console.error('Failed to load workspace data:', error);
|
||||
notifications.show({
|
||||
@@ -62,10 +58,8 @@ export const WorkspaceProvider = ({ children }) => {
|
||||
|
||||
const switchWorkspace = useCallback(async (workspaceId) => {
|
||||
try {
|
||||
console.log(workspaceId);
|
||||
setLoading(true);
|
||||
await updateLastWorkspace(workspaceId);
|
||||
console.log('Hello');
|
||||
await loadWorkspaceData(workspaceId);
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
@@ -89,28 +83,33 @@ export const WorkspaceProvider = ({ children }) => {
|
||||
if (!currentWorkspace) return;
|
||||
|
||||
try {
|
||||
await saveWorkspaceSettings(currentWorkspace.id, newSettings);
|
||||
setSettings(newSettings);
|
||||
setColorScheme(newSettings.theme);
|
||||
const updatedWorkspace = {
|
||||
...currentWorkspace,
|
||||
...newSettings,
|
||||
};
|
||||
|
||||
const response = await updateWorkspace(
|
||||
currentWorkspace.id,
|
||||
updatedWorkspace
|
||||
);
|
||||
setCurrentWorkspace(response);
|
||||
setColorScheme(response.theme);
|
||||
} catch (error) {
|
||||
console.error('Failed to save settings:', error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[currentWorkspace, setColorScheme]
|
||||
[currentWorkspace]
|
||||
);
|
||||
|
||||
// Update just the color scheme without saving to backend
|
||||
const updateColorScheme = useCallback(
|
||||
(newTheme) => {
|
||||
setColorScheme(newTheme);
|
||||
},
|
||||
[setColorScheme]
|
||||
);
|
||||
const updateColorScheme = useCallback((newTheme) => {
|
||||
setColorScheme(newTheme);
|
||||
}, []);
|
||||
|
||||
const value = {
|
||||
currentWorkspace,
|
||||
settings,
|
||||
settings: currentWorkspace || DEFAULT_WORKSPACE_SETTINGS,
|
||||
updateSettings,
|
||||
loading,
|
||||
colorScheme,
|
||||
|
||||
@@ -66,22 +66,16 @@ export const getWorkspace = async (workspaceId) => {
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const fetchWorkspaceSettings = async (workspaceId) => {
|
||||
// Combined function to update workspace data including settings
|
||||
export const updateWorkspace = async (workspaceId, workspaceData) => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/users/1/workspaces/${workspaceId}/settings`
|
||||
);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const saveWorkspaceSettings = async (workspaceId, settings) => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/users/1/workspaces/${workspaceId}/settings`,
|
||||
`${API_BASE_URL}/users/1/workspaces/${workspaceId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(settings),
|
||||
body: JSON.stringify(workspaceData),
|
||||
}
|
||||
);
|
||||
return response.json();
|
||||
@@ -162,20 +156,6 @@ export const createWorkspace = async (name) => {
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const updateWorkspace = async (workspaceId, name) => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/users/1/workspaces/${workspaceId}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ name }),
|
||||
}
|
||||
);
|
||||
return response.json();
|
||||
};
|
||||
|
||||
export const deleteWorkspace = async (workspaceId) => {
|
||||
const response = await apiCall(
|
||||
`${API_BASE_URL}/users/1/workspaces/${workspaceId}`,
|
||||
|
||||
@@ -26,7 +26,8 @@ export const IMAGE_EXTENSIONS = [
|
||||
'.svg',
|
||||
];
|
||||
|
||||
export const DEFAULT_SETTINGS = {
|
||||
// Renamed from DEFAULT_SETTINGS to be more specific
|
||||
export const DEFAULT_WORKSPACE_SETTINGS = {
|
||||
theme: THEMES.LIGHT,
|
||||
autoSave: false,
|
||||
gitEnabled: false,
|
||||
@@ -37,6 +38,12 @@ export const DEFAULT_SETTINGS = {
|
||||
gitCommitMsgTemplate: '${action} ${filename}',
|
||||
};
|
||||
|
||||
// Template for creating new workspaces
|
||||
export const DEFAULT_WORKSPACE = {
|
||||
name: '',
|
||||
...DEFAULT_WORKSPACE_SETTINGS,
|
||||
};
|
||||
|
||||
export const DEFAULT_FILE = {
|
||||
name: 'New File.md',
|
||||
path: 'New File.md',
|
||||
|
||||
Reference in New Issue
Block a user