CustomSidebar.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import React, { FC } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import Link from 'next/link';
  4. import { IRevision } from '~/interfaces/revision';
  5. import { useSWRxPageByPath } from '~/stores/page';
  6. import { useCustomSidebarOptions } from '~/stores/renderer';
  7. import loggerFactory from '~/utils/logger';
  8. import RevisionRenderer from '../Page/RevisionRenderer';
  9. import styles from './CustomSidebar.module.scss';
  10. const logger = loggerFactory('growi:cli:CustomSidebar');
  11. const SidebarNotFound = () => {
  12. return (
  13. <div className="grw-sidebar-content-header h5 text-center p-3">
  14. <Link href="/Sidebar#edit">
  15. <a href="/Sidebar#edit">
  16. <i className="icon-magic-wand"></i> Create <strong>/Sidebar</strong> page
  17. </a>
  18. </Link>
  19. </div>
  20. );
  21. };
  22. const CustomSidebar: FC = () => {
  23. const { t } = useTranslation();
  24. const { data: rendererOptions } = useCustomSidebarOptions();
  25. const { data: page, error, mutate } = useSWRxPageByPath('/Sidebar');
  26. if (rendererOptions == null) {
  27. return <></>;
  28. }
  29. const isLoading = page === undefined && error == null;
  30. const markdown = (page?.revision as IRevision | undefined)?.body;
  31. return (
  32. <>
  33. <div className="grw-sidebar-content-header p-3 d-flex">
  34. <h3 className="mb-0">
  35. {t('CustomSidebar')}
  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 grw-custom-sidebar-content ${styles['grw-custom-sidebar-content']}`}>
  52. <RevisionRenderer
  53. rendererOptions={rendererOptions}
  54. markdown={markdown}
  55. />
  56. </div>
  57. )
  58. }
  59. {
  60. (!isLoading && markdown === undefined) && (
  61. <SidebarNotFound />
  62. )
  63. }
  64. </>
  65. );
  66. };
  67. export default CustomSidebar;