commons.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type { ColorScheme, IUserHasId } from '@growi/core';
  2. import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
  3. import type { CrowiRequest } from '~/interfaces/crowi-request';
  4. import { getGrowiVersion } from '~/utils/growi-version';
  5. export type CommonInitialProps = {
  6. appTitle: string,
  7. siteUrl: string | undefined,
  8. confidential: string,
  9. growiVersion: string,
  10. isDefaultLogo: boolean,
  11. growiCloudUri: string | undefined,
  12. forcedColorScheme?: ColorScheme,
  13. };
  14. export const getServerSideCommonInitialProps: GetServerSideProps<CommonInitialProps> = async(context: GetServerSidePropsContext) => {
  15. const req = context.req as CrowiRequest;
  16. const { crowi } = req;
  17. const {
  18. appService, configManager, attachmentService,
  19. } = crowi;
  20. const isCustomizedLogoUploaded = await attachmentService.isBrandLogoExist();
  21. const isDefaultLogo = crowi.configManager.getConfig('customize:isDefaultLogo') || !isCustomizedLogoUploaded;
  22. const forcedColorScheme = crowi.customizeService.forcedColorScheme;
  23. return {
  24. props: {
  25. appTitle: appService.getAppTitle(),
  26. siteUrl: configManager.getConfig('app:siteUrl'), // DON'T USE growiInfoService.getSiteUrl()
  27. confidential: appService.getAppConfidential() || '',
  28. growiVersion: getGrowiVersion(),
  29. isDefaultLogo,
  30. growiCloudUri: configManager.getConfig('app:growiCloudUri'),
  31. forcedColorScheme,
  32. },
  33. };
  34. };
  35. export type CommonEachProps = {
  36. currentPathname: string,
  37. currentUser?: IUserHasId,
  38. nextjsRoutingPage?: string,
  39. csrfToken: string,
  40. isMaintenanceMode: boolean,
  41. redirectDestination?: string | null,
  42. };
  43. export const getServerSideCommonEachProps: GetServerSideProps<CommonEachProps> = async(context: GetServerSidePropsContext) => {
  44. const req = context.req as CrowiRequest;
  45. const { crowi, user } = req;
  46. const { appService } = crowi;
  47. const url = new URL(context.resolvedUrl, 'http://example.com');
  48. const currentPathname = decodeURIComponent(url.pathname);
  49. const isMaintenanceMode = appService.isMaintenanceMode();
  50. let currentUser;
  51. if (user != null) {
  52. currentUser = user.toObject();
  53. }
  54. // Redirect destination for page transition by next/link
  55. let redirectDestination: string | null = null;
  56. if (!crowi.aclService.isGuestAllowedToRead() && currentUser == null) {
  57. redirectDestination = '/login';
  58. }
  59. else if (!isMaintenanceMode && currentPathname === '/maintenance') {
  60. redirectDestination = '/';
  61. }
  62. else if (isMaintenanceMode && !currentPathname.match('/admin/*') && !(currentPathname === '/maintenance')) {
  63. redirectDestination = '/maintenance';
  64. }
  65. else {
  66. redirectDestination = null;
  67. }
  68. return {
  69. props: {
  70. currentPathname,
  71. currentUser,
  72. csrfToken: req.csrfToken(),
  73. isMaintenanceMode,
  74. redirectDestination,
  75. },
  76. };
  77. };