installer.page.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, { useMemo } from 'react';
  2. import type {
  3. NextPage, GetServerSideProps, GetServerSidePropsContext,
  4. } from 'next';
  5. import { useTranslation } from 'next-i18next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import Head from 'next/head';
  9. import { NoLoginLayout } from '~/components/Layout/NoLoginLayout';
  10. import type { CrowiRequest } from '~/interfaces/crowi-request';
  11. import {
  12. useCsrfToken, useAppTitle, useSiteUrl, useConfidential, useGrowiCloudUri,
  13. } from '~/stores-universal/context';
  14. import type { CommonProps } from './utils/commons';
  15. import { getNextI18NextConfig, getServerSideCommonProps, generateCustomTitle } from './utils/commons';
  16. const InstallerForm = dynamic(() => import('~/client/components/InstallerForm'), { ssr: false });
  17. const DataTransferForm = dynamic(() => import('~/client/components/DataTransferForm'), { ssr: false });
  18. const CustomNavAndContents = dynamic(() => import('~/client/components/CustomNavigation/CustomNavAndContents'), { ssr: false });
  19. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  20. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired, true);
  21. props._nextI18Next = nextI18NextConfig._nextI18Next;
  22. }
  23. type Props = CommonProps & {
  24. minPasswordLength: number,
  25. pageWithMetaStr: string,
  26. };
  27. const InstallerPage: NextPage<Props> = (props: Props) => {
  28. const { t } = useTranslation();
  29. const { t: tCommons } = useTranslation('commons');
  30. const navTabMapping = useMemo(() => {
  31. return {
  32. user_infomation: {
  33. Icon: () => <span className="material-symbols-outlined me-2">person</span>,
  34. Content: () => <InstallerForm minPasswordLength={props.minPasswordLength} />,
  35. i18n: t('installer.tab'),
  36. },
  37. external_accounts: {
  38. // TODO: chack and fix font-size. see: https://redmine.weseek.co.jp/issues/143015
  39. Icon: () => <span className="growi-custom-icons me-2">external_link</span>,
  40. Content: DataTransferForm,
  41. i18n: tCommons('g2g_data_transfer.tab'),
  42. },
  43. };
  44. }, [props.minPasswordLength, t, tCommons]);
  45. // commons
  46. useAppTitle(props.appTitle);
  47. useSiteUrl(props.siteUrl);
  48. useConfidential(props.confidential);
  49. useCsrfToken(props.csrfToken);
  50. useGrowiCloudUri(props.growiCloudUri);
  51. const title = generateCustomTitle(props, t('installer.title'));
  52. const classNames: string[] = [];
  53. return (
  54. <NoLoginLayout className={classNames.join(' ')}>
  55. <Head>
  56. <title>{title}</title>
  57. </Head>
  58. <div id="installer-form-container" className="nologin-dialog mx-auto rounded-4 rounded-top-0">
  59. <CustomNavAndContents navTabMapping={navTabMapping} tabContentClasses={['p-0']} />
  60. </div>
  61. </NoLoginLayout>
  62. );
  63. };
  64. async function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): Promise<void> {
  65. const req: CrowiRequest = context.req as CrowiRequest;
  66. const { crowi } = req;
  67. const { configManager } = crowi;
  68. props.minPasswordLength = configManager.getConfig('crowi', 'app:minPasswordLength');
  69. }
  70. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  71. const result = await getServerSideCommonProps(context);
  72. // check for presence
  73. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  74. if (!('props' in result)) {
  75. throw new Error('invalid getSSP result');
  76. }
  77. const props: Props = result.props as Props;
  78. await injectNextI18NextConfigurations(context, props, ['translation']);
  79. injectServerConfigurations(context, props);
  80. return {
  81. props,
  82. };
  83. };
  84. export default InstallerPage;