BookmarkContents.tsx 2.1 KB

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