ui.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { type RefObject, useCallback, useEffect } from 'react';
  2. import { PageGrant, type Nullable } from '@growi/core';
  3. import { type SWRResponseWithUtils, useSWRStatic } from '@growi/core/dist/swr';
  4. import { pagePathUtils, isClient, isServer } from '@growi/core/dist/utils';
  5. import { Breakpoint } from '@growi/ui/dist/interfaces';
  6. import { addBreakpointListener, cleanupBreakpointListener } from '@growi/ui/dist/utils';
  7. import type { HtmlElementNode } from 'rehype-toc';
  8. import type SimpleBar from 'simplebar-react';
  9. import {
  10. useSWRConfig, type SWRResponse, type Key,
  11. } from 'swr';
  12. import useSWRImmutable from 'swr/immutable';
  13. import type { IFocusable } from '~/client/interfaces/focusable';
  14. import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
  15. import type { IPageGrantData } from '~/interfaces/page';
  16. import type { ISidebarConfig } from '~/interfaces/sidebar-config';
  17. import { SidebarContentsType } from '~/interfaces/ui';
  18. import type { UpdateDescCountData } from '~/interfaces/websocket';
  19. import {
  20. useIsNotFound, useCurrentPagePath, useIsTrashPage, useCurrentPageId,
  21. } from '~/stores/page';
  22. import loggerFactory from '~/utils/logger';
  23. import {
  24. useIsEditable, useIsReadOnlyUser,
  25. useIsSharedUser, useIsIdenticalPath, useCurrentUser, useShareLinkId,
  26. } from './context';
  27. import { useStaticSWR } from './use-static-swr';
  28. const { isTrashTopPage, isUsersTopPage } = pagePathUtils;
  29. const logger = loggerFactory('growi:stores:ui');
  30. /** **********************************************************
  31. * Unions
  32. *********************************************************** */
  33. export const EditorMode = {
  34. View: 'view',
  35. Editor: 'editor',
  36. } as const;
  37. export type EditorMode = typeof EditorMode[keyof typeof EditorMode];
  38. /** **********************************************************
  39. * Storing objects to ref
  40. *********************************************************** */
  41. export const useSidebarScrollerRef = (initialData?: RefObject<SimpleBar>): SWRResponse<RefObject<SimpleBar>, Error> => {
  42. return useStaticSWR<RefObject<SimpleBar>, Error>('sidebarScrollerRef', initialData);
  43. };
  44. export const useCurrentPageTocNode = (): SWRResponse<HtmlElementNode, any> => {
  45. const { data: currentPagePath } = useCurrentPagePath();
  46. return useStaticSWR(['currentPageTocNode', currentPagePath]);
  47. };
  48. /** **********************************************************
  49. * SWR Hooks
  50. * for switching UI
  51. *********************************************************** */
  52. export const useIsMobile = (): SWRResponse<boolean, Error> => {
  53. const key = isClient() ? 'isMobile' : null;
  54. let configuration;
  55. if (isClient()) {
  56. const userAgent = window.navigator.userAgent.toLowerCase();
  57. configuration = {
  58. fallbackData: /iphone|ipad|android/.test(userAgent),
  59. };
  60. }
  61. return useStaticSWR<boolean, Error>(key, undefined, configuration);
  62. };
  63. const getClassNamesByEditorMode = (editorMode: EditorMode | undefined): string[] => {
  64. const classNames: string[] = [];
  65. switch (editorMode) {
  66. case EditorMode.Editor:
  67. classNames.push('editing', 'builtin-editor');
  68. break;
  69. }
  70. return classNames;
  71. };
  72. export const EditorModeHash = {
  73. View: '',
  74. Edit: '#edit',
  75. } as const;
  76. export type EditorModeHash = typeof EditorModeHash[keyof typeof EditorModeHash];
  77. export const isEditorModeHash = (hash: string): hash is EditorModeHash => Object.values<string>(EditorModeHash).includes(hash);
  78. const updateHashByEditorMode = (newEditorMode: EditorMode) => {
  79. const { pathname, search } = window.location;
  80. switch (newEditorMode) {
  81. case EditorMode.View:
  82. window.history.replaceState(null, '', `${pathname}${search}${EditorModeHash.View}`);
  83. break;
  84. case EditorMode.Editor:
  85. window.history.replaceState(null, '', `${pathname}${search}${EditorModeHash.Edit}`);
  86. break;
  87. }
  88. };
  89. export const determineEditorModeByHash = (): EditorMode => {
  90. if (isServer()) {
  91. return EditorMode.View;
  92. }
  93. const { hash } = window.location;
  94. switch (hash) {
  95. case EditorModeHash.Edit:
  96. return EditorMode.Editor;
  97. default:
  98. return EditorMode.View;
  99. }
  100. };
  101. type EditorModeUtils = {
  102. getClassNamesByEditorMode: () => string[],
  103. }
  104. export const useEditorMode = (): SWRResponseWithUtils<EditorModeUtils, EditorMode> => {
  105. const { data: _isEditable } = useIsEditable();
  106. const editorModeByHash = determineEditorModeByHash();
  107. const isLoading = _isEditable === undefined;
  108. const isEditable = !isLoading && _isEditable;
  109. const initialData = isEditable ? editorModeByHash : EditorMode.View;
  110. const swrResponse = useSWRImmutable(
  111. isLoading ? null : ['editorMode', isEditable],
  112. null,
  113. { fallbackData: initialData },
  114. );
  115. // construct overriding mutate method
  116. const mutateOriginal = swrResponse.mutate;
  117. const mutate = useCallback((editorMode: EditorMode, shouldRevalidate?: boolean) => {
  118. if (!isEditable) {
  119. return Promise.resolve(EditorMode.View); // fixed if not editable
  120. }
  121. updateHashByEditorMode(editorMode);
  122. return mutateOriginal(editorMode, shouldRevalidate);
  123. }, [isEditable, mutateOriginal]);
  124. const getClassNames = useCallback(() => {
  125. return getClassNamesByEditorMode(swrResponse.data);
  126. }, [swrResponse.data]);
  127. return Object.assign(swrResponse, {
  128. mutate,
  129. getClassNamesByEditorMode: getClassNames,
  130. });
  131. };
  132. export const useIsDeviceSmallerThanMd = (): SWRResponse<boolean, Error> => {
  133. const key: Key = isClient() ? 'isDeviceSmallerThanMd' : null;
  134. const { cache, mutate } = useSWRConfig();
  135. useEffect(() => {
  136. if (key != null) {
  137. const mdOrAvobeHandler = function(this: MediaQueryList): void {
  138. // sm -> md: matches will be true
  139. // md -> sm: matches will be false
  140. mutate(key, !this.matches);
  141. };
  142. const mql = addBreakpointListener(Breakpoint.MD, mdOrAvobeHandler);
  143. // initialize
  144. if (cache.get(key)?.data == null) {
  145. cache.set(key, { ...cache.get(key), data: !mql.matches });
  146. }
  147. return () => {
  148. cleanupBreakpointListener(mql, mdOrAvobeHandler);
  149. };
  150. }
  151. }, [cache, key, mutate]);
  152. return useStaticSWR(key);
  153. };
  154. export const useIsDeviceSmallerThanLg = (): SWRResponse<boolean, Error> => {
  155. const key: Key = isClient() ? 'isDeviceSmallerThanLg' : null;
  156. const { cache, mutate } = useSWRConfig();
  157. useEffect(() => {
  158. if (key != null) {
  159. const lgOrAvobeHandler = function(this: MediaQueryList): void {
  160. // md -> lg: matches will be true
  161. // lg -> md: matches will be false
  162. mutate(key, !this.matches);
  163. };
  164. const mql = addBreakpointListener(Breakpoint.LG, lgOrAvobeHandler);
  165. // initialize
  166. if (cache.get(key)?.data == null) {
  167. cache.set(key, { ...cache.get(key), data: !mql.matches });
  168. }
  169. return () => {
  170. cleanupBreakpointListener(mql, lgOrAvobeHandler);
  171. };
  172. }
  173. }, [cache, key, mutate]);
  174. return useStaticSWR(key);
  175. };
  176. export const usePreferCollapsedModeByUser = (initialData?: boolean): SWRResponse<boolean> => {
  177. return useSWRStatic('preferCollapsedModeByUser', initialData);
  178. };
  179. export const useSidebarCollapsed = (initialData?: boolean): SWRResponse<boolean, Error> => {
  180. return useSWRStatic('isSidebarCollapsed', initialData, { fallbackData: false });
  181. };
  182. export const useCurrentSidebarContents = (initialData?: SidebarContentsType): SWRResponse<SidebarContentsType, Error> => {
  183. return useSWRStatic('sidebarContents', initialData, { fallbackData: SidebarContentsType.TREE });
  184. };
  185. export const useCurrentProductNavWidth = (initialData?: number): SWRResponse<number, Error> => {
  186. return useSWRStatic('productNavWidth', initialData, { fallbackData: 320 });
  187. };
  188. export const useDrawerMode = (): SWRResponse<boolean, Error> => {
  189. const { data: editorMode } = useEditorMode();
  190. const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
  191. const condition = editorMode != null && isDeviceSmallerThanMd != null;
  192. const calcDrawerMode = (
  193. _keyString: string,
  194. editorMode: EditorMode,
  195. isDeviceSmallerThanMd: boolean,
  196. ): boolean => {
  197. return isDeviceSmallerThanMd
  198. ? true
  199. : editorMode === EditorMode.Editor;
  200. };
  201. return useSWRImmutable(
  202. condition ? ['isDrawerMode', editorMode, isDeviceSmallerThanMd] : null,
  203. // calcDrawerMode,
  204. key => calcDrawerMode(...key),
  205. condition
  206. ? {
  207. fallbackData: calcDrawerMode('isDrawerMode', editorMode, isDeviceSmallerThanMd),
  208. }
  209. : undefined,
  210. );
  211. };
  212. export const useSidebarResizeDisabled = (isDisabled?: boolean): SWRResponse<boolean, Error> => {
  213. return useStaticSWR('isSidebarResizeDisabled', isDisabled, { fallbackData: false });
  214. };
  215. export const useSelectedGrant = (initialData?: Nullable<IPageGrantData>): SWRResponse<Nullable<IPageGrantData>, Error> => {
  216. return useStaticSWR<Nullable<IPageGrantData>, Error>('selectedGrant', initialData, { fallbackData: { grant: PageGrant.GRANT_PUBLIC } });
  217. };
  218. export const useGlobalSearchFormRef = (initialData?: RefObject<IFocusable>): SWRResponse<RefObject<IFocusable>, Error> => {
  219. return useStaticSWR('globalSearchTypeahead', initialData);
  220. };
  221. type PageTreeDescCountMapUtils = {
  222. update(newData?: UpdateDescCountData): Promise<UpdateDescCountData | undefined>
  223. getDescCount(pageId?: string): number | null | undefined
  224. }
  225. export const usePageTreeDescCountMap = (initialData?: UpdateDescCountData): SWRResponse<UpdateDescCountData, Error> & PageTreeDescCountMapUtils => {
  226. const key = 'pageTreeDescCountMap';
  227. const swrResponse = useStaticSWR<UpdateDescCountData, Error>(key, initialData, { fallbackData: new Map() });
  228. return {
  229. ...swrResponse,
  230. getDescCount: (pageId?: string) => (pageId != null ? swrResponse.data?.get(pageId) : null),
  231. update: (newData: UpdateDescCountData) => swrResponse.mutate(new Map([...(swrResponse.data || new Map()), ...newData])),
  232. };
  233. };
  234. /** **********************************************************
  235. * SWR Hooks
  236. * Determined value by context
  237. *********************************************************** */
  238. export const useIsAbleToShowTrashPageManagementButtons = (): SWRResponse<boolean, Error> => {
  239. const { data: currentUser } = useCurrentUser();
  240. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  241. const { data: isTrashPage } = useIsTrashPage();
  242. return useStaticSWR('isAbleToShowTrashPageManagementButtons', isTrashPage && currentUser != null && !isReadOnlyUser);
  243. };
  244. export const useIsAbleToShowPageManagement = (): SWRResponse<boolean, Error> => {
  245. const key = 'isAbleToShowPageManagement';
  246. const { data: currentPageId } = useCurrentPageId();
  247. const { data: _isTrashPage } = useIsTrashPage();
  248. const { data: _isSharedUser } = useIsSharedUser();
  249. const { data: isNotFound } = useIsNotFound();
  250. const pageId = currentPageId;
  251. const includesUndefined = [pageId, _isTrashPage, _isSharedUser, isNotFound].some(v => v === undefined);
  252. const isPageExist = (pageId != null) && isNotFound === false;
  253. const isEmptyPage = (pageId != null) && isNotFound === true;
  254. const isTrashPage = isPageExist && _isTrashPage === true;
  255. const isSharedUser = isPageExist && _isSharedUser === true;
  256. return useSWRImmutable(
  257. includesUndefined ? null : [key, pageId, isPageExist, isEmptyPage, isTrashPage, isSharedUser],
  258. ([, , isPageExist, isEmptyPage, isTrashPage, isSharedUser]) => (isPageExist && !isTrashPage && !isSharedUser) || isEmptyPage,
  259. );
  260. };
  261. export const useIsAbleToShowTagLabel = (): SWRResponse<boolean, Error> => {
  262. const key = 'isAbleToShowTagLabel';
  263. const { data: pageId } = useCurrentPageId();
  264. const { data: currentPagePath } = useCurrentPagePath();
  265. const { data: isIdenticalPath } = useIsIdenticalPath();
  266. const { data: isNotFound } = useIsNotFound();
  267. const { data: editorMode } = useEditorMode();
  268. const { data: shareLinkId } = useShareLinkId();
  269. const includesUndefined = [currentPagePath, isIdenticalPath, isNotFound, editorMode].some(v => v === undefined);
  270. const isViewMode = editorMode === EditorMode.View;
  271. return useSWRImmutable(
  272. includesUndefined ? null : [key, pageId, currentPagePath, isIdenticalPath, isNotFound, editorMode, shareLinkId],
  273. // "/trash" page does not exist on page collection and unable to add tags
  274. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  275. () => !isUsersTopPage(currentPagePath!) && !isTrashTopPage(currentPagePath!) && shareLinkId == null && !isIdenticalPath && !(isViewMode && isNotFound),
  276. );
  277. };
  278. export const useIsAbleToChangeEditorMode = (): SWRResponse<boolean, Error> => {
  279. const key = 'isAbleToChangeEditorMode';
  280. const { data: isEditable } = useIsEditable();
  281. const { data: isSharedUser } = useIsSharedUser();
  282. const includesUndefined = [isEditable, isSharedUser].some(v => v === undefined);
  283. return useSWRImmutable(
  284. includesUndefined ? null : [key, isEditable, isSharedUser],
  285. () => !!isEditable && !isSharedUser,
  286. );
  287. };
  288. export const useIsAbleToShowPageAuthors = (): SWRResponse<boolean, Error> => {
  289. const key = 'isAbleToShowPageAuthors';
  290. const { data: pageId } = useCurrentPageId();
  291. const { data: pagePath } = useCurrentPagePath();
  292. const { data: isNotFound } = useIsNotFound();
  293. const includesUndefined = [pageId, pagePath, isNotFound].some(v => v === undefined);
  294. const isPageExist = (pageId != null) && !isNotFound;
  295. const isUsersTopPagePath = pagePath != null && isUsersTopPage(pagePath);
  296. return useSWRImmutable(
  297. includesUndefined ? null : [key, pageId, pagePath, isNotFound],
  298. () => isPageExist && !isUsersTopPagePath,
  299. );
  300. };