CustomSidebar.tsx 2.1 KB

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