CustomSidebar.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 { useCustomSidebarOptions } 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 { data: rendererOptions } = useCustomSidebarOptions();
  24. const { data: page, error, mutate } = useSWRxPageByPath('/Sidebar');
  25. if (rendererOptions == null) {
  26. return <></>;
  27. }
  28. const isLoading = page === undefined && error == null;
  29. const markdown = (page?.revision as IRevision | undefined)?.body;
  30. return (
  31. <>
  32. <div className="grw-sidebar-content-header p-3 d-flex">
  33. <h3 className="mb-0">
  34. Custom Sidebar
  35. <a className="h6 ml-2" href="/Sidebar"><i className="icon-pencil"></i></a>
  36. </h3>
  37. <button type="button" className="btn btn-sm ml-auto grw-btn-reload" onClick={() => mutate()}>
  38. <i className="icon icon-reload"></i>
  39. </button>
  40. </div>
  41. {
  42. isLoading && (
  43. <div className="text-muted text-center">
  44. <i className="fa fa-2x fa-spinner fa-pulse mr-1"></i>
  45. </div>
  46. )
  47. }
  48. {
  49. (!isLoading && markdown != null) && (
  50. <div className="p-3">
  51. <RevisionRenderer
  52. rendererOptions={rendererOptions}
  53. markdown={markdown}
  54. pagePath="/Sidebar"
  55. additionalClassName="grw-custom-sidebar-content"
  56. />
  57. </div>
  58. )
  59. }
  60. {
  61. (!isLoading && markdown === undefined) && (
  62. <SidebarNotFound />
  63. )
  64. }
  65. </>
  66. );
  67. };
  68. export default CustomSidebar;