0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

refactor(console): remove superstruct dependency from console (#3808)

This commit is contained in:
Charles Zhao 2023-05-05 17:32:40 +08:00 committed by GitHub
parent fafe27f87a
commit 260f39f72d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 35 deletions

View file

@ -111,7 +111,6 @@
"recharts": "^2.1.13",
"remark-gfm": "^3.0.1",
"stylelint": "^15.0.0",
"superstruct": "^0.16.0",
"swr": "^1.3.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",

View file

@ -1,5 +1,4 @@
import { emailRegEx } from '@logto/core-kit';
import { conditional } from '@silverhand/essentials';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useLocation, useNavigate } from 'react-router-dom';
@ -10,7 +9,7 @@ import { adminTenantEndpoint, meApi } from '@/consts';
import { useStaticApi } from '@/hooks/use-api';
import MainFlowLikeModal from '../../components/MainFlowLikeModal';
import { checkLocationState } from '../../utils';
import { parseLocationState } from '../../utils';
type EmailForm = {
email: string;
@ -44,7 +43,7 @@ function LinkEmailModal() {
})();
};
const currentEmail = conditional(checkLocationState(state) && state.email);
const { email: currentEmail } = parseLocationState(state);
return (
<MainFlowLikeModal

View file

@ -14,7 +14,7 @@ import { useConfirmModal } from '@/hooks/use-confirm-modal';
import useCurrentUser from '@/hooks/use-current-user';
import MainFlowLikeModal from '../../components/MainFlowLikeModal';
import { checkLocationState, handleError } from '../../utils';
import { handleError, parseLocationState } from '../../utils';
import * as styles from './index.module.scss';
@ -40,9 +40,7 @@ function VerificationCodeModal() {
resourceIndicator: meApi.indicator,
hideErrorToast: true,
});
const { email, action } = checkLocationState(state)
? state
: { email: undefined, action: undefined };
const { email, action } = parseLocationState(state);
const { seconds, isRunning, restart } = useTimer({
autoStart: true,

View file

@ -1,4 +1,3 @@
import { conditional } from '@silverhand/essentials';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
@ -15,7 +14,7 @@ import { adminTenantEndpoint, meApi } from '@/consts';
import { useStaticApi } from '@/hooks/use-api';
import MainFlowLikeModal from '../../components/MainFlowLikeModal';
import { checkLocationState, handleError } from '../../utils';
import { handleError, parseLocationState } from '../../utils';
import * as styles from './index.module.scss';
@ -43,7 +42,7 @@ function VerifyPasswordModal() {
hideErrorToast: true,
});
const [showPassword, setShowPassword] = useState(false);
const email = conditional(checkLocationState(state) && state.email);
const { email } = parseLocationState(state);
const onClose = () => {
navigate('/profile');

View file

@ -2,19 +2,13 @@ import type { RequestErrorBody } from '@logto/schemas';
import { HTTPError } from 'ky';
import { toast } from 'react-hot-toast';
export type LocationState = {
email: string;
action: 'changePassword' | 'changeEmail';
};
import { type LocationState, locationStateGuard } from '@/types/profile';
export const checkLocationState = (state: unknown): state is LocationState =>
typeof state === 'object' &&
state !== null &&
'email' in state &&
'action' in state &&
typeof state.email === 'string' &&
typeof state.action === 'string' &&
['changePassword', 'changeEmail'].includes(state.action);
export const parseLocationState = (state: unknown): Partial<LocationState> => {
const parsed = locationStateGuard.safeParse(state);
return parsed.success ? parsed.data : { email: undefined, action: undefined };
};
export const popupWindow = (url: string, windowName: string, width: number, height: number) => {
const outerHeight = window.top?.outerHeight ?? 0;

View file

@ -1,8 +1,9 @@
import * as s from 'superstruct';
import { emailRegEx } from '@logto/core-kit';
import { z } from 'zod';
export const locationStateGuard = s.object({
email: s.string(),
action: s.union([s.literal('changeEmail'), s.literal('changePassword')]),
export const locationStateGuard = z.object({
email: z.string().regex(emailRegEx),
action: z.union([z.literal('changeEmail'), z.literal('changePassword')]),
});
export type LocationState = s.Infer<typeof locationStateGuard>;
export type LocationState = z.infer<typeof locationStateGuard>;

View file

@ -3026,9 +3026,6 @@ importers:
stylelint:
specifier: ^15.0.0
version: 15.0.0
superstruct:
specifier: ^0.16.0
version: 0.16.0
swr:
specifier: ^1.3.0
version: 1.3.0(react@18.2.0)
@ -18861,10 +18858,6 @@ packages:
- supports-color
dev: true
/superstruct@0.16.0:
resolution: {integrity: sha512-IDQtwnnlaan1NhuHqyD/U11lROYvCQ79JyfwlFU9xEVHzqV/Ys/RrwmHPCG0CVH/1g0BuodEjH1msxK2UHxehA==}
dev: true
/superstruct@1.0.3:
resolution: {integrity: sha512-8iTn3oSS8nRGn+C2pgXSKPI3jmpm6FExNazNpjvqS6ZUJQCej3PUXEKM8NjHBOs54ExM+LPW/FBRhymrdcCiSg==}
engines: {node: '>=14.0.0'}