0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-03 21:48:55 -05:00
logto/packages/ui/src/hooks/use-page-context.ts
simeng-li 6865efff27
refactor(ui): refactor app notification logic (#1241)
refactor app notification logic
2022-06-27 13:22:17 +08:00

72 lines
2 KiB
TypeScript

import { useState, useMemo, createContext } from 'react';
import { isMobile } from 'react-device-detect';
import { SignInExperienceSettings, Platform, Theme } from '@/types';
export type Context = {
theme: Theme;
toast: string;
loading: boolean;
platform: Platform;
termsAgreement: boolean;
experienceSettings: SignInExperienceSettings | undefined;
setTheme: React.Dispatch<React.SetStateAction<Theme>>;
setToast: React.Dispatch<React.SetStateAction<string>>;
setLoading: React.Dispatch<React.SetStateAction<boolean>>;
setPlatform: React.Dispatch<React.SetStateAction<Platform>>;
setTermsAgreement: React.Dispatch<React.SetStateAction<boolean>>;
setExperienceSettings: React.Dispatch<React.SetStateAction<SignInExperienceSettings | undefined>>;
};
const noop = () => {
throw new Error('Context provider not found');
};
export const PageContext = createContext<Context>({
toast: '',
theme: 'light',
loading: false,
platform: isMobile ? 'mobile' : 'web',
termsAgreement: false,
experienceSettings: undefined,
setTheme: noop,
setToast: noop,
setLoading: noop,
setPlatform: noop,
setTermsAgreement: noop,
setExperienceSettings: noop,
});
const usePageContext = () => {
const [loading, setLoading] = useState(false);
const [toast, setToast] = useState('');
const [theme, setTheme] = useState<Theme>('light');
const [platform, setPlatform] = useState<Platform>(isMobile ? 'mobile' : 'web');
const [experienceSettings, setExperienceSettings] = useState<SignInExperienceSettings>();
const [termsAgreement, setTermsAgreement] = useState(false);
const context = useMemo(
() => ({
theme,
toast,
loading,
platform,
termsAgreement,
experienceSettings,
setTheme,
setLoading,
setToast,
setPlatform,
setTermsAgreement,
setExperienceSettings,
}),
[experienceSettings, loading, platform, termsAgreement, theme, toast]
);
return {
context,
Provider: PageContext.Provider,
};
};
export default usePageContext;