Refs.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import React, { useMemo, type JSX } from 'react';
  2. import { useSWRxRefs } from '../stores/refs';
  3. import { AttachmentList } from './AttachmentList';
  4. import { RefsContext } from './util/refs-context';
  5. type Props = {
  6. pagePath: string;
  7. prefix?: string;
  8. depth?: string;
  9. regexp?: string;
  10. isImmutable?: boolean;
  11. };
  12. const RefsSubstance = React.memo(
  13. ({
  14. pagePath,
  15. prefix,
  16. depth,
  17. regexp,
  18. isImmutable,
  19. }: Props): JSX.Element => {
  20. const refsContext = useMemo(() => {
  21. const options = {
  22. prefix,
  23. depth,
  24. regexp,
  25. };
  26. return new RefsContext('refs', pagePath, options);
  27. }, [pagePath, prefix, depth, regexp]);
  28. const { data, error, isLoading } = useSWRxRefs(
  29. pagePath,
  30. prefix,
  31. { depth, regexp },
  32. isImmutable,
  33. );
  34. const attachments = data != null ? data : [];
  35. return (
  36. <AttachmentList
  37. refsContext={refsContext}
  38. isLoading={isLoading}
  39. error={error}
  40. attachments={attachments}
  41. />
  42. );
  43. },
  44. );
  45. export const Refs = React.memo((props: Props): JSX.Element => {
  46. return <RefsSubstance {...props} />;
  47. });
  48. export const RefsImmutable = React.memo(
  49. (props: Omit<Props, 'isImmutable'>): JSX.Element => {
  50. return <RefsSubstance {...props} isImmutable />;
  51. },
  52. );
  53. Refs.displayName = 'Refs';