ChatMenu.svelte 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 {
  17. addTagById,
  18. deleteTagById,
  19. getChatPinnedStatusById,
  20. getTagsById,
  21. toggleChatPinnedStatusById
  22. } from '$lib/apis/chats';
  23. const i18n = getContext('i18n');
  24. export let shareHandler: Function;
  25. export let cloneChatHandler: Function;
  26. export let archiveChatHandler: Function;
  27. export let renameHandler: Function;
  28. export let deleteHandler: Function;
  29. export let onClose: Function;
  30. export let chatId = '';
  31. let show = false;
  32. let pinned = false;
  33. const pinHandler = async () => {
  34. await toggleChatPinnedStatusById(localStorage.token, chatId);
  35. dispatch('change');
  36. };
  37. const checkPinned = async () => {
  38. pinned = await getChatPinnedStatusById(localStorage.token, chatId);
  39. };
  40. $: if (show) {
  41. checkPinned();
  42. }
  43. </script>
  44. <Dropdown
  45. bind:show
  46. on:change={(e) => {
  47. if (e.detail === false) {
  48. onClose();
  49. }
  50. }}
  51. >
  52. <Tooltip content={$i18n.t('More')}>
  53. <slot />
  54. </Tooltip>
  55. <div slot="content">
  56. <DropdownMenu.Content
  57. 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"
  58. sideOffset={-2}
  59. side="bottom"
  60. align="start"
  61. transition={flyAndScale}
  62. >
  63. <DropdownMenu.Item
  64. 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"
  65. on:click={() => {
  66. pinHandler();
  67. }}
  68. >
  69. {#if pinned}
  70. <BookmarkSlash strokeWidth="2" />
  71. <div class="flex items-center">{$i18n.t('Unpin')}</div>
  72. {:else}
  73. <Bookmark strokeWidth="2" />
  74. <div class="flex items-center">{$i18n.t('Pin')}</div>
  75. {/if}
  76. </DropdownMenu.Item>
  77. <DropdownMenu.Item
  78. 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"
  79. on:click={() => {
  80. renameHandler();
  81. }}
  82. >
  83. <Pencil strokeWidth="2" />
  84. <div class="flex items-center">{$i18n.t('Rename')}</div>
  85. </DropdownMenu.Item>
  86. <DropdownMenu.Item
  87. 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"
  88. on:click={() => {
  89. cloneChatHandler();
  90. }}
  91. >
  92. <DocumentDuplicate strokeWidth="2" />
  93. <div class="flex items-center">{$i18n.t('Clone')}</div>
  94. </DropdownMenu.Item>
  95. <DropdownMenu.Item
  96. 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"
  97. on:click={() => {
  98. archiveChatHandler();
  99. }}
  100. >
  101. <ArchiveBox strokeWidth="2" />
  102. <div class="flex items-center">{$i18n.t('Archive')}</div>
  103. </DropdownMenu.Item>
  104. <DropdownMenu.Item
  105. 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"
  106. on:click={() => {
  107. shareHandler();
  108. }}
  109. >
  110. <Share />
  111. <div class="flex items-center">{$i18n.t('Share')}</div>
  112. </DropdownMenu.Item>
  113. <DropdownMenu.Item
  114. 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"
  115. on:click={() => {
  116. deleteHandler();
  117. }}
  118. >
  119. <GarbageBin strokeWidth="2" />
  120. <div class="flex items-center">{$i18n.t('Delete')}</div>
  121. </DropdownMenu.Item>
  122. <hr class="border-gray-100 dark:border-gray-800 mt-2.5 mb-1.5" />
  123. <div class="flex p-1">
  124. <Tags
  125. {chatId}
  126. on:close={() => {
  127. show = false;
  128. onClose();
  129. }}
  130. />
  131. </div>
  132. </DropdownMenu.Content>
  133. </div>
  134. </Dropdown>