continue working on user prefernces in command palette
This commit is contained in:
11 files changed
+415
-161
No files matched your search
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useMantineColorScheme } from "@mantine/core";
|
||||
import type { DayOfWeek } from "@mantine/dates";
|
||||
@@ -8,10 +8,11 @@ import type { DayOfWeek } from "@mantine/dates";
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useSession } from "@homarr/auth/client";
|
||||
import type { ColorScheme } from "@homarr/definitions";
|
||||
import { showErrorNotification } from "@homarr/notifications";
|
||||
import type { SettingsContextProps } from "@homarr/settings/creator";
|
||||
import type { UserPreferenceKey } from "@homarr/settings";
|
||||
import { useSettings } from "@homarr/settings";
|
||||
import { useChangeLocale, useCurrentLocale } from "@homarr/translation/client";
|
||||
import { useChangeLocale, useCurrentLocale, useI18n } from "@homarr/translation/client";
|
||||
|
||||
const optimisticPreferenceKeys = new Set<UserPreferenceKey>([
|
||||
"defaultSearchEngineId",
|
||||
@@ -24,14 +25,36 @@ const optimisticPreferenceKeys = new Set<UserPreferenceKey>([
|
||||
]);
|
||||
|
||||
const optimisticPreferencesRef: { current: Partial<Record<UserPreferenceKey, unknown>> } = { current: {} };
|
||||
let optimisticPreferencesVersion = 0;
|
||||
const optimisticPreferenceListeners = new Set<() => void>();
|
||||
|
||||
const searchPreferenceKeys = ["defaultSearchEngineId", "openSearchInNewTab", "ddgBangs"] as const;
|
||||
const homeBoardPreferenceKeys = ["homeBoardId", "mobileHomeBoardId"] as const;
|
||||
|
||||
const notifyOptimisticPreferenceListeners = () => {
|
||||
optimisticPreferencesVersion += 1;
|
||||
optimisticPreferenceListeners.forEach((listener) => listener());
|
||||
};
|
||||
|
||||
const subscribeToOptimisticPreferenceChanges = (listener: () => void) => {
|
||||
optimisticPreferenceListeners.add(listener);
|
||||
return () => optimisticPreferenceListeners.delete(listener);
|
||||
};
|
||||
|
||||
const setOptimisticPreference = (preferenceKey: UserPreferenceKey, value: unknown) => {
|
||||
optimisticPreferencesRef.current[preferenceKey] = value;
|
||||
notifyOptimisticPreferenceListeners();
|
||||
};
|
||||
|
||||
const clearOptimisticPreferences = (keys: readonly UserPreferenceKey[]) => {
|
||||
let changed = false;
|
||||
keys.forEach((preferenceKey) => {
|
||||
changed ||= preferenceKey in optimisticPreferencesRef.current;
|
||||
delete optimisticPreferencesRef.current[preferenceKey];
|
||||
});
|
||||
if (!changed) return;
|
||||
|
||||
notifyOptimisticPreferenceListeners();
|
||||
};
|
||||
|
||||
const getEffectivePreferenceValue = (preferenceKey: UserPreferenceKey, settings: SettingsContextProps): unknown => {
|
||||
@@ -57,7 +80,7 @@ const refreshAfterMutation = (router: ReturnType<typeof useRouter>) => {
|
||||
router.refresh();
|
||||
};
|
||||
|
||||
export const useUserPreference = (key: UserPreferenceKey) => {
|
||||
export const useUserPreferences = () => {
|
||||
const router = useRouter();
|
||||
const settings = useSettings();
|
||||
const settingsRef = useRef(settings);
|
||||
@@ -67,141 +90,258 @@ export const useUserPreference = (key: UserPreferenceKey) => {
|
||||
const { colorScheme, setColorScheme } = useMantineColorScheme();
|
||||
const currentLocale = useCurrentLocale();
|
||||
const { changeLocale } = useChangeLocale();
|
||||
const [optimisticValue, setOptimisticValue] = useState<unknown>();
|
||||
|
||||
const { mutate: mutateSearchPreferences, isPending: isSearchPreferencesPending } =
|
||||
clientApi.user.changeSearchPreferences.useMutation({
|
||||
onSuccess: () => refreshAfterMutation(router),
|
||||
onError: () => clearOptimisticPreferences(searchPreferenceKeys),
|
||||
});
|
||||
|
||||
const { mutate: mutateHomeBoards, isPending: isHomeBoardsPending } = clientApi.user.changeHomeBoards.useMutation({
|
||||
onSuccess: () => refreshAfterMutation(router),
|
||||
onError: () => clearOptimisticPreferences(homeBoardPreferenceKeys),
|
||||
const t = useI18n();
|
||||
const pendingGroupsRef = useRef({
|
||||
search: false,
|
||||
homeBoards: false,
|
||||
firstDayOfWeek: false,
|
||||
pingIcons: false,
|
||||
});
|
||||
|
||||
const { mutate: mutateFirstDayOfWeek, isPending: isFirstDayOfWeekPending } =
|
||||
clientApi.user.changeFirstDayOfWeek.useMutation({
|
||||
useSyncExternalStore(
|
||||
subscribeToOptimisticPreferenceChanges,
|
||||
() => optimisticPreferencesVersion,
|
||||
() => optimisticPreferencesVersion,
|
||||
);
|
||||
|
||||
const showPreferenceErrorNotification = () => {
|
||||
showErrorNotification({
|
||||
message: t("search.mode.command.group.preferences.notification.error.message"),
|
||||
});
|
||||
};
|
||||
|
||||
const { mutateAsync: mutateSearchPreferencesAsync, isPending: isSearchPreferencesPending } =
|
||||
clientApi.user.changeSearchPreferences.useMutation({
|
||||
onSuccess: () => refreshAfterMutation(router),
|
||||
onError: () => clearOptimisticPreferences(["firstDayOfWeek"]),
|
||||
});
|
||||
|
||||
const { mutate: mutatePingIconsEnabled, isPending: isPingIconsPending } =
|
||||
const { mutateAsync: mutateHomeBoardsAsync, isPending: isHomeBoardsPending } =
|
||||
clientApi.user.changeHomeBoards.useMutation({
|
||||
onSuccess: () => refreshAfterMutation(router),
|
||||
});
|
||||
|
||||
const { mutateAsync: mutateFirstDayOfWeekAsync, isPending: isFirstDayOfWeekPending } =
|
||||
clientApi.user.changeFirstDayOfWeek.useMutation({
|
||||
onSuccess: () => refreshAfterMutation(router),
|
||||
});
|
||||
|
||||
const { mutateAsync: mutatePingIconsEnabledAsync, isPending: isPingIconsPending } =
|
||||
clientApi.user.changePingIconsEnabled.useMutation({
|
||||
onSuccess: () => refreshAfterMutation(router),
|
||||
onError: () => clearOptimisticPreferences(["pingIconsEnabled"]),
|
||||
});
|
||||
|
||||
const userId = session?.user.id;
|
||||
|
||||
const persistedValues: Record<UserPreferenceKey, unknown> = {
|
||||
colorScheme: colorScheme as ColorScheme,
|
||||
locale: currentLocale,
|
||||
defaultSearchEngineId: settings.defaultSearchEngineId,
|
||||
openSearchInNewTab: settings.openSearchInNewTab,
|
||||
ddgBangs: settings.ddgBangs,
|
||||
firstDayOfWeek: settings.firstDayOfWeek,
|
||||
homeBoardId: settings.homeBoardId,
|
||||
mobileHomeBoardId: settings.mobileHomeBoardId,
|
||||
pingIconsEnabled: settings.pingIconsEnabled,
|
||||
fullPreferencesPage: userId ? `/manage/users/${userId}/general` : null,
|
||||
};
|
||||
const persistedValues = useMemo<Record<UserPreferenceKey, unknown>>(
|
||||
() => ({
|
||||
colorScheme: colorScheme as ColorScheme,
|
||||
locale: currentLocale,
|
||||
defaultSearchEngineId: settings.defaultSearchEngineId,
|
||||
openSearchInNewTab: settings.openSearchInNewTab,
|
||||
ddgBangs: settings.ddgBangs,
|
||||
firstDayOfWeek: settings.firstDayOfWeek,
|
||||
homeBoardId: settings.homeBoardId,
|
||||
mobileHomeBoardId: settings.mobileHomeBoardId,
|
||||
pingIconsEnabled: settings.pingIconsEnabled,
|
||||
fullPreferencesPage: userId ? `/manage/users/${userId}/general` : null,
|
||||
}),
|
||||
[
|
||||
colorScheme,
|
||||
currentLocale,
|
||||
settings.defaultSearchEngineId,
|
||||
settings.openSearchInNewTab,
|
||||
settings.ddgBangs,
|
||||
settings.firstDayOfWeek,
|
||||
settings.homeBoardId,
|
||||
settings.mobileHomeBoardId,
|
||||
settings.pingIconsEnabled,
|
||||
userId,
|
||||
],
|
||||
);
|
||||
|
||||
const persistedValue = persistedValues[key];
|
||||
const getValue = (preferenceKey: UserPreferenceKey) =>
|
||||
preferenceKey in optimisticPreferencesRef.current
|
||||
? optimisticPreferencesRef.current[preferenceKey]
|
||||
: persistedValues[preferenceKey];
|
||||
|
||||
useEffect(() => {
|
||||
delete optimisticPreferencesRef.current[key];
|
||||
setOptimisticValue(undefined);
|
||||
}, [persistedValue, key]);
|
||||
clearOptimisticPreferences(
|
||||
Object.entries(persistedValues)
|
||||
.filter(
|
||||
([preferenceKey, value]) => optimisticPreferencesRef.current[preferenceKey as UserPreferenceKey] === value,
|
||||
)
|
||||
.map(([preferenceKey]) => preferenceKey as UserPreferenceKey),
|
||||
);
|
||||
}, [persistedValues]);
|
||||
|
||||
const setPersistedValue = (value: never) => {
|
||||
const setPersistedValueAsync = async (key: UserPreferenceKey, value: never) => {
|
||||
const currentSettings = settingsRef.current;
|
||||
|
||||
const setters: Record<UserPreferenceKey, (nextValue: never) => void> = {
|
||||
const setters: Record<UserPreferenceKey, (nextValue: never) => Promise<void> | void> = {
|
||||
colorScheme: (nextValue) => setColorScheme(nextValue as ColorScheme),
|
||||
locale: (nextValue) => changeLocale(nextValue as typeof currentLocale),
|
||||
defaultSearchEngineId: (nextValue) => {
|
||||
defaultSearchEngineId: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutateSearchPreferences({
|
||||
userId,
|
||||
defaultSearchEngineId: nextValue as string | null,
|
||||
openInNewTab: getEffectivePreferenceValue("openSearchInNewTab", currentSettings) as boolean,
|
||||
ddgBangsEnabled: getEffectivePreferenceValue("ddgBangs", currentSettings) as boolean,
|
||||
});
|
||||
if (pendingGroupsRef.current.search) return;
|
||||
pendingGroupsRef.current.search = true;
|
||||
try {
|
||||
await mutateSearchPreferencesAsync({
|
||||
userId,
|
||||
defaultSearchEngineId: nextValue as string | null,
|
||||
openInNewTab: getEffectivePreferenceValue("openSearchInNewTab", currentSettings) as boolean,
|
||||
ddgBangsEnabled: getEffectivePreferenceValue("ddgBangs", currentSettings) as boolean,
|
||||
});
|
||||
} finally {
|
||||
pendingGroupsRef.current.search = false;
|
||||
}
|
||||
},
|
||||
openSearchInNewTab: (nextValue) => {
|
||||
openSearchInNewTab: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutateSearchPreferences({
|
||||
userId,
|
||||
defaultSearchEngineId: getEffectivePreferenceValue("defaultSearchEngineId", currentSettings) as string | null,
|
||||
openInNewTab: nextValue as boolean,
|
||||
ddgBangsEnabled: getEffectivePreferenceValue("ddgBangs", currentSettings) as boolean,
|
||||
});
|
||||
if (pendingGroupsRef.current.search) return;
|
||||
pendingGroupsRef.current.search = true;
|
||||
try {
|
||||
await mutateSearchPreferencesAsync({
|
||||
userId,
|
||||
defaultSearchEngineId: getEffectivePreferenceValue("defaultSearchEngineId", currentSettings) as
|
||||
| string
|
||||
| null,
|
||||
openInNewTab: nextValue as boolean,
|
||||
ddgBangsEnabled: getEffectivePreferenceValue("ddgBangs", currentSettings) as boolean,
|
||||
});
|
||||
} finally {
|
||||
pendingGroupsRef.current.search = false;
|
||||
}
|
||||
},
|
||||
ddgBangs: (nextValue) => {
|
||||
ddgBangs: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutateSearchPreferences({
|
||||
userId,
|
||||
defaultSearchEngineId: getEffectivePreferenceValue("defaultSearchEngineId", currentSettings) as string | null,
|
||||
openInNewTab: getEffectivePreferenceValue("openSearchInNewTab", currentSettings) as boolean,
|
||||
ddgBangsEnabled: nextValue as boolean,
|
||||
});
|
||||
if (pendingGroupsRef.current.search) return;
|
||||
pendingGroupsRef.current.search = true;
|
||||
try {
|
||||
await mutateSearchPreferencesAsync({
|
||||
userId,
|
||||
defaultSearchEngineId: getEffectivePreferenceValue("defaultSearchEngineId", currentSettings) as
|
||||
| string
|
||||
| null,
|
||||
openInNewTab: getEffectivePreferenceValue("openSearchInNewTab", currentSettings) as boolean,
|
||||
ddgBangsEnabled: nextValue as boolean,
|
||||
});
|
||||
} finally {
|
||||
pendingGroupsRef.current.search = false;
|
||||
}
|
||||
},
|
||||
firstDayOfWeek: (nextValue) => {
|
||||
firstDayOfWeek: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutateFirstDayOfWeek({ id: userId, firstDayOfWeek: nextValue as DayOfWeek });
|
||||
if (pendingGroupsRef.current.firstDayOfWeek) return;
|
||||
pendingGroupsRef.current.firstDayOfWeek = true;
|
||||
try {
|
||||
await mutateFirstDayOfWeekAsync({ id: userId, firstDayOfWeek: nextValue as DayOfWeek });
|
||||
} finally {
|
||||
pendingGroupsRef.current.firstDayOfWeek = false;
|
||||
}
|
||||
},
|
||||
homeBoardId: (nextValue) => {
|
||||
homeBoardId: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutateHomeBoards({
|
||||
userId,
|
||||
homeBoardId: nextValue as string | null,
|
||||
mobileHomeBoardId: getEffectivePreferenceValue("mobileHomeBoardId", currentSettings) as string | null,
|
||||
});
|
||||
if (pendingGroupsRef.current.homeBoards) return;
|
||||
pendingGroupsRef.current.homeBoards = true;
|
||||
try {
|
||||
await mutateHomeBoardsAsync({
|
||||
userId,
|
||||
homeBoardId: nextValue as string | null,
|
||||
mobileHomeBoardId: getEffectivePreferenceValue("mobileHomeBoardId", currentSettings) as string | null,
|
||||
});
|
||||
} finally {
|
||||
pendingGroupsRef.current.homeBoards = false;
|
||||
}
|
||||
},
|
||||
mobileHomeBoardId: (nextValue) => {
|
||||
mobileHomeBoardId: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutateHomeBoards({
|
||||
userId,
|
||||
homeBoardId: getEffectivePreferenceValue("homeBoardId", currentSettings) as string | null,
|
||||
mobileHomeBoardId: nextValue as string | null,
|
||||
});
|
||||
if (pendingGroupsRef.current.homeBoards) return;
|
||||
pendingGroupsRef.current.homeBoards = true;
|
||||
try {
|
||||
await mutateHomeBoardsAsync({
|
||||
userId,
|
||||
homeBoardId: getEffectivePreferenceValue("homeBoardId", currentSettings) as string | null,
|
||||
mobileHomeBoardId: nextValue as string | null,
|
||||
});
|
||||
} finally {
|
||||
pendingGroupsRef.current.homeBoards = false;
|
||||
}
|
||||
},
|
||||
pingIconsEnabled: (nextValue) => {
|
||||
pingIconsEnabled: async (nextValue) => {
|
||||
if (!userId) return;
|
||||
mutatePingIconsEnabled({ id: userId, pingIconsEnabled: nextValue as boolean });
|
||||
if (pendingGroupsRef.current.pingIcons) return;
|
||||
pendingGroupsRef.current.pingIcons = true;
|
||||
try {
|
||||
await mutatePingIconsEnabledAsync({ id: userId, pingIconsEnabled: nextValue as boolean });
|
||||
} finally {
|
||||
pendingGroupsRef.current.pingIcons = false;
|
||||
}
|
||||
},
|
||||
fullPreferencesPage: () => undefined,
|
||||
};
|
||||
|
||||
setters[key](value);
|
||||
await setters[key](value);
|
||||
};
|
||||
|
||||
const setValue = (value: never) => {
|
||||
const isPreferencePending = (key: UserPreferenceKey) => {
|
||||
if (searchPreferenceKeys.includes(key as never))
|
||||
return isSearchPreferencesPending || pendingGroupsRef.current.search;
|
||||
if (homeBoardPreferenceKeys.includes(key as never))
|
||||
return isHomeBoardsPending || pendingGroupsRef.current.homeBoards;
|
||||
if (key === "firstDayOfWeek") return isFirstDayOfWeekPending || pendingGroupsRef.current.firstDayOfWeek;
|
||||
if (key === "pingIconsEnabled") return isPingIconsPending || pendingGroupsRef.current.pingIcons;
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const setValueAsync = async (key: UserPreferenceKey, value: never) => {
|
||||
if (optimisticPreferenceKeys.has(key) && !userId) return;
|
||||
if (isPreferencePending(key)) return;
|
||||
|
||||
if (optimisticPreferenceKeys.has(key)) {
|
||||
optimisticPreferencesRef.current[key] = value;
|
||||
setOptimisticValue(value);
|
||||
setOptimisticPreference(key, value);
|
||||
}
|
||||
|
||||
setPersistedValue(value);
|
||||
try {
|
||||
await setPersistedValueAsync(key, value);
|
||||
} catch {
|
||||
if (searchPreferenceKeys.includes(key as never)) {
|
||||
clearOptimisticPreferences(searchPreferenceKeys);
|
||||
} else if (homeBoardPreferenceKeys.includes(key as never)) {
|
||||
clearOptimisticPreferences(homeBoardPreferenceKeys);
|
||||
} else {
|
||||
clearOptimisticPreferences([key]);
|
||||
}
|
||||
showPreferenceErrorNotification();
|
||||
}
|
||||
};
|
||||
|
||||
const pendingStates: Record<UserPreferenceKey, boolean> = {
|
||||
colorScheme: false,
|
||||
locale: false,
|
||||
defaultSearchEngineId: isSearchPreferencesPending,
|
||||
openSearchInNewTab: isSearchPreferencesPending,
|
||||
ddgBangs: isSearchPreferencesPending,
|
||||
firstDayOfWeek: isFirstDayOfWeekPending,
|
||||
homeBoardId: isHomeBoardsPending,
|
||||
mobileHomeBoardId: isHomeBoardsPending,
|
||||
pingIconsEnabled: isPingIconsPending,
|
||||
defaultSearchEngineId: isPreferencePending("defaultSearchEngineId"),
|
||||
openSearchInNewTab: isPreferencePending("openSearchInNewTab"),
|
||||
ddgBangs: isPreferencePending("ddgBangs"),
|
||||
firstDayOfWeek: isPreferencePending("firstDayOfWeek"),
|
||||
homeBoardId: isPreferencePending("homeBoardId"),
|
||||
mobileHomeBoardId: isPreferencePending("mobileHomeBoardId"),
|
||||
pingIconsEnabled: isPreferencePending("pingIconsEnabled"),
|
||||
fullPreferencesPage: false,
|
||||
};
|
||||
|
||||
return {
|
||||
value: optimisticPreferencesRef.current[key] ?? optimisticValue ?? persistedValue,
|
||||
setValue,
|
||||
isPending: pendingStates[key],
|
||||
getPreference(preferenceKey: UserPreferenceKey) {
|
||||
return {
|
||||
value: getValue(preferenceKey),
|
||||
setValue: (value: never) => setValueAsync(preferenceKey, value),
|
||||
isPending: pendingStates[preferenceKey],
|
||||
};
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const useUserPreference = (key: UserPreferenceKey) => {
|
||||
const preferences = useUserPreferences();
|
||||
|
||||
return preferences.getPreference(key);
|
||||
};
|
||||
Reference in new issue
Block a user