_app.page.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, useCustomizedLogoSrc,
  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. const isDev = process.env.NODE_ENV === 'development';
  18. const swrConfig: SWRConfigValue = {
  19. ...swrGlobalConfiguration,
  20. // set the request scoped cache provider in server
  21. provider: isServer()
  22. ? cache => new Map(cache)
  23. : undefined,
  24. };
  25. // eslint-disable-next-line @typescript-eslint/ban-types
  26. export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
  27. getLayout?: (page: ReactElement) => ReactNode,
  28. }
  29. type GrowiAppProps = AppProps & {
  30. Component: NextPageWithLayout,
  31. };
  32. // register custom serializer
  33. registerTransformerForObjectId();
  34. function GrowiApp({ Component, pageProps }: GrowiAppProps): JSX.Element {
  35. useI18nextHMR(isDev);
  36. useEffect(() => {
  37. import('bootstrap/dist/js/bootstrap');
  38. }, []);
  39. useEffect(() => {
  40. ActivatePluginService.activateAll();
  41. }, []);
  42. const commonPageProps = pageProps as CommonProps;
  43. // useInterceptorManager(new InterceptorManager());
  44. useAppTitle(commonPageProps.appTitle);
  45. useSiteUrl(commonPageProps.siteUrl);
  46. useConfidential(commonPageProps.confidential);
  47. useGrowiVersion(commonPageProps.growiVersion);
  48. useCustomizedLogoSrc(commonPageProps.customizedLogoSrc);
  49. // Use the layout defined at the page level, if available
  50. const getLayout = Component.getLayout ?? (page => page);
  51. return (
  52. <SWRConfig value={swrConfig}>
  53. {getLayout(<Component {...pageProps} />)}
  54. </SWRConfig>
  55. );
  56. }
  57. // export default appWithTranslation(GrowiApp);
  58. export default appWithTranslation(GrowiApp, nextI18nConfig);