Documents.svelte 4.23 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
<script lang="ts">
	import { createEventDispatcher } from 'svelte';

	import { documents } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
5
	import { removeFirstHashWord, isValidHttpUrl } from '$lib/utils';
Timothy J. Baek's avatar
Timothy J. Baek committed
6
	import { tick } from 'svelte';
7
	import toast from 'svelte-french-toast';
Timothy J. Baek's avatar
Timothy J. Baek committed
8
9
10
11
12

	export let prompt = '';

	const dispatch = createEventDispatcher();
	let selectedIdx = 0;
13
14

	let filteredItems = [];
Timothy J. Baek's avatar
Timothy J. Baek committed
15
	let filteredDocs = [];
16
17
18

	let collections = [];

Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
	$: collections = [
		{
			name: 'All Documents',
22
			type: 'collection',
Timothy J. Baek's avatar
Timothy J. Baek committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
			title: 'All Documents',
			collection_names: $documents.map((doc) => doc.collection_name)
		},
		...$documents
			.reduce((a, e, i, arr) => {
				return [...new Set([...a, ...(e?.content?.tags ?? []).map((tag) => tag.name)])];
			}, [])
			.map((tag) => ({
				name: tag,
				type: 'collection',
				collection_names: $documents
					.filter((doc) => (doc?.content?.tags ?? []).map((tag) => tag.name).includes(tag))
					.map((doc) => doc.collection_name)
			}))
	];
38
39

	$: filteredCollections = collections
Timothy J. Baek's avatar
Timothy J. Baek committed
40
		.filter((collection) => collection.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
41
		.sort((a, b) => a.name.localeCompare(b.name));
Timothy J. Baek's avatar
Timothy J. Baek committed
42
43

	$: filteredDocs = $documents
Timothy J. Baek's avatar
Timothy J. Baek committed
44
		.filter((doc) => doc.name.includes(prompt.split(' ')?.at(0)?.substring(1) ?? ''))
Timothy J. Baek's avatar
Timothy J. Baek committed
45
46
		.sort((a, b) => a.title.localeCompare(b.title));

47
48
	$: filteredItems = [...filteredCollections, ...filteredDocs];

Timothy J. Baek's avatar
Timothy J. Baek committed
49
50
	$: if (prompt) {
		selectedIdx = 0;
Timothy J. Baek's avatar
Timothy J. Baek committed
51
52

		console.log(filteredCollections);
Timothy J. Baek's avatar
Timothy J. Baek committed
53
54
55
56
57
58
59
	}

	export const selectUp = () => {
		selectedIdx = Math.max(0, selectedIdx - 1);
	};

	export const selectDown = () => {
60
		selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);
Timothy J. Baek's avatar
Timothy J. Baek committed
61
62
63
64
65
66
67
68
69
70
71
72
	};

	const confirmSelect = async (doc) => {
		dispatch('select', doc);

		prompt = removeFirstHashWord(prompt);
		const chatInputElement = document.getElementById('chat-textarea');

		await tick();
		chatInputElement?.focus();
		await tick();
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
73
74
75
76
77
78
79
80
81
82
83

	const confirmSelectWeb = async (url) => {
		dispatch('url', url);

		prompt = removeFirstHashWord(prompt);
		const chatInputElement = document.getElementById('chat-textarea');

		await tick();
		chatInputElement?.focus();
		await tick();
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85
</script>

Timothy J. Baek's avatar
Timothy J. Baek committed
86
{#if filteredItems.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
Timothy J. Baek's avatar
Timothy J. Baek committed
87
88
89
90
91
92
93
94
	<div class="md:px-2 mb-3 text-left w-full">
		<div class="flex w-full rounded-lg border border-gray-100 dark:border-gray-700">
			<div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-lg text-center">
				<div class=" text-lg font-semibold mt-2">#</div>
			</div>

			<div class="max-h-60 flex flex-col w-full rounded-r-lg">
				<div class=" overflow-y-auto bg-white p-2 rounded-tr-lg space-y-0.5">
95
					{#each filteredItems as doc, docIdx}
Timothy J. Baek's avatar
Timothy J. Baek committed
96
97
98
99
100
101
						<button
							class=" px-3 py-1.5 rounded-lg w-full text-left {docIdx === selectedIdx
								? ' bg-gray-100 selected-command-option-button'
								: ''}"
							type="button"
							on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
102
								console.log(doc);
103

Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
106
107
108
109
110
								confirmSelect(doc);
							}}
							on:mousemove={() => {
								selectedIdx = docIdx;
							}}
							on:focus={() => {}}
						>
111
112
							{#if doc.type === 'collection'}
								<div class=" font-medium text-black line-clamp-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
113
									{doc?.title ?? `#${doc.name}`}
114
115
116
117
118
119
120
121
122
123
124
125
								</div>

								<div class=" text-xs text-gray-600 line-clamp-1">Collection</div>
							{:else}
								<div class=" font-medium text-black line-clamp-1">
									#{doc.name} ({doc.filename})
								</div>

								<div class=" text-xs text-gray-600 line-clamp-1">
									{doc.title}
								</div>
							{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
126
127
						</button>
					{/each}
Timothy J. Baek's avatar
Timothy J. Baek committed
128
129
130
131
132
133
134
135
136

					{#if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
						<button
							class="px-3 py-1.5 rounded-lg w-full text-left bg-gray-100 selected-command-option-button"
							type="button"
							on:click={() => {
								const url = prompt.split(' ')?.at(0)?.substring(1);
								if (isValidHttpUrl(url)) {
									confirmSelectWeb(url);
137
138
139
140
								} else {
									toast.error(
										'Oops! Looks like the URL is invalid. Please double-check and try again.'
									);
Timothy J. Baek's avatar
Timothy J. Baek committed
141
142
143
144
145
146
147
148
149
150
								}
							}}
						>
							<div class=" font-medium text-black line-clamp-1">
								{prompt.split(' ')?.at(0)?.substring(1)}
							</div>

							<div class=" text-xs text-gray-600 line-clamp-1">Web</div>
						</button>
					{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
151
152
153
154
155
				</div>
			</div>
		</div>
	</div>
{/if}