Documents.svelte 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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">
  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 dark:bg-gray-850 dark:text-gray-100 ">
  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 dark:bg-gray-600 dark:text-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 dark:text-gray-100 line-clamp-1">
  104. {doc?.title ?? `#${doc.name}`}
  105. </div>
  106. <div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">{$i18n.t('Collection')}</div>
  107. {:else}
  108. <div class=" font-medium text-black dark:text-gray-100 line-clamp-1">
  109. #{doc.name} ({doc.filename})
  110. </div>
  111. <div class=" text-xs text-gray-600 dark:text-gray-100 line-clamp-1">
  112. {doc.title}
  113. </div>
  114. {/if}
  115. </button>
  116. {/each}
  117. {#if prompt
  118. .split(' ')
  119. .some((s) => s.substring(1).startsWith('https://www.youtube.com') || s
  120. .substring(1)
  121. .startsWith('https://youtu.be'))}
  122. <button
  123. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
  124. type="button"
  125. on:click={() => {
  126. const url = prompt.split(' ')?.at(0)?.substring(1);
  127. if (isValidHttpUrl(url)) {
  128. confirmSelectYoutube(url);
  129. } else {
  130. toast.error(
  131. $i18n.t(
  132. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  133. )
  134. );
  135. }
  136. }}
  137. >
  138. <div class=" font-medium text-black line-clamp-1">
  139. {prompt.split(' ')?.at(0)?.substring(1)}
  140. </div>
  141. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Youtube')}</div>
  142. </button>
  143. {:else if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  144. <button
  145. class="px-3 py-1.5 rounded-xl w-full text-left bg-gray-100 selected-command-option-button"
  146. type="button"
  147. on:click={() => {
  148. const url = prompt.split(' ')?.at(0)?.substring(1);
  149. if (isValidHttpUrl(url)) {
  150. confirmSelectWeb(url);
  151. } else {
  152. toast.error(
  153. $i18n.t(
  154. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  155. )
  156. );
  157. }
  158. }}
  159. >
  160. <div class=" font-medium text-black line-clamp-1">
  161. {prompt.split(' ')?.at(0)?.substring(1)}
  162. </div>
  163. <div class=" text-xs text-gray-600 line-clamp-1">{$i18n.t('Web')}</div>
  164. </button>
  165. {/if}
  166. </div>
  167. </div>
  168. </div>
  169. </div>
  170. {/if}