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