2022-10-19 16:17:57 +08:00
|
|
|
import { builtInLanguages as builtInConsoleLanguages } from '@logto/phrases';
|
2023-03-20 17:24:06 +08:00
|
|
|
import type { Theme } from '@logto/schemas';
|
2023-03-20 00:15:00 +08:00
|
|
|
import { useContext, useEffect, useMemo } from 'react';
|
2022-06-07 16:05:24 +08:00
|
|
|
import { z } from 'zod';
|
|
|
|
|
2023-03-20 00:15:00 +08:00
|
|
|
import { AppThemeContext, buildDefaultAppearanceMode } from '@/contexts/AppThemeProvider';
|
2023-03-20 17:24:06 +08:00
|
|
|
import type { DynamicAppearanceMode } from '@/types/appearance-mode';
|
2023-03-16 13:34:23 +08:00
|
|
|
import { appearanceModeGuard } from '@/types/appearance-mode';
|
2022-06-07 16:05:24 +08:00
|
|
|
|
2023-02-23 16:49:45 +08:00
|
|
|
import useMeCustomData from './use-me-custom-data';
|
|
|
|
|
|
|
|
const adminConsolePreferencesKey = 'adminConsolePreferences';
|
2022-06-07 16:05:24 +08:00
|
|
|
|
|
|
|
const userPreferencesGuard = z.object({
|
2022-10-19 16:17:57 +08:00
|
|
|
language: z.enum(builtInConsoleLanguages).optional(),
|
2023-03-20 17:24:06 +08:00
|
|
|
appearanceMode: appearanceModeGuard.optional(),
|
2022-06-07 16:05:24 +08:00
|
|
|
experienceNoticeConfirmed: z.boolean().optional(),
|
2022-07-02 09:43:03 +08:00
|
|
|
getStartedHidden: z.boolean().optional(),
|
2022-11-03 10:52:34 +08:00
|
|
|
connectorSieNoticeConfirmed: z.boolean().optional(),
|
2022-06-07 16:05:24 +08:00
|
|
|
});
|
|
|
|
|
2023-06-09 21:56:20 +08:00
|
|
|
type UserPreferences = z.infer<typeof userPreferencesGuard>;
|
2022-06-07 16:05:24 +08:00
|
|
|
|
2023-03-20 17:24:06 +08:00
|
|
|
type DefaultUserPreference = {
|
|
|
|
language: (typeof builtInConsoleLanguages)[number];
|
|
|
|
appearanceMode: Theme | DynamicAppearanceMode.System;
|
|
|
|
} & Omit<UserPreferences, 'language' | 'appearanceMode'>;
|
|
|
|
|
|
|
|
const defaultUserPreferences: DefaultUserPreference = {
|
|
|
|
appearanceMode: buildDefaultAppearanceMode(),
|
|
|
|
language: 'en',
|
|
|
|
};
|
|
|
|
|
2022-06-07 16:05:24 +08:00
|
|
|
const useUserPreferences = () => {
|
2023-02-23 16:49:45 +08:00
|
|
|
const { data, error, isLoading, isLoaded, update: updateMeCustomData } = useMeCustomData();
|
2023-03-20 00:15:00 +08:00
|
|
|
const { setAppearanceMode } = useContext(AppThemeContext);
|
2023-02-23 16:49:45 +08:00
|
|
|
|
|
|
|
const userPreferences = useMemo(() => {
|
|
|
|
const parsed = z.object({ [adminConsolePreferencesKey]: userPreferencesGuard }).safeParse(data);
|
|
|
|
|
|
|
|
return parsed.success
|
2023-03-20 17:24:06 +08:00
|
|
|
? {
|
|
|
|
...defaultUserPreferences,
|
|
|
|
...parsed.data[adminConsolePreferencesKey],
|
|
|
|
}
|
|
|
|
: defaultUserPreferences;
|
2022-06-07 16:05:24 +08:00
|
|
|
}, [data]);
|
|
|
|
|
|
|
|
const update = async (data: Partial<UserPreferences>) => {
|
2023-02-23 16:49:45 +08:00
|
|
|
await updateMeCustomData({
|
|
|
|
[adminConsolePreferencesKey]: {
|
|
|
|
...userPreferences,
|
|
|
|
...data,
|
|
|
|
},
|
|
|
|
});
|
2022-06-07 16:05:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-20 00:15:00 +08:00
|
|
|
setAppearanceMode(userPreferences.appearanceMode);
|
|
|
|
}, [setAppearanceMode, userPreferences.appearanceMode]);
|
2022-06-07 16:05:24 +08:00
|
|
|
|
|
|
|
return {
|
2023-02-23 16:49:45 +08:00
|
|
|
isLoading,
|
|
|
|
isLoaded,
|
2022-06-07 16:05:24 +08:00
|
|
|
data: userPreferences,
|
|
|
|
update,
|
|
|
|
error,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useUserPreferences;
|