CustomSidebar.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { SidebarHeaderReloadButton } from './SidebarHeaderReloadButton';
  10. import CustomSidebarContentSkeleton from './Skeleton/CustomSidebarContentSkeleton';
  11. import styles from './CustomSidebar.module.scss';
  12. const logger = loggerFactory('growi:cli:CustomSidebar');
  13. const SidebarNotFound = () => {
  14. return (
  15. <div className="grw-sidebar-content-header h5 text-center py-3">
  16. <Link href="/Sidebar#edit">
  17. <i className="icon-magic-wand"></i>Create<strong>/Sidebar</strong>page
  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. <div className="px-3">
  33. <div className="grw-sidebar-content-header py-3 d-flex">
  34. <h3 className="mb-0">
  35. {t('CustomSidebar')}
  36. <Link href="/Sidebar#edit" className="h6 ml-2"><i className="icon-pencil"></i></Link>
  37. </h3>
  38. <SidebarHeaderReloadButton onClick={() => mutate()} />
  39. </div>
  40. {
  41. isLoading && (
  42. <CustomSidebarContentSkeleton />
  43. )
  44. }
  45. {
  46. (!isLoading && markdown != null) && (
  47. <div className={`py-3 grw-custom-sidebar-content ${styles['grw-custom-sidebar-content']}`}>
  48. <RevisionRenderer
  49. rendererOptions={rendererOptions}
  50. markdown={markdown}
  51. />
  52. </div>
  53. )
  54. }
  55. {
  56. (!isLoading && markdown === undefined) && (
  57. <SidebarNotFound />
  58. )
  59. }
  60. </div>
  61. );
  62. };
  63. export default CustomSidebar;