Documents.svelte 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 filteredDocs = [];
  11. $: filteredDocs = $documents
  12. .filter((p) => p.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
  13. .sort((a, b) => a.title.localeCompare(b.title));
  14. $: if (prompt) {
  15. selectedIdx = 0;
  16. }
  17. export const selectUp = () => {
  18. selectedIdx = Math.max(0, selectedIdx - 1);
  19. };
  20. export const selectDown = () => {
  21. selectedIdx = Math.min(selectedIdx + 1, filteredDocs.length - 1);
  22. };
  23. const confirmSelect = async (doc) => {
  24. dispatch('select', doc);
  25. prompt = removeFirstHashWord(prompt);
  26. const chatInputElement = document.getElementById('chat-textarea');
  27. await tick();
  28. chatInputElement?.focus();
  29. await tick();
  30. };
  31. const confirmSelectWeb = async (url) => {
  32. dispatch('url', url);
  33. prompt = removeFirstHashWord(prompt);
  34. const chatInputElement = document.getElementById('chat-textarea');
  35. await tick();
  36. chatInputElement?.focus();
  37. await tick();
  38. };
  39. </script>
  40. {#if filteredDocs.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  41. <div class="md:px-2 mb-3 text-left w-full">
  42. <div class="flex w-full rounded-lg border border-gray-100 dark:border-gray-700">
  43. <div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-lg text-center">
  44. <div class=" text-lg font-semibold mt-2">#</div>
  45. </div>
  46. <div class="max-h-60 flex flex-col w-full rounded-r-lg">
  47. <div class=" overflow-y-auto bg-white p-2 rounded-tr-lg space-y-0.5">
  48. {#each filteredDocs as doc, docIdx}
  49. <button
  50. class=" px-3 py-1.5 rounded-lg w-full text-left {docIdx === selectedIdx
  51. ? ' bg-gray-100 selected-command-option-button'
  52. : ''}"
  53. type="button"
  54. on:click={() => {
  55. console.log(doc);
  56. confirmSelect(doc);
  57. }}
  58. on:mousemove={() => {
  59. selectedIdx = docIdx;
  60. }}
  61. on:focus={() => {}}
  62. >
  63. <div class=" font-medium text-black line-clamp-1">
  64. #{doc.name} ({doc.filename})
  65. </div>
  66. <div class=" text-xs text-gray-600 line-clamp-1">
  67. {doc.title}
  68. </div>
  69. </button>
  70. {/each}
  71. {#if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
  72. <button
  73. class="px-3 py-1.5 rounded-lg w-full text-left bg-gray-100 selected-command-option-button"
  74. type="button"
  75. on:click={() => {
  76. const url = prompt.split(' ')?.at(0)?.substring(1);
  77. if (isValidHttpUrl(url)) {
  78. confirmSelectWeb(url);
  79. } else {
  80. toast.error(
  81. 'Oops! Looks like the URL is invalid. Please double-check and try again.'
  82. );
  83. }
  84. }}
  85. >
  86. <div class=" font-medium text-black line-clamp-1">
  87. {prompt.split(' ')?.at(0)?.substring(1)}
  88. </div>
  89. <div class=" text-xs text-gray-600 line-clamp-1">Web</div>
  90. </button>
  91. {/if}
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. {/if}