Documents.svelte 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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="md:px-2 mb-3 text-left w-full absolute bottom-0 left-0 right-0">
  81. <div class="flex w-full px-2">
  82. <div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-xl text-center">
  83. <div class=" text-lg font-semibold mt-2">#</div>
  84. </div>
  85. <div class="max-h-60 flex flex-col w-full rounded-r-xl bg-white">
  86. <div class="m-1 overflow-y-auto p-1 rounded-r-xl space-y-0.5">
  87. {#each filteredItems as doc, docIdx}
  88. <button
  89. class=" px-3 py-1.5 rounded-xl w-full text-left {docIdx === selectedIdx
  90. ? ' bg-gray-100 selected-command-option-button'
  91. : ''}"
  92. type="button"
  93. on:click={() => {
  94. console.log(doc);
  95. confirmSelect(doc);
  96. }}
  97. on:mousemove={() => {
  98. selectedIdx = docIdx;
  99. }}
  100. on:focus={() => {}}
  101. >
  102. {#if doc.type === 'collection'}
  103. <div class=" font-medium text-black line-clamp-1">
  104. {doc?.title ?? `#${doc.name}`}
  105. </div>
  106. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Collection')}</div>
  107. {:else}
  108. <div class=" font-medium text-black line-clamp-1">
  109. #{doc.name} ({doc.filename})
  110. </div>
  111. <div class=" text-xs text-gray-600 line-clamp-1">
  112. {doc.title}
  113. </div>
  114. {/if}
  115. </button>
  116. {/each}
  117. {#if prompt.split(' ')?.at(0)?.substring(1).startsWith('https://www.youtube.com')}
  118. <button
  119. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
  120. type="button"
  121. on:click={() => {
  122. const url = prompt.split(' ')?.at(0)?.substring(1);
  123. if (isValidHttpUrl(url)) {
  124. confirmSelectYoutube(url);
  125. } else {
  126. toast.error(
  127. $i18n.t(
  128. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  129. )
  130. );
  131. }
  132. }}
  133. >
  134. <div class=" font-medium text-black line-clamp-1">
  135. {prompt.split(' ')?.at(0)?.substring(1)}
  136. </div>
  137. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Youtube')}</div>
  138. </button>
  139. {:else if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  140. <button
  141. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
  142. type="button"
  143. on:click={() => {
  144. const url = prompt.split(' ')?.at(0)?.substring(1);
  145. if (isValidHttpUrl(url)) {
  146. confirmSelectWeb(url);
  147. } else {
  148. toast.error(
  149. $i18n.t(
  150. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  151. )
  152. );
  153. }
  154. }}
  155. >
  156. <div class=" font-medium text-black line-clamp-1">
  157. {prompt.split(' ')?.at(0)?.substring(1)}
  158. </div>
  159. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Web')}</div>
  160. </button>
  161. {/if}
  162. </div>
  163. </div>
  164. </div>
  165. </div>
  166. {/if}