fix(spotlight): address PR review feedback

- Clear optimistic store on user/session change to prevent stale values leaking across logins
- Move dispatcher lookup before setOptimistic to avoid permanent ghost entries when no dispatcher exists
- Inline dayjs.weekdays() calls to reflect runtime locale changes instead of caching at module load
- Remove unused UserPreferenceKey import
This commit is contained in:
Thomas Camlong committed 2026-06-01 10:26:08 +02:00
1 parent e0a77948e2
commit 0449716805
3 files changed
+11 -7

No files matched your search

@@ -8,7 +8,6 @@ import localeData from "dayjs/plugin/localeData";
import { clientApi } from "@homarr/api/client";
import { useSession } from "@homarr/auth/client";
import type { ColorScheme } from "@homarr/definitions";
import type { UserPreferenceKey } from "@homarr/settings";
import { visiblePreferenceDefinitions } from "@homarr/settings";
import { localeConfigurations } from "@homarr/translation";
import { useScopedI18n } from "@homarr/translation/client";
@@ -24,8 +23,6 @@ dayjs.extend(localeData);
type SettingsAction = ChildrenAction<Record<string, unknown>>;
const weekDays = dayjs.weekdays(false);
const BooleanRow = ({
label,
Icon,
@@ -87,7 +84,7 @@ const useSettingsActions = (_: Record<string, unknown>, query: string): Settings
defaultSearchEngineId: () =>
searchEnginesQuery.data?.find((e) => e.value === preferences.getPreference("defaultSearchEngineId").value)
?.label ?? t("searchEngine.none" as never),
firstDayOfWeek: () => weekDays[preferences.getPreference("firstDayOfWeek").value as DayOfWeek] ?? "",
firstDayOfWeek: () => dayjs.weekdays(false)[preferences.getPreference("firstDayOfWeek").value as DayOfWeek] ?? "",
homeBoardId: () =>
boardsQuery.data?.find((b) => b.id === preferences.getPreference("homeBoardId").value)?.name ??
t("board.none" as never),
@@ -97,7 +97,6 @@ const createAsyncSelectableChildren = <TItem,>(
DetailComponent: () => <PreferenceDetailHeader titleKey={`${preferenceKey}.children.detail.title`} />,
});
const weekDays = dayjs.weekdays(false);
const firstDayOfWeekOptions: DayOfWeek[] = [1, 6, 0];
export const preferenceChildrenOptionsByKey: Partial<Record<UserPreferenceKey, ChildrenFactory>> = {
@@ -116,7 +115,7 @@ export const preferenceChildrenOptionsByKey: Partial<Record<UserPreferenceKey, C
firstDayOfWeek: createStaticChildren("firstDayOfWeek", () =>
firstDayOfWeekOptions.map((day) => ({
key: day.toString(),
label: weekDays[day] ?? day.toString(),
label: dayjs.weekdays(false)[day] ?? day.toString(),
value: day,
})),
),
@@ -73,6 +73,13 @@ export const useUserPreferences = () => {
const t = useI18n();
const userId = session?.user.id;
const pendingRef = useRef<Record<string, boolean>>({});
const prevUserIdRef = useRef(userId);
useEffect(() => {
if (prevUserIdRef.current === userId) return;
prevUserIdRef.current = userId;
clearOptimistic(userPreferenceDefinitions.map((d) => d.key));
}, [userId]);
useSyncExternalStore(
subscribeOptimistic,
@@ -163,11 +170,12 @@ export const useUserPreferences = () => {
return;
}
setOptimistic(key, value);
const group = userPreferenceDefinitionByKey[key].mutationGroup!;
const dispatcher = groupDispatchers[group];
if (!dispatcher) return;
setOptimistic(key, value);
pendingRef.current[group] = true;
try {
await dispatcher(key, value);