use-handsontable.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { useCallback } from 'react';
  2. import { EditorView } from '@codemirror/view';
  3. import { useSWRStatic } from '@growi/core/dist/swr';
  4. import type { SWRResponse } from 'swr';
  5. type HandsontableModalStatus = {
  6. isOpened: boolean,
  7. editor?: EditorView,
  8. }
  9. type HandsontableModalStatusUtils = {
  10. open(
  11. editor?: EditorView,
  12. ): void
  13. close(): void
  14. }
  15. export const useHandsontableModalForEditor = (status?: HandsontableModalStatus): SWRResponse<HandsontableModalStatus, Error> & HandsontableModalStatusUtils => {
  16. const initialData: HandsontableModalStatus = {
  17. isOpened: false,
  18. editor: undefined,
  19. };
  20. const swrResponse = useSWRStatic<HandsontableModalStatus, Error>('handsontableModalStatus', status, { fallbackData: initialData });
  21. const { mutate } = swrResponse;
  22. const open = useCallback((editor?: EditorView): void => {
  23. mutate({
  24. isOpened: true, editor,
  25. });
  26. }, [mutate]);
  27. const close = useCallback((): void => {
  28. mutate({
  29. isOpened: false, editor: undefined,
  30. });
  31. }, [mutate]);
  32. return {
  33. ...swrResponse,
  34. open,
  35. close,
  36. };
  37. };