Selector.svelte 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <script lang="ts">
  2. import { DropdownMenu } from 'bits-ui';
  3. import { documents } from '$lib/stores';
  4. import { flyAndScale } from '$lib/utils/transitions';
  5. import Dropdown from '$lib/components/common/Dropdown.svelte';
  6. import { onMount, getContext } from 'svelte';
  7. const i18n = getContext('i18n');
  8. export let onClose: Function = () => {};
  9. export let knowledge = [];
  10. let items = [];
  11. onMount(() => {
  12. let collections = [
  13. ...($documents.length > 0
  14. ? [
  15. {
  16. name: 'All Documents',
  17. type: 'collection',
  18. title: $i18n.t('All Documents'),
  19. collection_names: $documents.map((doc) => doc.collection_name)
  20. }
  21. ]
  22. : []),
  23. ...$documents
  24. .reduce((a, e, i, arr) => {
  25. return [...new Set([...a, ...(e?.content?.tags ?? []).map((tag) => tag.name)])];
  26. }, [])
  27. .map((tag) => ({
  28. name: tag,
  29. type: 'collection',
  30. collection_names: $documents
  31. .filter((doc) => (doc?.content?.tags ?? []).map((tag) => tag.name).includes(tag))
  32. .map((doc) => doc.collection_name)
  33. }))
  34. ];
  35. items = [...collections, ...$documents];
  36. });
  37. </script>
  38. <Dropdown
  39. on:change={(e) => {
  40. if (e.detail === false) {
  41. onClose();
  42. }
  43. }}
  44. >
  45. <slot />
  46. <div slot="content">
  47. <DropdownMenu.Content
  48. class="w-full max-w-[300px] rounded-lg 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-lg"
  49. sideOffset={8}
  50. side="bottom"
  51. align="start"
  52. transition={flyAndScale}
  53. >
  54. <div class="max-h-[10rem] overflow-y-scroll">
  55. {#if items.length === 0}
  56. <div class="text-center text-sm text-gray-500 dark:text-gray-400">
  57. {$i18n.t('No documents found')}
  58. </div>
  59. {:else}
  60. {#each items as item}
  61. <DropdownMenu.Item
  62. class="flex gap-2.5 items-center px-3 py-2 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  63. on:click={() => {
  64. if (!knowledge.find((k) => k.name === item.name)) {
  65. knowledge = [
  66. ...knowledge,
  67. {
  68. ...item,
  69. type: item?.type ?? 'doc'
  70. }
  71. ];
  72. }
  73. }}
  74. >
  75. <div class="flex self-start">
  76. {#if (item?.type ?? 'doc') === 'doc'}
  77. <svg
  78. xmlns="http://www.w3.org/2000/svg"
  79. viewBox="0 0 24 24"
  80. fill="currentColor"
  81. class="w-4"
  82. >
  83. <path
  84. fill-rule="evenodd"
  85. d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0 0 16.5 9h-1.875a1.875 1.875 0 0 1-1.875-1.875V5.25A3.75 3.75 0 0 0 9 1.5H5.625ZM7.5 15a.75.75 0 0 1 .75-.75h7.5a.75.75 0 0 1 0 1.5h-7.5A.75.75 0 0 1 7.5 15Zm.75 2.25a.75.75 0 0 0 0 1.5H12a.75.75 0 0 0 0-1.5H8.25Z"
  86. clip-rule="evenodd"
  87. />
  88. <path
  89. d="M12.971 1.816A5.23 5.23 0 0 1 14.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 0 1 3.434 1.279 9.768 9.768 0 0 0-6.963-6.963Z"
  90. />
  91. </svg>
  92. {:else if item.type === 'collection'}
  93. <svg
  94. xmlns="http://www.w3.org/2000/svg"
  95. viewBox="0 0 24 24"
  96. fill="currentColor"
  97. class="size-4"
  98. >
  99. <path
  100. d="M7.5 3.375c0-1.036.84-1.875 1.875-1.875h.375a3.75 3.75 0 0 1 3.75 3.75v1.875C13.5 8.161 14.34 9 15.375 9h1.875A3.75 3.75 0 0 1 21 12.75v3.375C21 17.16 20.16 18 19.125 18h-9.75A1.875 1.875 0 0 1 7.5 16.125V3.375Z"
  101. />
  102. <path
  103. d="M15 5.25a5.23 5.23 0 0 0-1.279-3.434 9.768 9.768 0 0 1 6.963 6.963A5.23 5.23 0 0 0 17.25 7.5h-1.875A.375.375 0 0 1 15 7.125V5.25ZM4.875 6H6v10.125A3.375 3.375 0 0 0 9.375 19.5H16.5v1.125c0 1.035-.84 1.875-1.875 1.875h-9.75A1.875 1.875 0 0 1 3 20.625V7.875C3 6.839 3.84 6 4.875 6Z"
  104. />
  105. </svg>
  106. {/if}
  107. </div>
  108. <div class="flex items-center">
  109. <div class="flex flex-col">
  110. <div
  111. class=" w-fit text-xs font-bold px-1 rounded uppercase line-clamp-1 bg-gray-500/20 text-gray-700 dark:text-gray-200"
  112. >
  113. {item?.type ?? 'Document'}
  114. </div>
  115. <div class="line-clamp-1 font-medium pr-0.5">
  116. {item.name}
  117. </div>
  118. </div>
  119. </div>
  120. </DropdownMenu.Item>
  121. {/each}
  122. {/if}
  123. </div>
  124. </DropdownMenu.Content>
  125. </div>
  126. </Dropdown>