2
0

CustomSidebar.tsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. additionalClassName="grw-custom-sidebar-content"
  52. />
  53. </div>
  54. )
  55. }
  56. {
  57. (!isLoading && markdown === undefined) && (
  58. <SidebarNotFound />
  59. )
  60. }
  61. </>
  62. );
  63. };
  64. export default CustomSidebar;