UseNewPageInput.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { useState, type FC, useCallback } from 'react';
  2. import { apiv3Post } from '~/client/util/apiv3-client';
  3. import { useSWRxPageChildren } from '~/stores/page-listing';
  4. import { usePageTreeDescCountMap } from '~/stores/ui';
  5. import { NewPageCreateButton } from './NewPageCreateButton';
  6. import { NewPageInput } from './NewPageInput';
  7. import type { SimpleItemContentProps } from './interfaces';
  8. type UseNewPageInput = {
  9. Input: FC<SimpleItemContentProps>,
  10. CreateButton: FC<SimpleItemContentProps>,
  11. isProcessingSubmission: boolean,
  12. }
  13. export const useNewPageInput = (): UseNewPageInput => {
  14. const [showInput, setShowInput] = useState(false);
  15. const [isProcessingSubmission, setProcessingSubmission] = useState(false);
  16. const { getDescCount } = usePageTreeDescCountMap();
  17. const CreateButton: FC<SimpleItemContentProps> = (props) => {
  18. const { page, children, stateHandlers } = props;
  19. const { setIsOpen } = stateHandlers;
  20. // descendantCount
  21. const descendantCount = getDescCount(page._id) || page.descendantCount || 0;
  22. const isChildrenLoaded = children?.length > 0;
  23. const hasDescendants = descendantCount > 0 || isChildrenLoaded;
  24. const onClick = useCallback(() => {
  25. setShowInput(true);
  26. if (hasDescendants) {
  27. setIsOpen(true);
  28. }
  29. }, [hasDescendants, setIsOpen]);
  30. return (
  31. <NewPageCreateButton
  32. page={props.page}
  33. onClick={onClick}
  34. />
  35. );
  36. };
  37. const Input: FC<SimpleItemContentProps> = (props) => {
  38. const {
  39. page, children, stateHandlers,
  40. } = props;
  41. const { isOpen, setIsOpen } = stateHandlers;
  42. const { mutate: mutateChildren } = useSWRxPageChildren(isOpen ? page._id : null);
  43. const { getDescCount } = usePageTreeDescCountMap();
  44. const descendantCount = getDescCount(page._id) || page.descendantCount || 0;
  45. const isChildrenLoaded = children?.length > 0;
  46. const hasDescendants = descendantCount > 0 || isChildrenLoaded;
  47. const submitHandler = useCallback(async(newPagePath: string) => {
  48. setProcessingSubmission(true);
  49. setShowInput(false);
  50. await apiv3Post('/pages/', {
  51. path: newPagePath,
  52. body: undefined,
  53. grant: page.grant,
  54. // grantUserGroupId: page.grantedGroup,
  55. grantUserGroupIds: page.grantedGroups,
  56. });
  57. mutateChildren();
  58. if (!hasDescendants) {
  59. setIsOpen(true);
  60. }
  61. }, [hasDescendants, mutateChildren, page.grant, page.grantedGroups, setIsOpen]);
  62. const submittionFailedHandler = useCallback(() => {
  63. setProcessingSubmission(false);
  64. }, []);
  65. return showInput
  66. ? (
  67. <NewPageInput
  68. page={props.page}
  69. isEnableActions={props.isEnableActions}
  70. onSubmit={submitHandler}
  71. onSubmittionFailed={submittionFailedHandler}
  72. onCanceled={() => setShowInput(false)}
  73. />
  74. )
  75. : <></>;
  76. };
  77. return {
  78. Input,
  79. CreateButton,
  80. isProcessingSubmission,
  81. };
  82. };