CustomSidebar.tsx 2.1 KB

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