commons.ts 4.5 KB

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