| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- import {
- NextPage, GetServerSideProps, GetServerSidePropsContext,
- } from 'next';
- import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
- import dynamic from 'next/dynamic';
- import Head from 'next/head';
- import type { CrowiRequest } from '~/interfaces/crowi-request';
- import type { RendererConfig } from '~/interfaces/services/renderer';
- import type { ISidebarConfig } from '~/interfaces/sidebar-config';
- import type { IUser, IUserHasId } from '~/interfaces/user';
- import type { IUserUISettings } from '~/interfaces/user-ui-settings';
- import type { UserUISettingsModel } from '~/server/models/user-ui-settings';
- import {
- useCsrfToken, useCurrentUser, useIsSearchPage, useIsSearchScopeChildrenAsDefault,
- useIsSearchServiceConfigured, useIsSearchServiceReachable, useRendererConfig, useShowPageLimitationL,
- } from '~/stores/context';
- import {
- usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed,
- useCurrentSidebarContents, useCurrentProductNavWidth,
- } from '~/stores/ui';
- import { SearchPage } from '../components/SearchPage';
- import {
- CommonProps, getNextI18NextConfig, getServerSideCommonProps, useCustomTitle,
- } from './utils/commons';
- const SearchResultLayout = dynamic(() => import('~/components/Layout/SearchResultLayout'), { ssr: false });
- type Props = CommonProps & {
- currentUser: IUser,
- isSearchServiceConfigured: boolean,
- isSearchServiceReachable: boolean,
- isSearchScopeChildrenAsDefault: boolean,
- // UI
- userUISettings?: IUserUISettings
- // Sidebar
- sidebarConfig: ISidebarConfig,
- // Render config
- rendererConfig: RendererConfig,
- // search limit
- showPageLimitationL: number
- };
- const SearchResultPage: NextPage<Props> = (props: Props) => {
- const { userUISettings } = props;
- // commons
- useCsrfToken(props.csrfToken);
- useCurrentUser(props.currentUser ?? null);
- // Search
- useIsSearchPage(true);
- useIsSearchServiceConfigured(props.isSearchServiceConfigured);
- useIsSearchServiceReachable(props.isSearchServiceReachable);
- useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
- // UserUISettings
- usePreferDrawerModeByUser(userUISettings?.preferDrawerModeByUser ?? props.sidebarConfig.isSidebarDrawerMode);
- usePreferDrawerModeOnEditByUser(userUISettings?.preferDrawerModeOnEditByUser);
- useSidebarCollapsed(userUISettings?.isSidebarCollapsed ?? props.sidebarConfig.isSidebarClosedAtDockMode);
- useCurrentSidebarContents(userUISettings?.currentSidebarContents);
- useCurrentProductNavWidth(userUISettings?.currentProductNavWidth);
- // render config
- useRendererConfig(props.rendererConfig);
- useShowPageLimitationL(props.showPageLimitationL);
- const PutbackPageModal = (): JSX.Element => {
- const PutbackPageModal = dynamic(() => import('../components/PutbackPageModal'), { ssr: false });
- return <PutbackPageModal />;
- };
- const classNames: string[] = [];
- // if (props.isContainerFluid) {
- // classNames.push('growi-layout-fluid');
- // }
- return (
- <>
- <Head>
- {/*
- {renderScriptTagByName('drawio-viewer')}
- {renderScriptTagByName('highlight-addons')}
- */}
- </Head>
- <SearchResultLayout title={useCustomTitle(props, 'GROWI')} className={classNames.join(' ')}>
- <div id="search-page">
- <SearchPage />
- </div>
- </SearchResultLayout>
- <PutbackPageModal />
- </>
- );
- };
- async function injectUserUISettings(context: GetServerSidePropsContext, props: Props): Promise<void> {
- const { model: mongooseModel } = await import('mongoose');
- const req = context.req as CrowiRequest<IUserHasId & any>;
- const { user } = req;
- const UserUISettings = mongooseModel('UserUISettings') as UserUISettingsModel;
- const userUISettings = user == null ? null : await UserUISettings.findOne({ user: user._id }).exec();
- if (userUISettings != null) {
- props.userUISettings = userUISettings.toObject();
- }
- }
- function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
- const req: CrowiRequest = context.req as CrowiRequest;
- const { crowi } = req;
- const { configManager, searchService } = crowi;
- props.isSearchServiceConfigured = searchService.isConfigured;
- props.isSearchServiceReachable = searchService.isReachable;
- props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
- props.sidebarConfig = {
- isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
- isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
- };
- props.rendererConfig = {
- isEnabledLinebreaks: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
- isEnabledLinebreaksInComments: configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
- adminPreferredIndentSize: configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
- isIndentSizeForced: configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
- plantumlUri: process.env.PLANTUML_URI ?? null,
- blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
- // XSS Options
- isEnabledXssPrevention: configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
- attrWhiteList: crowi.xssService.getAttrWhiteList(),
- tagWhiteList: crowi.xssService.getTagWhiteList(),
- highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
- };
- props.showPageLimitationL = configManager.getConfig('crowi', 'customize:showPageLimitationL');
- }
- /**
- * for Server Side Translations
- * @param context
- * @param props
- * @param namespacesRequired
- */
- async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
- const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
- props._nextI18Next = nextI18NextConfig._nextI18Next;
- }
- export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
- const req = context.req as CrowiRequest<IUserHasId & any>;
- const { user } = req;
- const result = await getServerSideCommonProps(context);
- // check for presence
- // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
- if (!('props' in result)) {
- throw new Error('invalid getSSP result');
- }
- const props: Props = result.props as Props;
- if (user != null) {
- props.currentUser = user.toObject();
- }
- await injectUserUISettings(context, props);
- injectServerConfigurations(context, props);
- await injectNextI18NextConfigurations(context, props, ['translation']);
- return {
- props,
- };
- };
- export default SearchResultPage;
|