ChatMenu.svelte 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <script lang="ts">
  2. import { DropdownMenu } from 'bits-ui';
  3. import { flyAndScale } from '$lib/utils/transitions';
  4. import { getContext, createEventDispatcher } from 'svelte';
  5. const dispatch = createEventDispatcher();
  6. import Dropdown from '$lib/components/common/Dropdown.svelte';
  7. import GarbageBin from '$lib/components/icons/GarbageBin.svelte';
  8. import Pencil from '$lib/components/icons/Pencil.svelte';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import Tags from '$lib/components/chat/Tags.svelte';
  11. import Share from '$lib/components/icons/Share.svelte';
  12. import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte';
  13. import DocumentDuplicate from '$lib/components/icons/DocumentDuplicate.svelte';
  14. import Bookmark from '$lib/components/icons/Bookmark.svelte';
  15. import BookmarkSlash from '$lib/components/icons/BookmarkSlash.svelte';
  16. import { addTagById, deleteTagById, getTagsById } from '$lib/apis/chats';
  17. const i18n = getContext('i18n');
  18. export let shareHandler: Function;
  19. export let cloneChatHandler: Function;
  20. export let archiveChatHandler: Function;
  21. export let renameHandler: Function;
  22. export let deleteHandler: Function;
  23. export let onClose: Function;
  24. export let chatId = '';
  25. let show = false;
  26. let pinned = false;
  27. const pinHandler = async () => {
  28. if (pinned) {
  29. await deleteTagById(localStorage.token, chatId, 'pinned');
  30. } else {
  31. await addTagById(localStorage.token, chatId, 'pinned');
  32. }
  33. dispatch('change');
  34. };
  35. const checkPinned = async () => {
  36. pinned = (
  37. await getTagsById(localStorage.token, chatId).catch(async (error) => {
  38. return [];
  39. })
  40. ).find((tag) => tag.name === 'pinned');
  41. };
  42. $: if (show) {
  43. checkPinned();
  44. }
  45. </script>
  46. <Dropdown
  47. bind:show
  48. on:change={(e) => {
  49. if (e.detail === false) {
  50. onClose();
  51. }
  52. }}
  53. >
  54. <Tooltip content={$i18n.t('More')}>
  55. <slot />
  56. </Tooltip>
  57. <div slot="content">
  58. <DropdownMenu.Content
  59. class="w-full max-w-[180px] rounded-xl px-1 py-1.5 border border-gray-300/30 dark:border-gray-700/50 z-50 bg-white dark:bg-gray-850 dark:text-white shadow"
  60. sideOffset={-2}
  61. side="bottom"
  62. align="start"
  63. transition={flyAndScale}
  64. >
  65. <DropdownMenu.Item
  66. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  67. on:click={() => {
  68. pinHandler();
  69. }}
  70. >
  71. {#if pinned}
  72. <BookmarkSlash strokeWidth="2" />
  73. <div class="flex items-center">{$i18n.t('Unpin')}</div>
  74. {:else}
  75. <Bookmark strokeWidth="2" />
  76. <div class="flex items-center">{$i18n.t('Pin')}</div>
  77. {/if}
  78. </DropdownMenu.Item>
  79. <DropdownMenu.Item
  80. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  81. on:click={() => {
  82. renameHandler();
  83. }}
  84. >
  85. <Pencil strokeWidth="2" />
  86. <div class="flex items-center">{$i18n.t('Rename')}</div>
  87. </DropdownMenu.Item>
  88. <DropdownMenu.Item
  89. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  90. on:click={() => {
  91. cloneChatHandler();
  92. }}
  93. >
  94. <DocumentDuplicate strokeWidth="2" />
  95. <div class="flex items-center">{$i18n.t('Clone')}</div>
  96. </DropdownMenu.Item>
  97. <DropdownMenu.Item
  98. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  99. on:click={() => {
  100. archiveChatHandler();
  101. }}
  102. >
  103. <ArchiveBox strokeWidth="2" />
  104. <div class="flex items-center">{$i18n.t('Archive')}</div>
  105. </DropdownMenu.Item>
  106. <DropdownMenu.Item
  107. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  108. on:click={() => {
  109. shareHandler();
  110. }}
  111. >
  112. <Share />
  113. <div class="flex items-center">{$i18n.t('Share')}</div>
  114. </DropdownMenu.Item>
  115. <DropdownMenu.Item
  116. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  117. on:click={() => {
  118. deleteHandler();
  119. }}
  120. >
  121. <GarbageBin strokeWidth="2" />
  122. <div class="flex items-center">{$i18n.t('Delete')}</div>
  123. </DropdownMenu.Item>
  124. <hr class="border-gray-100 dark:border-gray-800 mt-2.5 mb-1.5" />
  125. <div class="flex p-1">
  126. <Tags
  127. {chatId}
  128. on:close={() => {
  129. show = false;
  130. onClose();
  131. }}
  132. />
  133. </div>
  134. </DropdownMenu.Content>
  135. </div>
  136. </Dropdown>