BookmarkContents.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { useCallback, useState } from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { addNewFolder } from '~/client/util/bookmark-utils';
  4. import { toastError } from '~/client/util/toastr';
  5. import { BookmarkFolderNameInput } from '~/components/Bookmarks/BookmarkFolderNameInput';
  6. import { BookmarkFolderTree } from '~/components/Bookmarks/BookmarkFolderTree';
  7. import { useSWRxBookmarkFolderAndChild } from '~/stores/bookmark-folder';
  8. import { useCurrentUser } from '~/stores/context';
  9. export const BookmarkContents = (): JSX.Element => {
  10. const { t } = useTranslation();
  11. const [isCreateAction, setIsCreateAction] = useState<boolean>(false);
  12. const { data: currentUser } = useCurrentUser();
  13. const { mutate: mutateBookmarkFolders } = useSWRxBookmarkFolderAndChild(currentUser?._id);
  14. const onClickNewBookmarkFolder = useCallback(() => {
  15. setIsCreateAction(true);
  16. }, []);
  17. const cancel = useCallback(() => {
  18. setIsCreateAction(false);
  19. }, []);
  20. const create = useCallback(async(folderName: string) => {
  21. if (folderName.trim() === '') {
  22. return cancel();
  23. }
  24. try {
  25. await addNewFolder(folderName.trim(), null);
  26. await mutateBookmarkFolders();
  27. setIsCreateAction(false);
  28. }
  29. catch (err) {
  30. toastError(err);
  31. }
  32. }, [cancel, mutateBookmarkFolders]);
  33. return (
  34. <div>
  35. <div className="mb-2">
  36. <button
  37. type="button"
  38. className="btn btn-outline-secondary rounded-pill d-flex justify-content-start align-middle"
  39. onClick={onClickNewBookmarkFolder}
  40. >
  41. <div className="d-flex align-items-center">
  42. <span className="material-symbols-outlined">create_new_folder</span>
  43. <span className="ms-2">{t('bookmark_folder.new_folder')}</span>
  44. </div>
  45. </button>
  46. </div>
  47. {isCreateAction && (
  48. <div className="col-12 mb-2 ">
  49. <BookmarkFolderNameInput
  50. onPressEnter={create}
  51. onBlur={create}
  52. onPressEscape={cancel}
  53. />
  54. </div>
  55. )}
  56. <BookmarkFolderTree isOperable userId={currentUser?._id} />
  57. </div>
  58. );
  59. };