commons.ts 4.4 KB

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