use-create-todays-memo.tsx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { useCallback } from 'react';
  2. import { Origin } from '@growi/core';
  3. import { userHomepagePath } from '@growi/core/dist/utils/page-path-utils';
  4. import { format } from 'date-fns/format';
  5. import { useTranslation } from 'react-i18next';
  6. import { useCreatePage } from '~/client/services/create-page';
  7. import { useCurrentUser } from '~/stores/context';
  8. type UseCreateTodaysMemo = () => {
  9. isCreating: boolean,
  10. todaysPath: string | null,
  11. createTodaysMemo: () => Promise<void>,
  12. }
  13. export const useCreateTodaysMemo: UseCreateTodaysMemo = () => {
  14. const { t } = useTranslation('commons');
  15. const { data: currentUser } = useCurrentUser();
  16. const { isCreating, create } = useCreatePage();
  17. const isCreatable = currentUser != null;
  18. const parentDirName = t('create_page_dropdown.todays.memo');
  19. const now = format(new Date(), 'yyyy/MM/dd');
  20. const parentPath = `${userHomepagePath(currentUser)}/${parentDirName}`;
  21. const todaysPath = isCreatable
  22. ? `${parentPath}/${now}`
  23. : null;
  24. const createTodaysMemo = useCallback(async() => {
  25. if (!isCreatable || todaysPath == null) return;
  26. return create(
  27. {
  28. path: todaysPath, parentPath, wip: true, origin: Origin.View,
  29. },
  30. );
  31. }, [create, isCreatable, todaysPath, parentPath]);
  32. return {
  33. isCreating,
  34. todaysPath,
  35. createTodaysMemo,
  36. };
  37. };