Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions src/features/settings/hooks/useSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { useAppDispatch, useAppSelector } from "../../../store";
import {
resetSettings,
setMaxExecutionTime,
setTheme,
toggleAutoUpdateScripts,
toggleLogExecutions,
toggleScriptsEnabled,
updateSettings,
} from "../settingsSlice";
import { SettingsState } from "../types";

export const useSettings = () => {
const dispatch = useAppDispatch();
const settings = useAppSelector((state) => state.settings);

// General settings update
const updateAppSettings = (updates: Partial<SettingsState>) => {
dispatch(updateSettings(updates));
};

// Script-related settings
const toggleScripts = () => {
dispatch(toggleScriptsEnabled());
};

const updateMaxExecutionTime = (time: number) => {
dispatch(setMaxExecutionTime(time));
};

const toggleExecutionLogging = () => {
dispatch(toggleLogExecutions());
};

const toggleAutoUpdate = () => {
dispatch(toggleAutoUpdateScripts());
};

// Theme settings
const switchTheme = (theme: "light" | "dark" | "system") => {
dispatch(setTheme(theme));
};

// Reset functionality
const resetAllSettings = () => {
dispatch(resetSettings());
};

// Validation helpers
const isValidExecutionTime = (time: number) => {
return time >= 1000 && time <= 30000;
};

const getExecutionTimeDisplay = (time: number) => {
return `${(time / 1000).toFixed(1)}s`;
};

// Computed values
const isDarkMode = settings.theme === "dark";
const isLightMode = settings.theme === "light";
const isSystemMode = settings.theme === "system";
const executionTimeInSeconds = settings.maxExecutionTime / 1000;

return {
// Current settings state
...settings,

// Computed values
isDarkMode,
isLightMode,
isSystemMode,
executionTimeInSeconds,

// Actions
updateSettings: updateAppSettings,
toggleScripts,
updateMaxExecutionTime,
toggleExecutionLogging,
toggleAutoUpdate,
switchTheme,
resetAllSettings,

// Helpers
isValidExecutionTime,
getExecutionTimeDisplay,
};
};
4 changes: 2 additions & 2 deletions src/features/settings/settingsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const initialState: SettingsState = {
scriptsEnabled: true,
maxExecutionTime: 5000,
logExecutions: true,
theme: "light",
theme: "system",
autoUpdateScripts: false,
};

Expand All @@ -29,7 +29,7 @@ const settingsSlice = createSlice({
state.logExecutions = !state.logExecutions;
},

setTheme: (state, action: PayloadAction<"light" | "dark">) => {
setTheme: (state, action: PayloadAction<"light" | "dark" | "system">) => {
state.theme = action.payload;
},

Expand Down
2 changes: 1 addition & 1 deletion src/features/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ export interface SettingsState {
scriptsEnabled: boolean;
maxExecutionTime: number;
logExecutions: boolean;
theme: "light" | "dark";
theme: "light" | "dark" | "system";
autoUpdateScripts: boolean;
}