CustomSidebar.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { FC } from 'react';
  2. import type { IRevision } from '@growi/core';
  3. import { useTranslation } from 'next-i18next';
  4. import Link from 'next/link';
  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. const { t } = useTranslation();
  15. return (
  16. <div className="grw-sidebar-content-header h5 text-center py-3">
  17. <Link href="/Sidebar#edit">
  18. <i className="icon-fw icon-magic-wand"></i>
  19. <span dangerouslySetInnerHTML={{ __html: t('Create Sidebar Page') }}></span>
  20. </Link>
  21. </div>
  22. );
  23. };
  24. const CustomSidebar: FC = () => {
  25. const { t } = useTranslation();
  26. const { data: rendererOptions } = useCustomSidebarOptions();
  27. const { data: page, error, mutate } = useSWRxPageByPath('/Sidebar');
  28. if (rendererOptions == null) {
  29. return <></>;
  30. }
  31. const isLoading = page === undefined && error == null;
  32. const markdown = (page?.revision as IRevision | undefined)?.body;
  33. return (
  34. <div className="px-3">
  35. <div className="grw-sidebar-content-header py-3 d-flex">
  36. <h3 className="mb-0">
  37. {t('CustomSidebar')}
  38. <Link href="/Sidebar#edit" className="h6 ml-2"><i className="icon-pencil"></i></Link>
  39. </h3>
  40. <SidebarHeaderReloadButton onClick={() => mutate()} />
  41. </div>
  42. {
  43. isLoading && (
  44. <CustomSidebarContentSkeleton />
  45. )
  46. }
  47. {
  48. (!isLoading && markdown != null) && (
  49. <div className={`py-3 grw-custom-sidebar-content ${styles['grw-custom-sidebar-content']}`}>
  50. <RevisionRenderer
  51. rendererOptions={rendererOptions}
  52. markdown={markdown}
  53. />
  54. </div>
  55. )
  56. }
  57. {
  58. (!isLoading && markdown === undefined) && (
  59. <SidebarNotFound />
  60. )
  61. }
  62. </div>
  63. );
  64. };
  65. export default CustomSidebar;