Documents.svelte 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 } from 'svelte';
  6. import toast from 'svelte-french-toast';
  7. export let prompt = '';
  8. const dispatch = createEventDispatcher();
  9. let selectedIdx = 0;
  10. let filteredItems = [];
  11. let filteredDocs = [];
  12. let collections = [];
  13. $: collections = [
  14. ...($documents.length > 0
  15. ? [
  16. {
  17. name: 'All Documents',
  18. type: 'collection',
  19. title: 'All Documents',
  20. collection_names: $documents.map((doc) => doc.collection_name)
  21. }
  22. ]
  23. : []),
  24. ...$documents
  25. .reduce((a, e, i, arr) => {
  26. return [...new Set([...a, ...(e?.content?.tags ?? []).map((tag) => tag.name)])];
  27. }, [])
  28. .map((tag) => ({
  29. name: tag,
  30. type: 'collection',
  31. collection_names: $documents
  32. .filter((doc) => (doc?.content?.tags ?? []).map((tag) => tag.name).includes(tag))
  33. .map((doc) => doc.collection_name)
  34. }))
  35. ];
  36. $: filteredCollections = collections
  37. .filter((collection) => collection.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
  38. .sort((a, b) => a.name.localeCompare(b.name));
  39. $: filteredDocs = $documents
  40. .filter((doc) => doc.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
  41. .sort((a, b) => a.title.localeCompare(b.title));
  42. $: filteredItems = [...filteredCollections, ...filteredDocs];
  43. $: if (prompt) {
  44. selectedIdx = 0;
  45. console.log(filteredCollections);
  46. }
  47. export const selectUp = () => {
  48. selectedIdx = Math.max(0, selectedIdx - 1);
  49. };
  50. export const selectDown = () => {
  51. selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
  52. };
  53. const confirmSelect = async (doc) => {
  54. dispatch('select', doc);
  55. prompt = removeFirstHashWord(prompt);
  56. const chatInputElement = document.getElementById('chat-textarea');
  57. await tick();
  58. chatInputElement?.focus();
  59. await tick();
  60. };
  61. const confirmSelectWeb = async (url) => {
  62. dispatch('url', url);
  63. prompt = removeFirstHashWord(prompt);
  64. const chatInputElement = document.getElementById('chat-textarea');
  65. await tick();
  66. chatInputElement?.focus();
  67. await tick();
  68. };
  69. </script>
  70. {#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  71. <div class="md:px-2 mb-3 text-left w-full absolute bottom-0 left-0 right-0">
  72. <div class="flex w-full rounded-lg border border-gray-100 dark:border-gray-700">
  73. <div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-lg text-center">
  74. <div class=" text-lg font-semibold mt-2">#</div>
  75. </div>
  76. <div class="max-h-60 flex flex-col w-full rounded-r-lg">
  77. <div class=" overflow-y-auto bg-white p-2 rounded-tr-lg space-y-0.5">
  78. {#each filteredItems as doc, docIdx}
  79. <button
  80. class=" px-3 py-1.5 rounded-lg w-full text-left {docIdx === selectedIdx
  81. ? ' bg-gray-100 selected-command-option-button'
  82. : ''}"
  83. type="button"
  84. on:click={() => {
  85. console.log(doc);
  86. confirmSelect(doc);
  87. }}
  88. on:mousemove={() => {
  89. selectedIdx = docIdx;
  90. }}
  91. on:focus={() => {}}
  92. >
  93. {#if doc.type === 'collection'}
  94. <div class=" font-medium text-black line-clamp-1">
  95. {doc?.title ?? `#${doc.name}`}
  96. </div>
  97. <div class=" text-xs text-gray-600 line-clamp-1">Collection</div>
  98. {:else}
  99. <div class=" font-medium text-black line-clamp-1">
  100. #{doc.name} ({doc.filename})
  101. </div>
  102. <div class=" text-xs text-gray-600 line-clamp-1">
  103. {doc.title}
  104. </div>
  105. {/if}
  106. </button>
  107. {/each}
  108. {#if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  109. <button
  110. class="px-3 py-1.5 rounded-lg w-full text-left bg-gray-100 selected-command-option-button"
  111. type="button"
  112. on:click={() => {
  113. const url = prompt.split(' ')?.at(0)?.substring(1);
  114. if (isValidHttpUrl(url)) {
  115. confirmSelectWeb(url);
  116. } else {
  117. toast.error(
  118. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  119. );
  120. }
  121. }}
  122. >
  123. <div class=" font-medium text-black line-clamp-1">
  124. {prompt.split(' ')?.at(0)?.substring(1)}
  125. </div>
  126. <div class=" text-xs text-gray-600 line-clamp-1">Web</div>
  127. </button>
  128. {/if}
  129. </div>
  130. </div>
  131. </div>
  132. </div>
  133. {/if}