maintenance.page.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import type { IUser, IUserHasId } from '@growi/core';
  2. import type { NextPage, GetServerSideProps, GetServerSidePropsContext } from 'next';
  3. import { useTranslation } from 'next-i18next';
  4. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  5. import { apiv3Post } from '~/client/util/apiv3-client';
  6. import { toastError } from '~/client/util/toastr';
  7. import type { CrowiRequest } from '~/interfaces/crowi-request';
  8. import { useCurrentUser } from '~/stores/context';
  9. import type { CommonProps } from './utils/commons';
  10. import { getServerSideCommonProps, getNextI18NextConfig } from './utils/commons';
  11. type Props = CommonProps & {
  12. currentUser: IUser,
  13. };
  14. const MaintenancePage: NextPage<CommonProps> = (props: Props) => {
  15. const { t } = useTranslation();
  16. useCurrentUser(props.currentUser ?? null);
  17. const logoutHandler = async() => {
  18. try {
  19. await apiv3Post('/logout');
  20. window.location.reload();
  21. }
  22. catch (err) {
  23. toastError(err);
  24. }
  25. };
  26. return (
  27. <div className="container-lg">
  28. <div className="container">
  29. <div className="row justify-content-md-center">
  30. <div className="col-md-6 mt-5">
  31. <div className="text-center">
  32. <h1><span className="material-symbols-outlined large">error</span></h1>
  33. <h1 className="text-center">{ t('maintenance_mode.maintenance_mode') }</h1>
  34. <h3>{ t('maintenance_mode.growi_is_under_maintenance') }</h3>
  35. <hr />
  36. <div className="text-start">
  37. {props.currentUser?.admin
  38. && (
  39. <p>
  40. <span className="material-symbols-outlined">arrow_circle_right</span>
  41. <a className="btn btn-link" href="/admin">{ t('maintenance_mode.admin_page') }</a>
  42. </p>
  43. )}
  44. {props.currentUser != null
  45. ? (
  46. <p>
  47. <span className="material-symbols-outlined">arrow_circle_right</span>
  48. <a className="btn btn-link" onClick={logoutHandler} id="maintanounse-mode-logout">{ t('maintenance_mode.logout') }</a>
  49. </p>
  50. )
  51. : (
  52. <p>
  53. <span className="material-symbols-outlined">arrow_circle_right</span>
  54. <a className="btn btn-link" href="/login">{ t('maintenance_mode.login') }</a>
  55. </p>
  56. )
  57. }
  58. </div>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. );
  65. };
  66. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  67. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  68. props._nextI18Next = nextI18NextConfig._nextI18Next;
  69. }
  70. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  71. const req = context.req as CrowiRequest;
  72. const result = await getServerSideCommonProps(context);
  73. if ('redirect' in result) {
  74. return { redirect: result.redirect };
  75. }
  76. if (!('props' in result)) {
  77. throw new Error('invalid getSSP result');
  78. }
  79. const props: Props = result.props as Props;
  80. if (props.redirectDestination != null) {
  81. return {
  82. redirect: {
  83. permanent: false,
  84. destination: props.redirectDestination,
  85. },
  86. };
  87. }
  88. const { user } = req;
  89. if (user != null) {
  90. props.currentUser = user.toObject();
  91. }
  92. await injectNextI18NextConfigurations(context, props, ['translation']);
  93. return {
  94. props,
  95. };
  96. };
  97. export default MaintenancePage;