invited.page.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React from 'react';
  2. import type { IUserHasId, IUser } from '@growi/core';
  3. import { USER_STATUS } from '@growi/core';
  4. import { NextPage, GetServerSideProps, GetServerSidePropsContext } 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 { InvitedFormProps } from '~/components/InvitedForm';
  10. import { NoLoginLayout } from '~/components/Layout/NoLoginLayout';
  11. import type { CrowiRequest } from '~/interfaces/crowi-request';
  12. import { useCsrfToken, useCurrentPathname, useCurrentUser } from '../stores/context';
  13. import {
  14. CommonProps, getServerSideCommonProps, generateCustomTitle, getNextI18NextConfig,
  15. } from './utils/commons';
  16. const InvitedForm = dynamic<InvitedFormProps>(() => import('~/components/InvitedForm').then(mod => mod.InvitedForm), { ssr: false });
  17. type Props = CommonProps & {
  18. currentUser: IUser,
  19. invitedFormUsername: string,
  20. invitedFormName: string,
  21. }
  22. const InvitedPage: NextPage<Props> = (props: Props) => {
  23. const { t } = useTranslation();
  24. useCsrfToken(props.csrfToken);
  25. useCurrentPathname(props.currentPathname);
  26. useCurrentUser(props.currentUser);
  27. const title = generateCustomTitle(props, t('invited.title'));
  28. const classNames: string[] = ['invited-page'];
  29. return (
  30. <NoLoginLayout className={classNames.join(' ')}>
  31. <Head>
  32. <title>{title}</title>
  33. </Head>
  34. <InvitedForm invitedFormUsername={props.invitedFormUsername} invitedFormName={props.invitedFormName} />
  35. </NoLoginLayout>
  36. );
  37. };
  38. async function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): Promise<void> {
  39. const req: CrowiRequest = context.req as CrowiRequest;
  40. const { body: invitedForm } = req;
  41. if (props.invitedFormUsername != null) {
  42. props.invitedFormUsername = invitedForm.username;
  43. }
  44. if (props.invitedFormName != null) {
  45. props.invitedFormName = invitedForm.name;
  46. }
  47. }
  48. /**
  49. * for Server Side Translations
  50. * @param context
  51. * @param props
  52. * @param namespacesRequired
  53. */
  54. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  55. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  56. props._nextI18Next = nextI18NextConfig._nextI18Next;
  57. }
  58. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  59. const req = context.req as CrowiRequest;
  60. const { user } = req;
  61. const result = await getServerSideCommonProps(context);
  62. // check for presence
  63. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  64. if (!('props' in result)) {
  65. throw new Error('invalid getSSP result');
  66. }
  67. const props: Props = result.props as Props;
  68. if (user != null) {
  69. props.currentUser = user.toObject();
  70. // Only invited user can access to /invited page
  71. if (props.currentUser.status !== USER_STATUS.INVITED) {
  72. return {
  73. redirect: {
  74. permanent: false,
  75. destination: '/',
  76. },
  77. };
  78. }
  79. }
  80. await injectServerConfigurations(context, props);
  81. await injectNextI18NextConfigurations(context, props, ['translation']);
  82. return { props };
  83. };
  84. export default InvitedPage;