|
|
@@ -1,12 +1,10 @@
|
|
|
-import React, {
|
|
|
- useState, useCallback, useEffect,
|
|
|
-} from 'react';
|
|
|
-import PropTypes from 'prop-types';
|
|
|
+import React, { FC } from 'react';
|
|
|
|
|
|
+import AppContainer from '~/client/services/AppContainer';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
+import { useSWRxPage } from '~/stores/page';
|
|
|
|
|
|
import { withUnstatedContainers } from '../UnstatedUtils';
|
|
|
-import AppContainer from '~/client/services/AppContainer';
|
|
|
import RevisionRenderer from '../Page/RevisionRenderer';
|
|
|
|
|
|
const logger = loggerFactory('growi:cli:CustomSidebar');
|
|
|
@@ -22,55 +20,38 @@ const SidebarNotFound = () => {
|
|
|
);
|
|
|
};
|
|
|
|
|
|
-const CustomSidebar = (props) => {
|
|
|
-
|
|
|
- const { appContainer } = props;
|
|
|
- const { apiGet } = appContainer;
|
|
|
+type Props = {
|
|
|
+ appContainer: AppContainer,
|
|
|
+};
|
|
|
|
|
|
- const [isMounted, setMounted] = useState(false);
|
|
|
- const [markdown, setMarkdown] = useState();
|
|
|
+const CustomSidebar: FC<Props> = (props: Props) => {
|
|
|
|
|
|
- const growiRenderer = appContainer.getRenderer('sidebar');
|
|
|
+ const { appContainer } = props;
|
|
|
|
|
|
- // TODO: refactor with SWR
|
|
|
- const fetchDataAndRenderHtml = useCallback(async() => {
|
|
|
- let page = null;
|
|
|
- try {
|
|
|
- const result = await apiGet('/pages.get', { path: '/Sidebar' });
|
|
|
- page = result.page;
|
|
|
- }
|
|
|
- catch (e) {
|
|
|
- logger.warn(e.message);
|
|
|
- return;
|
|
|
- }
|
|
|
- finally {
|
|
|
- setMounted(true);
|
|
|
- }
|
|
|
+ const renderer = appContainer.getRenderer('sidebar');
|
|
|
|
|
|
- setMarkdown(page.revision.body);
|
|
|
- }, [apiGet]);
|
|
|
+ const { data: page, mutate } = useSWRxPage('/Sidebar');
|
|
|
|
|
|
- useEffect(() => {
|
|
|
- fetchDataAndRenderHtml();
|
|
|
- }, [fetchDataAndRenderHtml]);
|
|
|
+ const isLoading = page === undefined;
|
|
|
+ const markdown = page?.revision?.body;
|
|
|
|
|
|
return (
|
|
|
<>
|
|
|
<div className="grw-sidebar-content-header p-3 d-flex">
|
|
|
- <h3 className="mb-0 text-nowrap">
|
|
|
+ <h3 className="mb-0">
|
|
|
Custom Sidebar
|
|
|
<a className="h6 ml-2" href="/Sidebar"><i className="icon-pencil"></i></a>
|
|
|
</h3>
|
|
|
- <button type="button" className="btn btn-sm ml-auto grw-btn-reload" onClick={fetchDataAndRenderHtml}>
|
|
|
+ <button type="button" className="btn btn-sm btn-outline-secondary ml-auto" onClick={() => mutate()}>
|
|
|
<i className="icon icon-reload"></i>
|
|
|
</button>
|
|
|
</div>
|
|
|
- { isMounted && markdown == null && <SidebarNotFound /> }
|
|
|
+ { !isLoading && markdown == null && <SidebarNotFound /> }
|
|
|
{/* eslint-disable-next-line react/no-danger */}
|
|
|
{ markdown != null && (
|
|
|
<div className="p-3">
|
|
|
<RevisionRenderer
|
|
|
- growiRenderer={growiRenderer}
|
|
|
+ growiRenderer={renderer}
|
|
|
markdown={markdown}
|
|
|
additionalClassName="grw-custom-sidebar-content"
|
|
|
/>
|
|
|
@@ -81,10 +62,6 @@ const CustomSidebar = (props) => {
|
|
|
|
|
|
};
|
|
|
|
|
|
-CustomSidebar.propTypes = {
|
|
|
- appContainer: PropTypes.instanceOf(AppContainer).isRequired,
|
|
|
-};
|
|
|
-
|
|
|
/**
|
|
|
* Wrapper component for using unstated
|
|
|
*/
|