|
|
@@ -1,3 +1,7 @@
|
|
|
+import {
|
|
|
+ useCallback, useEffect, useRef,
|
|
|
+} from 'react';
|
|
|
+
|
|
|
import { HtmlElementNode } from 'rehype-toc';
|
|
|
import useSWR, { SWRResponse } from 'swr';
|
|
|
import useSWRImmutable from 'swr/immutable';
|
|
|
@@ -17,19 +21,30 @@ import { useCurrentPagePath } from './page';
|
|
|
import { useCurrentPageTocNode } from './ui';
|
|
|
|
|
|
|
|
|
-export const useViewOptions = (storeTocNodeHandler: (toc: HtmlElementNode) => void): SWRResponse<RendererOptions, Error> => {
|
|
|
+export const useViewOptions = (): SWRResponse<RendererOptions, Error> => {
|
|
|
const { data: currentPagePath } = useCurrentPagePath();
|
|
|
const { data: rendererConfig } = useRendererConfig();
|
|
|
+ const { mutate: mutateCurrentPageTocNode } = useCurrentPageTocNode();
|
|
|
|
|
|
- const isAllDataValid = currentPagePath != null && rendererConfig != null;
|
|
|
+ const tocRef = useRef<HtmlElementNode|undefined>();
|
|
|
|
|
|
- const key = isAllDataValid
|
|
|
- ? ['viewOptions', currentPagePath, rendererConfig]
|
|
|
- : null;
|
|
|
+ const storeTocNodeHandler = useCallback((toc: HtmlElementNode) => {
|
|
|
+ tocRef.current = toc;
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ mutateCurrentPageTocNode(tocRef.current);
|
|
|
+ // using useRef not to re-render
|
|
|
+ // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
+ }, [mutateCurrentPageTocNode, tocRef.current]);
|
|
|
+
|
|
|
+ const isAllDataValid = currentPagePath != null && rendererConfig != null;
|
|
|
|
|
|
- return useSWR<RendererOptions, Error>(
|
|
|
- key,
|
|
|
- (rendererId, currentPagePath, rendererConfig) => {
|
|
|
+ return useSWR(
|
|
|
+ isAllDataValid
|
|
|
+ ? ['viewOptions', currentPagePath, rendererConfig]
|
|
|
+ : null,
|
|
|
+ ([, currentPagePath, rendererConfig]) => {
|
|
|
// determine options generator
|
|
|
const optionsGenerator = getGrowiFacade().markdownRenderer?.optionsGenerators?.customGenerateViewOptions ?? generateViewOptions;
|
|
|
return optionsGenerator(currentPagePath, rendererConfig, storeTocNodeHandler);
|
|
|
@@ -47,13 +62,11 @@ export const useTocOptions = (): SWRResponse<RendererOptions, Error> => {
|
|
|
|
|
|
const isAllDataValid = currentPagePath != null && rendererConfig != null && tocNode != null;
|
|
|
|
|
|
- const key = isAllDataValid
|
|
|
- ? ['tocOptions', currentPagePath, tocNode, rendererConfig]
|
|
|
- : null;
|
|
|
-
|
|
|
- return useSWRImmutable<RendererOptions, Error>(
|
|
|
- key,
|
|
|
- (rendererId, path, tocNode, rendererConfig) => generateTocOptions(rendererConfig, tocNode),
|
|
|
+ return useSWRImmutable(
|
|
|
+ isAllDataValid
|
|
|
+ ? ['tocOptions', currentPagePath, tocNode, rendererConfig]
|
|
|
+ : null,
|
|
|
+ ([, , tocNode, rendererConfig]) => generateTocOptions(rendererConfig, tocNode),
|
|
|
{
|
|
|
fallbackData: isAllDataValid ? generateTocOptions(rendererConfig, tocNode) : undefined,
|
|
|
},
|
|
|
@@ -66,16 +79,14 @@ export const usePreviewOptions = (): SWRResponse<RendererOptions, Error> => {
|
|
|
|
|
|
const isAllDataValid = currentPagePath != null && rendererConfig != null;
|
|
|
|
|
|
- const key = isAllDataValid
|
|
|
- ? ['previewOptions', rendererConfig, currentPagePath]
|
|
|
- : null;
|
|
|
-
|
|
|
- return useSWR<RendererOptions, Error>(
|
|
|
- key,
|
|
|
- (rendererId, rendererConfig, pagePath, highlightKeywords) => {
|
|
|
+ return useSWR(
|
|
|
+ isAllDataValid
|
|
|
+ ? ['previewOptions', rendererConfig, currentPagePath]
|
|
|
+ : null,
|
|
|
+ ([, rendererConfig, pagePath]) => {
|
|
|
// determine options generator
|
|
|
const optionsGenerator = getGrowiFacade().markdownRenderer?.optionsGenerators?.customGeneratePreviewOptions ?? generatePreviewOptions;
|
|
|
- return optionsGenerator(rendererConfig, pagePath, highlightKeywords);
|
|
|
+ return optionsGenerator(rendererConfig, pagePath);
|
|
|
},
|
|
|
{
|
|
|
fallbackData: isAllDataValid ? generatePreviewOptions(rendererConfig, currentPagePath) : undefined,
|
|
|
@@ -89,13 +100,11 @@ export const useCommentForCurrentPageOptions = (): SWRResponse<RendererOptions,
|
|
|
|
|
|
const isAllDataValid = currentPagePath != null && rendererConfig != null;
|
|
|
|
|
|
- const key = isAllDataValid
|
|
|
- ? ['commentPreviewOptions', rendererConfig, currentPagePath]
|
|
|
- : null;
|
|
|
-
|
|
|
- return useSWRImmutable<RendererOptions, Error>(
|
|
|
- key,
|
|
|
- (rendererId, rendererConfig, currentPagePath) => generateSimpleViewOptions(
|
|
|
+ return useSWRImmutable(
|
|
|
+ isAllDataValid
|
|
|
+ ? ['commentPreviewOptions', rendererConfig, currentPagePath]
|
|
|
+ : null,
|
|
|
+ ([, rendererConfig, currentPagePath]) => generateSimpleViewOptions(
|
|
|
rendererConfig,
|
|
|
currentPagePath,
|
|
|
undefined,
|
|
|
@@ -118,13 +127,11 @@ export const useSelectedPagePreviewOptions = (pagePath: string, highlightKeyword
|
|
|
|
|
|
const isAllDataValid = rendererConfig != null;
|
|
|
|
|
|
- const key = isAllDataValid
|
|
|
- ? ['selectedPagePreviewOptions', rendererConfig, pagePath, highlightKeywords]
|
|
|
- : null;
|
|
|
-
|
|
|
- return useSWRImmutable<RendererOptions, Error>(
|
|
|
- key,
|
|
|
- (rendererId, rendererConfig, pagePath, highlightKeywords) => generateSimpleViewOptions(rendererConfig, pagePath, highlightKeywords),
|
|
|
+ return useSWRImmutable(
|
|
|
+ isAllDataValid
|
|
|
+ ? ['selectedPagePreviewOptions', rendererConfig, pagePath, highlightKeywords]
|
|
|
+ : null,
|
|
|
+ ([, rendererConfig, pagePath, highlightKeywords]) => generateSimpleViewOptions(rendererConfig, pagePath, highlightKeywords),
|
|
|
{
|
|
|
fallbackData: isAllDataValid ? generateSimpleViewOptions(rendererConfig, pagePath, highlightKeywords) : undefined,
|
|
|
},
|
|
|
@@ -139,13 +146,11 @@ export const useCustomSidebarOptions = (): SWRResponse<RendererOptions, Error> =
|
|
|
|
|
|
const isAllDataValid = rendererConfig != null;
|
|
|
|
|
|
- const key = isAllDataValid
|
|
|
- ? ['customSidebarOptions', rendererConfig]
|
|
|
- : null;
|
|
|
-
|
|
|
- return useSWRImmutable<RendererOptions, Error>(
|
|
|
- key,
|
|
|
- (rendererId, rendererConfig, pagePath, highlightKeywords) => generateSimpleViewOptions(rendererConfig, pagePath, highlightKeywords),
|
|
|
+ return useSWRImmutable(
|
|
|
+ isAllDataValid
|
|
|
+ ? ['customSidebarOptions', rendererConfig]
|
|
|
+ : null,
|
|
|
+ ([, rendererConfig]) => generateSimpleViewOptions(rendererConfig, '/'),
|
|
|
{
|
|
|
fallbackData: isAllDataValid ? generateSimpleViewOptions(rendererConfig, '/') : undefined,
|
|
|
},
|