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