|
| 1 | +import { produce } from 'immer'; |
| 2 | +import { create } from 'zustand'; |
| 3 | +import { persist } from 'zustand/middleware'; |
| 4 | + |
| 5 | +import { CustomHeader, InterceptorRule, ThemeColor } from '@shared/types'; |
| 6 | + |
| 7 | +interface IPreferencesState { |
| 8 | + hasHydrated: boolean; |
| 9 | + theme: ThemeColor; |
| 10 | + rules: InterceptorRule[]; |
| 11 | + customHeaders: CustomHeader[]; |
| 12 | +} |
| 13 | + |
| 14 | +interface IPreferencesStore extends IPreferencesState { |
| 15 | + // Custom header methods |
| 16 | + addHeader: () => void; |
| 17 | + removeHeader: (id: string) => void; |
| 18 | + updateHeader: (header: CustomHeader) => void; |
| 19 | + |
| 20 | + // Rule methods |
| 21 | + addRule: () => void; |
| 22 | + removeRule: (id: string) => void; |
| 23 | + updateRule: (rule: InterceptorRule) => void; |
| 24 | + |
| 25 | + // Other preferences/methods |
| 26 | + setTheme: (theme: IPreferencesState['theme']) => void; |
| 27 | + hydrate: () => void; |
| 28 | +} |
| 29 | + |
| 30 | +const usePreferencesStore = create<IPreferencesStore>()( |
| 31 | + persist( |
| 32 | + (set) => { |
| 33 | + const initialState: IPreferencesState = { |
| 34 | + rules: [], |
| 35 | + customHeaders: [], |
| 36 | + theme: 'indigo', |
| 37 | + hasHydrated: false, |
| 38 | + }; |
| 39 | + |
| 40 | + const setTheme = (theme: IPreferencesState['theme']) => { |
| 41 | + set( |
| 42 | + produce((draft) => { |
| 43 | + draft.theme = theme; |
| 44 | + }) |
| 45 | + ); |
| 46 | + }; |
| 47 | + |
| 48 | + /** |
| 49 | + * Add a new custom header This adds a new row to the table and wont add the header |
| 50 | + * until a key and value are provided |
| 51 | + */ |
| 52 | + const addHeader = () => { |
| 53 | + set((state) => |
| 54 | + produce(state, (draft) => { |
| 55 | + draft.customHeaders.push({ |
| 56 | + id: crypto.randomUUID(), |
| 57 | + key: '', |
| 58 | + value: '', |
| 59 | + enabled: true, |
| 60 | + }); |
| 61 | + }) |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + /** |
| 66 | + * Update an existing header to new values. Matches based |
| 67 | + * on ID |
| 68 | + */ |
| 69 | + const updateHeader = (header: CustomHeader) => { |
| 70 | + set((state) => ({ |
| 71 | + customHeaders: state.customHeaders.map((h) => (h.id === header.id ? header : h)), |
| 72 | + })); |
| 73 | + }; |
| 74 | + |
| 75 | + /** |
| 76 | + * Remove a rule based on its ID. Requests will no longer be |
| 77 | + * intercepted for this rule |
| 78 | + */ |
| 79 | + const removeHeader = (id: string) => { |
| 80 | + set((state) => ({ |
| 81 | + customHeaders: state.customHeaders.filter((h) => h.id !== id), |
| 82 | + })); |
| 83 | + }; |
| 84 | + |
| 85 | + /** |
| 86 | + * Add a new interceptor rule. This adds a new row to the table and wont actually |
| 87 | + * intercept anything until the value is not empty |
| 88 | + */ |
| 89 | + const addRule = () => { |
| 90 | + set((state) => |
| 91 | + produce(state, (draft) => { |
| 92 | + draft.rules.push({ |
| 93 | + id: crypto.randomUUID(), |
| 94 | + field: 'url', |
| 95 | + operator: 'equals', |
| 96 | + value: '', |
| 97 | + enabled: true, |
| 98 | + }); |
| 99 | + }) |
| 100 | + ); |
| 101 | + }; |
| 102 | + |
| 103 | + /** |
| 104 | + * Update an existing interceptor rule to new values. Matches based |
| 105 | + * on ID |
| 106 | + */ |
| 107 | + const updateRule = (rule: InterceptorRule) => { |
| 108 | + set((state) => ({ |
| 109 | + rules: state.rules.map((r) => (r.id === rule.id ? rule : r)), |
| 110 | + })); |
| 111 | + }; |
| 112 | + |
| 113 | + /** |
| 114 | + * Remove a rule based on its ID. Requests will no longer be |
| 115 | + * intercepted for this rule |
| 116 | + */ |
| 117 | + const removeRule = (id: string) => { |
| 118 | + set((state) => ({ rules: state.rules.filter((r) => r.id !== id) })); |
| 119 | + }; |
| 120 | + |
| 121 | + /** |
| 122 | + * Mark the store as hydrated from persisted data |
| 123 | + */ |
| 124 | + const hydrate = () => { |
| 125 | + set((state) => ({ ...state, hasHydrated: true })); |
| 126 | + }; |
| 127 | + |
| 128 | + return { |
| 129 | + ...initialState, |
| 130 | + setTheme, |
| 131 | + addHeader, |
| 132 | + removeHeader, |
| 133 | + updateHeader, |
| 134 | + addRule, |
| 135 | + removeRule, |
| 136 | + updateRule, |
| 137 | + hydrate, |
| 138 | + }; |
| 139 | + }, |
| 140 | + { |
| 141 | + name: 'preferences-store', |
| 142 | + onRehydrateStorage: () => { |
| 143 | + return (state, error) => { |
| 144 | + if (error || !state) { |
| 145 | + return state; |
| 146 | + } |
| 147 | + |
| 148 | + return state.hydrate(); |
| 149 | + }; |
| 150 | + }, |
| 151 | + } |
| 152 | + ) |
| 153 | +); |
| 154 | + |
| 155 | +export default usePreferencesStore; |
| 156 | + |
| 157 | +// For static initializations, so the var name looks cleaner |
| 158 | +export const preferencesStore = usePreferencesStore; |
0 commit comments