CustomSidebar.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import React, { FC } from 'react';
  2. import AppContainer from '~/client/services/AppContainer';
  3. import { IRevision } from '~/interfaces/revision';
  4. import { useSWRxPageByPath } from '~/stores/page';
  5. import { useCustomSidebarRenderer } from '~/stores/renderer';
  6. import loggerFactory from '~/utils/logger';
  7. import RevisionRenderer from '../Page/RevisionRenderer';
  8. const logger = loggerFactory('growi:cli:CustomSidebar');
  9. const SidebarNotFound = () => {
  10. return (
  11. <div className="grw-sidebar-content-header h5 text-center p-3">
  12. <a href="/Sidebar#edit">
  13. <i className="icon-magic-wand"></i> Create <strong>/Sidebar</strong> page
  14. </a>
  15. </div>
  16. );
  17. };
  18. type Props = {
  19. appContainer: AppContainer,
  20. };
  21. const CustomSidebar: FC<Props> = () => {
  22. const { data: renderer } = useCustomSidebarRenderer();
  23. const { data: page, error, mutate } = useSWRxPageByPath('/Sidebar');
  24. if (renderer == null) {
  25. return <></>;
  26. }
  27. const isLoading = page === undefined && error == null;
  28. const markdown = (page?.revision as IRevision | undefined)?.body;
  29. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  30. const RevisionRendererAny: any = RevisionRenderer;
  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. <RevisionRendererAny
  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. export default CustomSidebar;