commons.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { DevidedPagePath, Lang, AllLang } from '@growi/core';
  2. import { GetServerSideProps, GetServerSidePropsContext } from 'next';
  3. import { SSRConfig, UserConfig } from 'next-i18next';
  4. import * as nextI18NextConfig from '^/config/next-i18next.config';
  5. import { SupportedActionType } from '~/interfaces/activity';
  6. import { CrowiRequest } from '~/interfaces/crowi-request';
  7. import { GrowiThemes } from '~/interfaces/theme';
  8. export type CommonProps = {
  9. namespacesRequired: string[], // i18next
  10. currentPathname: string,
  11. appTitle: string,
  12. siteUrl: string,
  13. confidential: string,
  14. theme: GrowiThemes,
  15. customTitleTemplate: string,
  16. csrfToken: string,
  17. isContainerFluid: boolean,
  18. growiVersion: string,
  19. isMaintenanceMode: boolean,
  20. redirectDestination: string | null,
  21. } & Partial<SSRConfig>;
  22. // eslint-disable-next-line max-len
  23. export const getServerSideCommonProps: GetServerSideProps<CommonProps> = async(context: GetServerSidePropsContext) => {
  24. const req: CrowiRequest = context.req as CrowiRequest;
  25. const { crowi } = req;
  26. const {
  27. appService, configManager, customizeService,
  28. } = crowi;
  29. const url = new URL(context.resolvedUrl, 'http://example.com');
  30. const currentPathname = decodeURI(url.pathname);
  31. const isMaintenanceMode = appService.isMaintenanceMode();
  32. // eslint-disable-next-line max-len, no-nested-ternary
  33. const redirectDestination = !isMaintenanceMode && currentPathname === '/maintenance' ? '/' : isMaintenanceMode && !currentPathname.match('/admin/*') && !(currentPathname === '/maintenance') ? '/maintenance' : null;
  34. const props: CommonProps = {
  35. namespacesRequired: ['translation'],
  36. currentPathname,
  37. appTitle: appService.getAppTitle(),
  38. siteUrl: configManager.getConfig('crowi', 'app:siteUrl'), // DON'T USE appService.getSiteUrl()
  39. confidential: appService.getAppConfidential() || '',
  40. theme: configManager.getConfig('crowi', 'customize:theme'),
  41. customTitleTemplate: customizeService.customTitleTemplate,
  42. csrfToken: req.csrfToken(),
  43. isContainerFluid: configManager.getConfig('crowi', 'customize:isContainerFluid') ?? false,
  44. growiVersion: crowi.version,
  45. isMaintenanceMode,
  46. redirectDestination,
  47. };
  48. return { props };
  49. };
  50. export const getNextI18NextConfig = async(
  51. // 'serverSideTranslations' method should be given from Next.js Page
  52. // because importing it in this file causes https://github.com/isaachinman/next-i18next/issues/1545
  53. serverSideTranslations: (
  54. initialLocale: string, namespacesRequired?: string[] | undefined, configOverride?: UserConfig | null, extraLocales?: string[] | false
  55. ) => Promise<SSRConfig>,
  56. context: GetServerSidePropsContext, namespacesRequired?: string[] | undefined, preloadAllLang = false,
  57. ): Promise<SSRConfig> => {
  58. const req: CrowiRequest = context.req as CrowiRequest;
  59. const { crowi, user } = req;
  60. const { configManager } = crowi;
  61. // determine language
  62. const locale = user?.lang
  63. ?? configManager.getConfig('crowi', 'app:globalLang') as Lang
  64. ?? Lang.en_US;
  65. return serverSideTranslations(locale, namespacesRequired ?? ['translation'], nextI18NextConfig, preloadAllLang ? AllLang : false);
  66. };
  67. /**
  68. * Generate whole title string for the specified title
  69. * @param props
  70. * @param title
  71. */
  72. export const useCustomTitle = (props: CommonProps, title: string): string => {
  73. return props.customTitleTemplate
  74. .replace('{{sitename}}', props.appTitle)
  75. .replace('{{page}}', title)
  76. .replace('{{pagepath}}', title)
  77. .replace('{{pagename}}', title);
  78. };
  79. /**
  80. * Generate whole title string for the specified page path
  81. * @param props
  82. * @param pagePath
  83. */
  84. export const useCustomTitleForPage = (props: CommonProps, pagePath: string): string => {
  85. const dPagePath = new DevidedPagePath(pagePath, true, true);
  86. return props.customTitleTemplate
  87. .replace('{{sitename}}', props.appTitle)
  88. .replace('{{pagepath}}', pagePath)
  89. .replace('{{page}}', dPagePath.latter) // for backward compatibility
  90. .replace('{{pagename}}', dPagePath.latter);
  91. };