From 6bdf6cd92837254e7ceacdb35918b5912b931198 Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Mon, 9 May 2022 23:08:40 +0800 Subject: [PATCH] refactor(console): refactor guide pages --- .../components/GuideModal}/index.module.scss | 0 .../components/GuideModal/index.tsx | 123 +++++++++++++-- .../components/LibrarySelector/index.tsx | 8 +- .../components/GuideModal/index.tsx | 16 +- packages/console/src/pages/Guide/index.tsx | 141 ------------------ packages/phrases/src/locales/en.ts | 8 +- packages/phrases/src/locales/zh-cn.ts | 8 +- 7 files changed, 119 insertions(+), 185 deletions(-) rename packages/console/src/pages/{Guide => Applications/components/GuideModal}/index.module.scss (100%) delete mode 100644 packages/console/src/pages/Guide/index.tsx diff --git a/packages/console/src/pages/Guide/index.module.scss b/packages/console/src/pages/Applications/components/GuideModal/index.module.scss similarity index 100% rename from packages/console/src/pages/Guide/index.module.scss rename to packages/console/src/pages/Applications/components/GuideModal/index.module.scss diff --git a/packages/console/src/pages/Applications/components/GuideModal/index.tsx b/packages/console/src/pages/Applications/components/GuideModal/index.tsx index 81929d5ca..0114e7dcb 100644 --- a/packages/console/src/pages/Applications/components/GuideModal/index.tsx +++ b/packages/console/src/pages/Applications/components/GuideModal/index.tsx @@ -1,12 +1,23 @@ -import React from 'react'; +import { MDXProvider } from '@mdx-js/react'; +import i18next from 'i18next'; +import { MDXProps } from 'mdx/types'; +import React, { cloneElement, lazy, LazyExoticComponent, Suspense, useState } from 'react'; +import { useForm, FormProvider } from 'react-hook-form'; import Modal from 'react-modal'; -import Guide from '@/pages/Guide'; +import Button from '@/components/Button'; +import CardTitle from '@/components/CardTitle'; +import CodeEditor from '@/components/CodeEditor'; +import DangerousRaw from '@/components/DangerousRaw'; +import IconButton from '@/components/IconButton'; +import Spacer from '@/components/Spacer'; +import Close from '@/icons/Close'; import * as modalStyles from '@/scss/modal.module.scss'; import { SupportedJavascriptLibraries } from '@/types/applications'; import { GuideForm } from '@/types/guide'; import LibrarySelector from '../LibrarySelector'; +import * as styles from './index.module.scss'; type Props = { appName: string; @@ -15,17 +26,101 @@ type Props = { onComplete: (data: GuideForm) => Promise; }; -const GuideModal = ({ appName, isOpen, onClose, onComplete }: Props) => ( - - } - title={appName} - subtitle="applications.get_started.header_description" - defaultSubtype={SupportedJavascriptLibraries.React} - onClose={onClose} - onComplete={onComplete} - /> - -); +const Guides: Record JSX.Element>> = { + react: lazy(async () => import('@/assets/docs/tutorial/integrate-sdk/react.mdx')), + 'react_zh-cn': lazy(async () => import('@/assets/docs/tutorial/integrate-sdk/react_zh-cn.mdx')), +}; + +const onClickFetchSampleProject = (projectName: string) => { + const sampleUrl = `https://github.com/logto-io/js/tree/master/packages/${projectName}-sample`; + window.open(sampleUrl, '_blank'); +}; + +const GuideModal = ({ appName, isOpen, onClose, onComplete }: Props) => { + const [subtype, setSubtype] = useState(SupportedJavascriptLibraries.React); + const [activeStepIndex, setActiveStepIndex] = useState(-1); + const [invalidStepIndex, setInvalidStepIndex] = useState(-1); + + const locale = i18next.language; + const guideKey = `${subtype}_${locale}`.toLowerCase(); + const GuideComponent = Guides[guideKey] ?? Guides[subtype]; + + const methods = useForm({ mode: 'onSubmit', reValidateMode: 'onChange' }); + const { + formState: { isSubmitting }, + handleSubmit, + } = methods; + + const onSubmit = handleSubmit((data) => { + if (isSubmitting) { + return; + } + void onComplete(data); + }); + + return ( + +
+
+ + + +
+ {appName}} + subtitle="applications.guide.header_description" + /> + +
+
+ +
+ {cloneElement(, { + className: styles.banner, + onChange: setSubtype, + onToggle: () => { + setActiveStepIndex(0); + }, + })} + { + const [, language] = /language-(\w+)/.exec(className ?? '') ?? []; + + return ; + }, + }} + > + Loading...
}> + {GuideComponent && ( + { + setActiveStepIndex(nextIndex); + }} + onError={(invalidIndex: number) => { + setInvalidStepIndex(invalidIndex); + }} + /> + )} + + + + +
+
+
+ ); +}; export default GuideModal; diff --git a/packages/console/src/pages/Applications/components/LibrarySelector/index.tsx b/packages/console/src/pages/Applications/components/LibrarySelector/index.tsx index 7d794a5e6..aa4d78c40 100644 --- a/packages/console/src/pages/Applications/components/LibrarySelector/index.tsx +++ b/packages/console/src/pages/Applications/components/LibrarySelector/index.tsx @@ -32,8 +32,8 @@ const LibrarySelector = ({ success
-
{t('applications.get_started.title')}
-
{t('applications.get_started.subtitle')}
+
{t('applications.guide.title')}
+
{t('applications.guide.subtitle')}
(
Tada! - - {t('applications.get_started.description_by_library', { library: libraryName })} - + {t('applications.guide.description_by_library', { library: libraryName })}
), [className, libraryName, t] diff --git a/packages/console/src/pages/Connectors/components/GuideModal/index.tsx b/packages/console/src/pages/Connectors/components/GuideModal/index.tsx index 16a4346d4..a221c2f4d 100644 --- a/packages/console/src/pages/Connectors/components/GuideModal/index.tsx +++ b/packages/console/src/pages/Connectors/components/GuideModal/index.tsx @@ -29,11 +29,6 @@ type Props = { onComplete?: (data: GuideForm) => Promise; }; -const onClickFetchSampleProject = (name: string) => { - const sampleUrl = `https://github.com/logto-io/js/tree/master/packages/connectors/${name}-sample`; - window.open(sampleUrl, '_blank'); -}; - const GuideModal = ({ connector, isOpen, onClose }: Props) => { const api = useApi(); const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' }); @@ -88,19 +83,10 @@ const GuideModal = ({ connector, isOpen, onClose }: Props) => { {connectorName}} - subtitle="connectors.get_started.subtitle" + subtitle="connectors.guide.subtitle" />