File size: 2,725 Bytes
bc27e65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<script lang="ts">

	import Fuse from 'fuse.js';



	import { createEventDispatcher, onMount } from 'svelte';

	import { tick, getContext } from 'svelte';



	import { models } from '$lib/stores';



	const i18n = getContext('i18n');



	const dispatch = createEventDispatcher();



	export let command = '';



	let selectedIdx = 0;

	let filteredItems = [];



	let fuse = new Fuse(

		$models

			.filter((model) => !model?.info?.meta?.hidden)

			.map((model) => {

				const _item = {

					...model,

					modelName: model?.name,

					tags: model?.info?.meta?.tags?.map((tag) => tag.name).join(' '),

					desc: model?.info?.meta?.description

				};

				return _item;

			}),

		{

			keys: ['value', 'tags', 'modelName'],

			threshold: 0.3

		}

	);



	$: filteredItems = command.slice(1)

		? fuse.search(command).map((e) => {

				return e.item;

			})

		: $models.filter((model) => !model?.info?.meta?.hidden);



	$: if (command) {

		selectedIdx = 0;

	}



	export const selectUp = () => {

		selectedIdx = Math.max(0, selectedIdx - 1);

	};



	export const selectDown = () => {

		selectedIdx = Math.min(selectedIdx + 1, filteredItems.length - 1);

	};



	const confirmSelect = async (model) => {

		command = '';

		dispatch('select', model);

	};



	onMount(async () => {

		await tick();

		const chatInputElement = document.getElementById('chat-input');

		await tick();

		chatInputElement?.focus();

		await tick();

	});

</script>

{#if filteredItems.length > 0}
	<div

		id="commands-container"

		class="px-2 mb-2 text-left w-full absolute bottom-0 left-0 right-0 z-10"

	>
		<div class="flex w-full rounded-xl border border-gray-100 dark:border-gray-850">
			<div

				class="max-h-60 flex flex-col w-full rounded-xl bg-white dark:bg-gray-900 dark:text-gray-100"

			>
				<div class="m-1 overflow-y-auto p-1 rounded-r-lg space-y-0.5 scrollbar-hidden">
					{#each filteredItems as model, modelIdx}
						<button

							class="px-3 py-1.5 rounded-xl w-full text-left {modelIdx === selectedIdx

								? 'bg-gray-50 dark:bg-gray-850 selected-command-option-button'

								: ''}"

							type="button"

							on:click={() => {
								confirmSelect(model);
							}}
							on:mousemove={() => {
								selectedIdx = modelIdx;
							}}
							on:focus={() => {}}
						>
							<div class="flex font-medium text-black dark:text-gray-100 line-clamp-1">
								<img

									src={model?.info?.meta?.profile_image_url ?? '/static/favicon.png'}

									alt={model?.name ?? model.id}

									class="rounded-full size-6 items-center mr-2"

								/>
								{model.name}
							</div>
						</button>
					{/each}
				</div>
			</div>
		</div>
	</div>
{/if}