|
| 1 | +import { useAppDispatch, useAppSelector } from "../../../store"; |
| 2 | +import { |
| 3 | + resetSettings, |
| 4 | + setMaxExecutionTime, |
| 5 | + setTheme, |
| 6 | + toggleAutoUpdateScripts, |
| 7 | + toggleLogExecutions, |
| 8 | + toggleScriptsEnabled, |
| 9 | + updateSettings, |
| 10 | +} from "../settingsSlice"; |
| 11 | +import { SettingsState } from "../types"; |
| 12 | + |
| 13 | +export const useSettings = () => { |
| 14 | + const dispatch = useAppDispatch(); |
| 15 | + const settings = useAppSelector((state) => state.settings); |
| 16 | + |
| 17 | + // General settings update |
| 18 | + const updateAppSettings = (updates: Partial<SettingsState>) => { |
| 19 | + dispatch(updateSettings(updates)); |
| 20 | + }; |
| 21 | + |
| 22 | + // Script-related settings |
| 23 | + const toggleScripts = () => { |
| 24 | + dispatch(toggleScriptsEnabled()); |
| 25 | + }; |
| 26 | + |
| 27 | + const updateMaxExecutionTime = (time: number) => { |
| 28 | + dispatch(setMaxExecutionTime(time)); |
| 29 | + }; |
| 30 | + |
| 31 | + const toggleExecutionLogging = () => { |
| 32 | + dispatch(toggleLogExecutions()); |
| 33 | + }; |
| 34 | + |
| 35 | + const toggleAutoUpdate = () => { |
| 36 | + dispatch(toggleAutoUpdateScripts()); |
| 37 | + }; |
| 38 | + |
| 39 | + // Theme settings |
| 40 | + const switchTheme = (theme: "light" | "dark" | "system") => { |
| 41 | + dispatch(setTheme(theme)); |
| 42 | + }; |
| 43 | + |
| 44 | + // Reset functionality |
| 45 | + const resetAllSettings = () => { |
| 46 | + dispatch(resetSettings()); |
| 47 | + }; |
| 48 | + |
| 49 | + // Validation helpers |
| 50 | + const isValidExecutionTime = (time: number) => { |
| 51 | + return time >= 1000 && time <= 30000; |
| 52 | + }; |
| 53 | + |
| 54 | + const getExecutionTimeDisplay = (time: number) => { |
| 55 | + return `${(time / 1000).toFixed(1)}s`; |
| 56 | + }; |
| 57 | + |
| 58 | + // Computed values |
| 59 | + const isDarkMode = settings.theme === "dark"; |
| 60 | + const isLightMode = settings.theme === "light"; |
| 61 | + const isSystemMode = settings.theme === "system"; |
| 62 | + const executionTimeInSeconds = settings.maxExecutionTime / 1000; |
| 63 | + |
| 64 | + return { |
| 65 | + // Current settings state |
| 66 | + ...settings, |
| 67 | + |
| 68 | + // Computed values |
| 69 | + isDarkMode, |
| 70 | + isLightMode, |
| 71 | + isSystemMode, |
| 72 | + executionTimeInSeconds, |
| 73 | + |
| 74 | + // Actions |
| 75 | + updateSettings: updateAppSettings, |
| 76 | + toggleScripts, |
| 77 | + updateMaxExecutionTime, |
| 78 | + toggleExecutionLogging, |
| 79 | + toggleAutoUpdate, |
| 80 | + switchTheme, |
| 81 | + resetAllSettings, |
| 82 | + |
| 83 | + // Helpers |
| 84 | + isValidExecutionTime, |
| 85 | + getExecutionTimeDisplay, |
| 86 | + }; |
| 87 | +}; |
0 commit comments