Documents.svelte 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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) => collection.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
  39. .sort((a, b) => a.name.localeCompare(b.name));
  40. $: filteredDocs = $documents
  41. .filter((doc) => doc.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
  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. export const selectUp = () => {
  49. selectedIdx = Math.max(0, selectedIdx - 1);
  50. };
  51. export const selectDown = () => {
  52. selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
  53. };
  54. const confirmSelect = async (doc) => {
  55. dispatch('select', doc);
  56. prompt = removeFirstHashWord(prompt);
  57. const chatInputElement = document.getElementById('chat-textarea');
  58. await tick();
  59. chatInputElement?.focus();
  60. await tick();
  61. };
  62. const confirmSelectWeb = async (url) => {
  63. dispatch('url', url);
  64. prompt = removeFirstHashWord(prompt);
  65. const chatInputElement = document.getElementById('chat-textarea');
  66. await tick();
  67. chatInputElement?.focus();
  68. await tick();
  69. };
  70. const confirmSelectYoutube = async (url) => {
  71. dispatch('youtube', url);
  72. prompt = removeFirstHashWord(prompt);
  73. const chatInputElement = document.getElementById('chat-textarea');
  74. await tick();
  75. chatInputElement?.focus();
  76. await tick();
  77. };
  78. </script>
  79. {#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  80. <div class="pl-1 pr-12 mb-3 text-left w-full absolute bottom-0 left-0 right-0 z-10">
  81. <div class="flex w-full dark:border dark:border-gray-850 rounded-lg">
  82. <div class=" bg-gray-50 dark:bg-gray-850 w-10 rounded-l-lg text-center">
  83. <div class=" text-lg font-semibold mt-2">#</div>
  84. </div>
  85. <div
  86. class="max-h-60 flex flex-col w-full rounded-r-xl bg-white dark:bg-gray-900 dark:text-gray-100"
  87. >
  88. <div class="m-1 overflow-y-auto p-1 rounded-r-xl space-y-0.5 scrollbar-hidden">
  89. {#each filteredItems as doc, docIdx}
  90. <button
  91. class=" px-3 py-1.5 rounded-xl w-full text-left {docIdx === selectedIdx
  92. ? ' bg-gray-50 dark:bg-gray-850 dark:text-gray-100 selected-command-option-button'
  93. : ''}"
  94. type="button"
  95. on:click={() => {
  96. console.log(doc);
  97. confirmSelect(doc);
  98. }}
  99. on:mousemove={() => {
  100. selectedIdx = docIdx;
  101. }}
  102. on:focus={() => {}}
  103. >
  104. {#if doc.type === 'collection'}
  105. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  106. {doc?.title ?? `#${doc.name}`}
  107. </div>
  108. <div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
  109. {$i18n.t('Collection')}
  110. </div>
  111. {:else}
  112. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  113. #{doc.name} ({doc.filename})
  114. </div>
  115. <div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
  116. {doc.title}
  117. </div>
  118. {/if}
  119. </button>
  120. {/each}
  121. {#if prompt
  122. .split(' ')
  123. .some((s) => s.substring(1).startsWith('https://www.youtube.com') || s
  124. .substring(1)
  125. .startsWith('https://youtu.be'))}
  126. <button
  127. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
  128. type="button"
  129. on:click={() => {
  130. const url = prompt.split(' ')?.at(0)?.substring(1);
  131. if (isValidHttpUrl(url)) {
  132. confirmSelectYoutube(url);
  133. } else {
  134. toast.error(
  135. $i18n.t(
  136. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  137. )
  138. );
  139. }
  140. }}
  141. >
  142. <div class=" font-medium text-black line-clamp-1">
  143. {prompt.split(' ')?.at(0)?.substring(1)}
  144. </div>
  145. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Youtube')}</div>
  146. </button>
  147. {:else if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  148. <button
  149. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
  150. type="button"
  151. on:click={() => {
  152. const url = prompt.split(' ')?.at(0)?.substring(1);
  153. if (isValidHttpUrl(url)) {
  154. confirmSelectWeb(url);
  155. } else {
  156. toast.error(
  157. $i18n.t(
  158. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  159. )
  160. );
  161. }
  162. }}
  163. >
  164. <div class=" font-medium text-black line-clamp-1">
  165. {prompt.split(' ')?.at(0)?.substring(1)}
  166. </div>
  167. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Web')}</div>
  168. </button>
  169. {/if}
  170. </div>
  171. </div>
  172. </div>
  173. </div>
  174. {/if}