Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 1x 1x 1x 1x 22x 22x 22x 22x 5x 5x 5x 5x 22x 22x 22x 22x 22x 22x 22x 22x 2x 2x 1x 1x 1x 1x 1x 22x 22x 22x 22x 1x 1x 1x 1x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 1x 22x 22x | import { defaultSettings, search } from '@/../config.json';
import { APP_ACTION, CACHE, DRAWER_INDEX, PANEL } from '@/constants/common';
import { diff } from '@/helpers/collectionUtils';
import { getCountryName } from '@/helpers/country';
import { getCurrencyCodeFromLocation } from '@/helpers/currency';
import { getUserLanguage, getUserLocation } from '@/helpers/utils';
import { cstorage } from '@/utils/storage';
import { isValidUserSettings } from '@/utils/typeGuards/common';
import { startTransition } from 'react';
import { Logger } from '@/utils/Logger';
import { getErrorMessage } from '@/helpers/exceptions';
const logger = new Logger('appReducer');
/**
* The consolidated App state managed by React's `useActionState`. Split out of
* `App.tsx` so the reducer and its transitions can be unit-tested in isolation.
* @group State
* @source
*/
export interface AppState {
/** The active user settings (theme, currency, caching, suppliers, …). */
userSettings?: UserSettings;
/** The active panel (SearchHome / Results / Stats). */
panel: PANEL;
/** Whether the SpeedDial FAB is currently shown. */
speedDialVisibility: boolean;
/** The open drawer tab, or `CLOSED`. */
drawerTab: DRAWER_INDEX;
/** Suppliers selected for search filtering. */
selectedSuppliers: SupplierClassName[];
/** Cached ID of the ChemPal Favorites bookmarks folder. */
bookmarksFolderId: string | null;
}
/**
* The discriminated union of actions accepted by {@link appReducer}.
* @group State
* @source
*/
export type AppAction =
| { type: typeof APP_ACTION.UPDATE_SETTINGS; settings: UserSettings }
| { type: typeof APP_ACTION.SET_CURRENCY_RATE; rate: number }
| { type: typeof APP_ACTION.SET_PANEL; panel: PANEL }
| { type: typeof APP_ACTION.SET_SPEED_DIAL_VISIBILITY; visible: boolean }
| { type: typeof APP_ACTION.LOAD_FROM_STORAGE; data: Partial<AppState> }
| { type: typeof APP_ACTION.HYDRATE_SETTINGS; settings: UserSettings }
| { type: typeof APP_ACTION.SET_DRAWER_TAB; tab: DRAWER_INDEX }
| { type: typeof APP_ACTION.SET_SELECTED_SUPPLIERS; suppliers: SupplierClassName[] }
| { type: typeof APP_ACTION.SET_BOOKMARKS_FOLDER_ID; id: string | null };
/**
* Builds the initial App state: the shipped `defaultSettings` merged with the
* user's detected location/language/currency, landing on the search-home panel
* with the drawer closed. Detection helpers are pure (no network), so this is
* safe to evaluate at import time — matching the original module-level behavior.
* @returns A fresh partial App state used to seed `useActionState`.
* @category Utils
* @group State
* @source
*/
function buildInitialAppState(): Partial<AppState> {
const state: Partial<AppState> = {};
if (isValidUserSettings(defaultSettings)) {
state.userSettings = defaultSettings;
}
const location = getUserLocation();
state.userSettings = {
...state.userSettings,
currency: getCurrencyCodeFromLocation(location),
location,
country: getCountryName(location),
language: getUserLanguage(),
suppliers: {
...state.userSettings?.suppliers,
resultLimit: search.defaultResultsLimitPerSupplier,
},
} satisfies UserSettings;
state.panel = PANEL.SEARCH_HOME;
state.speedDialVisibility = false;
state.drawerTab = DRAWER_INDEX.CLOSED;
state.selectedSuppliers = [];
state.bookmarksFolderId = null;
return state;
}
/**
* The initial state passed to `useActionState` in `App.tsx`.
* @group State
*/
export const initialAppState: Partial<AppState> = buildInitialAppState();
/**
* The App state reducer driving `useActionState`. Pure with respect to the
* returned state; the persistence side effects (writing to `cstorage`) are
* fire-and-forget inside `startTransition`, mirroring the original inline
* reducer. `UPDATE_SETTINGS` keeps `country` in sync with `location`;
* `HYDRATE_SETTINGS` re-applies an external settings write without echoing it
* back to storage (early-returning on a no-op diff).
* @param currentState - The current App state.
* @param action - The action to apply.
* @returns The next App state (the same reference when nothing changed).
* @category Utils
* @group State
* @example
* ```ts
* const next = appReducer({ panel: PANEL.SEARCH_HOME }, { type: APP_ACTION.SET_PANEL, panel: PANEL.RESULTS });
* // next.panel === PANEL.RESULTS, and cstorage.session persists the selection
* ```
* @source
*/
export function appReducer(currentState: Partial<AppState>, action: AppAction): Partial<AppState> {
switch (action.type) {
// Applies new user settings (theme, currency, caching, suppliers, etc.) and
// persists them to cstorage.local. Also fetches the updated currency rate.
// Dispatched by child components via appContext.setUserSettings().
case APP_ACTION.UPDATE_SETTINGS: {
// Keep `country` (full name) in sync with `location` (country code) on
// every settings change, so consumers like Ambeed can read it directly.
const newSettings: UserSettings = {
...action.settings,
country: getCountryName(action.settings.location),
};
startTransition(() => {
(async () => {
try {
await cstorage.local.set({ [CACHE.USER_SETTINGS]: newSettings });
} catch (error) {
logger.error(`Failed to update settings: ${getErrorMessage(error)}`, { error });
}
})();
});
return {
...currentState,
userSettings: newSettings,
};
}
// Merges the freshly-fetched USD→currency rate into state and persists it.
// Dispatched from the currency-watching effect (the rate is async, so it
// can't be resolved inside UPDATE_SETTINGS); this is what makes the price
// column reconvert immediately when the user changes currency.
case APP_ACTION.SET_CURRENCY_RATE: {
const current = currentState.userSettings;
if (!current || current.currencyRate === action.rate) return currentState;
const updatedSettings: UserSettings = { ...current, currencyRate: action.rate };
startTransition(() => {
(async () => {
try {
await cstorage.local.set({ [CACHE.USER_SETTINGS]: updatedSettings });
} catch (error) {
logger.error(`Failed to persist currency rate: ${getErrorMessage(error)}`, { error });
}
})();
});
return {
...currentState,
userSettings: updatedSettings,
};
}
// Switches the active panel (0 = SearchHome, 1 = Results, 2 = Stats) and
// persists the selection to cstorage.session so it survives popup re-opens.
// Dispatched by child components via appContext.setPanel().
case APP_ACTION.SET_PANEL: {
startTransition(() => {
(async () => {
try {
await cstorage.session.set({ [CACHE.PANEL]: action.panel });
} catch (error) {
logger.error(`Failed to save panel: ${getErrorMessage(error)}`, { error });
}
})();
});
return {
...currentState,
panel: action.panel,
};
}
// Toggles the SpeedDial FAB visibility based on mouse proximity to the
// bottom-right corner of the popup. Dispatched by the mousemove listener.
case APP_ACTION.SET_SPEED_DIAL_VISIBILITY:
return {
...currentState,
speedDialVisibility: action.visible,
};
// Hydrates app state from chrome.storage on initial mount. Merges saved
// panel, userSettings, and selectedSuppliers into the current state.
// Dispatched once by the mount useEffect.
case APP_ACTION.LOAD_FROM_STORAGE:
return {
...currentState,
...action.data,
};
// Re-hydrates user settings when they change in another extension surface
// (e.g. the options page writes user_settings while this popup is open).
// Unlike UPDATE_SETTINGS this does NOT write back to storage — doing so
// would echo the change and loop. Early-return when nothing actually
// changed so the surface that made the edit doesn't re-render on its own
// storage echo. The currency-rate and locale effects key off
// userSettings.currency/.language, so they only re-run on real changes.
case APP_ACTION.HYDRATE_SETTINGS: {
const current = currentState.userSettings;
if (current && diff(current, action.settings).length === 0) return currentState;
return {
...currentState,
userSettings: action.settings,
};
}
// Opens a specific drawer tab or closes the drawer (tab = -1).
// Used by setDrawerTab() for direct tab selection and toggleDrawer()
// for open/close toggling.
case APP_ACTION.SET_DRAWER_TAB:
return {
...currentState,
drawerTab: action.tab,
};
// Updates the list of selected suppliers for search filtering and persists
// the selection to cstorage.local. Dispatched via appContext.setSelectedSuppliers().
case APP_ACTION.SET_SELECTED_SUPPLIERS: {
startTransition(() => {
(async () => {
try {
await cstorage.local.set({
[CACHE.SELECTED_SUPPLIERS]: action.suppliers,
});
} catch (error) {
logger.error(`Failed to save selectedSuppliers: ${getErrorMessage(error)}`, { error });
}
})();
});
return {
...currentState,
selectedSuppliers: action.suppliers,
};
}
// Persists the ChemPal Favorites bookmarks folder ID to cstorage.local
// so we don't need to scan the bookmark tree on every popup open.
case APP_ACTION.SET_BOOKMARKS_FOLDER_ID: {
startTransition(() => {
(async () => {
try {
await cstorage.local.set({
[CACHE.BOOKMARKS_FOLDER_ID]: action.id,
});
} catch (error) {
logger.error(`Failed to save bookmarksFolderId: ${getErrorMessage(error)}`, { error });
}
})();
});
return {
...currentState,
bookmarksFolderId: action.id,
};
}
default:
return currentState;
}
}
|