MenuItem.hooks.ts 1019 B

12345678910111213141516171819202122232425262728293031
  1. import { DocumentControllerContext } from '$app/stores/effects/document/document_controller';
  2. import { useAppDispatch } from '@/appflowy_app/stores/store';
  3. import { useCallback, useContext } from 'react';
  4. import { insertAfterNodeThunk, deleteNodeThunk } from '@/appflowy_app/stores/reducers/document/async_actions';
  5. export enum ActionType {
  6. InsertAfter = 'insertAfter',
  7. Remove = 'remove',
  8. }
  9. export function useActions(id: string, type: ActionType) {
  10. const dispatch = useAppDispatch();
  11. const controller = useContext(DocumentControllerContext);
  12. const insertAfter = useCallback(async () => {
  13. if (!controller) return;
  14. await dispatch(insertAfterNodeThunk({ id, controller }));
  15. }, [id, controller, dispatch]);
  16. const remove = useCallback(async () => {
  17. if (!controller) return;
  18. await dispatch(deleteNodeThunk({ id, controller }));
  19. }, [id, dispatch]);
  20. if (type === ActionType.InsertAfter) {
  21. return insertAfter;
  22. }
  23. if (type === ActionType.Remove) {
  24. return remove;
  25. }
  26. return;
  27. }