_app.page.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { ReactElement, ReactNode, useEffect } from 'react';
  2. import { isServer } from '@growi/core';
  3. import { NextPage } from 'next';
  4. import { appWithTranslation } from 'next-i18next';
  5. import { AppProps } from 'next/app';
  6. import { SWRConfig } from 'swr';
  7. import * as nextI18nConfig from '^/config/next-i18next.config';
  8. import { ActivatePluginService } from '~/client/services/activate-plugin';
  9. import { useI18nextHMR } from '~/services/i18next-hmr';
  10. import {
  11. useAppTitle, useConfidential, useGrowiVersion, useSiteUrl, useIsDefaultLogo, useForcedColorScheme,
  12. } from '~/stores/context';
  13. import { SWRConfigValue, swrGlobalConfiguration } from '~/utils/swr-utils';
  14. import { CommonProps } from './utils/commons';
  15. import { registerTransformerForObjectId } from './utils/objectid-transformer';
  16. import '~/styles/style-app.scss';
  17. import '~/styles/theme/_apply-colors-light.scss';
  18. import '~/styles/theme/_apply-colors-dark.scss';
  19. import '~/styles/theme/_apply-colors.scss';
  20. const isDev = process.env.NODE_ENV === 'development';
  21. const swrConfig: SWRConfigValue = {
  22. ...swrGlobalConfiguration,
  23. // set the request scoped cache provider in server
  24. provider: isServer()
  25. ? cache => new Map(cache)
  26. : undefined,
  27. };
  28. // eslint-disable-next-line @typescript-eslint/ban-types
  29. export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
  30. getLayout?: (page: ReactElement) => ReactNode,
  31. }
  32. type GrowiAppProps = AppProps & {
  33. Component: NextPageWithLayout,
  34. };
  35. // register custom serializer
  36. registerTransformerForObjectId();
  37. function GrowiApp({ Component, pageProps }: GrowiAppProps): JSX.Element {
  38. useI18nextHMR(isDev);
  39. useEffect(() => {
  40. import('bootstrap/dist/js/bootstrap');
  41. }, []);
  42. useEffect(() => {
  43. ActivatePluginService.activateAll();
  44. }, []);
  45. const commonPageProps = pageProps as CommonProps;
  46. // useInterceptorManager(new InterceptorManager());
  47. useAppTitle(commonPageProps.appTitle);
  48. useSiteUrl(commonPageProps.siteUrl);
  49. useConfidential(commonPageProps.confidential);
  50. useGrowiVersion(commonPageProps.growiVersion);
  51. useIsDefaultLogo(commonPageProps.isDefaultLogo);
  52. useForcedColorScheme(commonPageProps.forcedColorScheme);
  53. // Use the layout defined at the page level, if available
  54. const getLayout = Component.getLayout ?? (page => page);
  55. return (
  56. <SWRConfig value={swrConfig}>
  57. {getLayout(<Component {...pageProps} />)}
  58. </SWRConfig>
  59. );
  60. }
  61. // export default appWithTranslation(GrowiApp);
  62. export default appWithTranslation(GrowiApp, nextI18nConfig);