Documents.svelte 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <script lang="ts">
  2. import { createEventDispatcher } from 'svelte';
  3. import { documents } from '$lib/stores';
  4. import { removeFirstHashWord, isValidHttpUrl } from '$lib/utils';
  5. import { tick, getContext } from 'svelte';
  6. import { toast } from 'svelte-sonner';
  7. const i18n = getContext('i18n');
  8. export let prompt = '';
  9. const dispatch = createEventDispatcher();
  10. let selectedIdx = 0;
  11. let filteredItems = [];
  12. let filteredDocs = [];
  13. let collections = [];
  14. $: collections = [
  15. ...($documents.length > 0
  16. ? [
  17. {
  18. name: 'All Documents',
  19. type: 'collection',
  20. title: $i18n.t('All Documents'),
  21. collection_names: $documents.map((doc) => doc.collection_name)
  22. }
  23. ]
  24. : []),
  25. ...$documents
  26. .reduce((a, e, i, arr) => {
  27. return [...new Set([...a, ...(e?.content?.tags ?? []).map((tag) => tag.name)])];
  28. }, [])
  29. .map((tag) => ({
  30. name: tag,
  31. type: 'collection',
  32. collection_names: $documents
  33. .filter((doc) => (doc?.content?.tags ?? []).map((tag) => tag.name).includes(tag))
  34. .map((doc) => doc.collection_name)
  35. }))
  36. ];
  37. $: filteredCollections = collections
  38. .filter((collection) => findByName(collection, prompt))
  39. .sort((a, b) => a.name.localeCompare(b.name));
  40. $: filteredDocs = $documents
  41. .filter((doc) => findByName(doc, prompt))
  42. .sort((a, b) => a.title.localeCompare(b.title));
  43. $: filteredItems = [...filteredCollections, ...filteredDocs];
  44. $: if (prompt) {
  45. selectedIdx = 0;
  46. console.log(filteredCollections);
  47. }
  48. type ObjectWithName = {
  49. name: string;
  50. };
  51. const findByName = (obj: ObjectWithName, prompt: string) => {
  52. const name = obj.name.toLowerCase();
  53. return name.includes(prompt.toLowerCase().split(' ')?.at(0)?.substring(1) ?? '');
  54. };
  55. export const selectUp = () => {
  56. selectedIdx = Math.max(0, selectedIdx - 1);
  57. };
  58. export const selectDown = () => {
  59. selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
  60. };
  61. const confirmSelect = async (doc) => {
  62. dispatch('select', doc);
  63. prompt = removeFirstHashWord(prompt);
  64. const chatInputElement = document.getElementById('chat-textarea');
  65. await tick();
  66. chatInputElement?.focus();
  67. await tick();
  68. };
  69. const confirmSelectWeb = async (url) => {
  70. dispatch('url', url);
  71. prompt = removeFirstHashWord(prompt);
  72. const chatInputElement = document.getElementById('chat-textarea');
  73. await tick();
  74. chatInputElement?.focus();
  75. await tick();
  76. };
  77. const confirmSelectYoutube = async (url) => {
  78. dispatch('youtube', url);
  79. prompt = removeFirstHashWord(prompt);
  80. const chatInputElement = document.getElementById('chat-textarea');
  81. await tick();
  82. chatInputElement?.focus();
  83. await tick();
  84. };
  85. </script>
  86. {#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  87. <div class="pl-1 pr-12 mb-3 text-left w-full absolute bottom-0 left-0 right-0 z-10">
  88. <div class="flex w-full dark:border dark:border-gray-850 rounded-lg">
  89. <div class=" bg-gray-50 dark:bg-gray-850 w-10 rounded-l-lg text-center">
  90. <div class=" text-lg font-semibold mt-2">#</div>
  91. </div>
  92. <div
  93. class="max-h-60 flex flex-col w-full rounded-r-xl bg-white dark:bg-gray-900 dark:text-gray-100"
  94. >
  95. <div class="m-1 overflow-y-auto p-1 rounded-r-xl space-y-0.5 scrollbar-hidden">
  96. {#each filteredItems as doc, docIdx}
  97. <button
  98. class=" px-3 py-1.5 rounded-xl w-full text-left {docIdx === selectedIdx
  99. ? ' bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button'
  100. : ''}"
  101. type="button"
  102. on:click={() => {
  103. console.log(doc);
  104. confirmSelect(doc);
  105. }}
  106. on:mousemove={() => {
  107. selectedIdx = docIdx;
  108. }}
  109. on:focus={() => {}}
  110. >
  111. {#if doc.type === 'collection'}
  112. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  113. {doc?.title ?? `#${doc.name}`}
  114. </div>
  115. <div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
  116. {$i18n.t('Collection')}
  117. </div>
  118. {:else}
  119. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  120. #{doc.name} ({doc.filename})
  121. </div>
  122. <div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
  123. {doc.title}
  124. </div>
  125. {/if}
  126. </button>
  127. {/each}
  128. {#if prompt
  129. .split(' ')
  130. .some((s) => s.substring(1).startsWith('https://www.youtube.com') || s
  131. .substring(1)
  132. .startsWith('https://youtu.be'))}
  133. <button
  134. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button"
  135. type="button"
  136. on:click={() => {
  137. const url = prompt.split(' ')?.at(0)?.substring(1);
  138. if (isValidHttpUrl(url)) {
  139. confirmSelectYoutube(url);
  140. } else {
  141. toast.error(
  142. $i18n.t(
  143. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  144. )
  145. );
  146. }
  147. }}
  148. >
  149. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  150. {prompt.split(' ')?.at(0)?.substring(1)}
  151. </div>
  152. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Youtube')}</div>
  153. </button>
  154. {:else if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  155. <button
  156. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button"
  157. type="button"
  158. on:click={() => {
  159. const url = prompt.split(' ')?.at(0)?.substring(1);
  160. if (isValidHttpUrl(url)) {
  161. confirmSelectWeb(url);
  162. } else {
  163. toast.error(
  164. $i18n.t(
  165. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  166. )
  167. );
  168. }
  169. }}
  170. >
  171. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  172. {prompt.split(' ')?.at(0)?.substring(1)}
  173. </div>
  174. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Web')}</div>
  175. </button>
  176. {/if}
  177. </div>
  178. </div>
  179. </div>
  180. </div>
  181. {/if}