CustomSidebar.tsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React, { FC } from 'react';
  2. import AppContainer from '~/client/services/AppContainer';
  3. import loggerFactory from '~/utils/logger';
  4. import { useSWRxPageByPath } from '~/stores/page';
  5. import { withUnstatedContainers } from '../UnstatedUtils';
  6. import RevisionRenderer from '../Page/RevisionRenderer';
  7. import { IRevision } from '~/interfaces/revision';
  8. import { useCustomSidebarRenderer } from '~/stores/renderer';
  9. const logger = loggerFactory('growi:cli:CustomSidebar');
  10. const SidebarNotFound = () => {
  11. return (
  12. <div className="grw-sidebar-content-header h5 text-center p-3">
  13. <a href="/Sidebar#edit">
  14. <i className="icon-magic-wand"></i> Create <strong>/Sidebar</strong> page
  15. </a>
  16. </div>
  17. );
  18. };
  19. type Props = {
  20. appContainer: AppContainer,
  21. };
  22. const CustomSidebar: FC<Props> = (props: Props) => {
  23. const { appContainer } = props;
  24. const { data: renderer } = useCustomSidebarRenderer();
  25. const { data: page, error, mutate } = useSWRxPageByPath('/Sidebar');
  26. if (renderer == null) {
  27. return <></>;
  28. }
  29. const isLoading = page === undefined && error == null;
  30. const markdown = (page?.revision as IRevision | undefined)?.body;
  31. return (
  32. <>
  33. <div className="grw-sidebar-content-header p-3 d-flex">
  34. <h3 className="mb-0">
  35. Custom Sidebar
  36. <a className="h6 ml-2" href="/Sidebar"><i className="icon-pencil"></i></a>
  37. </h3>
  38. <button type="button" className="btn btn-sm ml-auto grw-btn-reload" onClick={() => mutate()}>
  39. <i className="icon icon-reload"></i>
  40. </button>
  41. </div>
  42. {
  43. isLoading && (
  44. <div className="text-muted text-center">
  45. <i className="fa fa-2x fa-spinner fa-pulse mr-1"></i>
  46. </div>
  47. )
  48. }
  49. {
  50. (!isLoading && markdown != null) && (
  51. <div className="p-3">
  52. <RevisionRenderer
  53. growiRenderer={renderer}
  54. markdown={markdown}
  55. pagePath="/Sidebar"
  56. additionalClassName="grw-custom-sidebar-content"
  57. />
  58. </div>
  59. )
  60. }
  61. {
  62. (!isLoading && markdown === undefined) && (
  63. <SidebarNotFound />
  64. )
  65. }
  66. </>
  67. );
  68. };
  69. /**
  70. * Wrapper component for using unstated
  71. */
  72. const CustomSidebarWrapper = withUnstatedContainers(CustomSidebar, [AppContainer]);
  73. export default CustomSidebarWrapper;