~james/core

core/web/src/lib/components/shell/TopbarSearch.svelte -rw-r--r-- 6.5 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<script lang="ts">
	import { goto } from "$app/navigation";
	import { resolve } from "$app/paths";
	import ArrowRight from "$icon/arrow-right";
	import Search from "$icon/search";
	import SearchX from "$icon/search-x";
	import { getAuth } from "#lib/auth.svelte.ts";
	import { createBobbinClient } from "#lib/api/client.ts";
	import { repoKey, type RepoCardData } from "#lib/components/profile/types.ts";
	import RepoCard from "#lib/components/repo/RepoCard.svelte";
	import { searchRepos, type RepoSearchOutcome } from "#lib/components/search/repoSearch.ts";
	import {
		QUICK_SEARCH_DELAY,
		QUICK_SEARCH_LIMIT,
		moveActive,
		shortcutLabel
	} from "#lib/components/shell/topbarSearch.ts";
	import Button from "#lib/components/ui/Button.svelte";
	import { combobox } from "#lib/components/ui/Combobox.svelte";
	import Input from "#lib/components/ui/Input.svelte";
	import { kbd } from "#lib/components/ui/Kbd.svelte";
	import { isMac } from "#lib/components/ui/keyLabels.ts";

	interface Props {
		sheet?: boolean;
		onnavigate?: () => void;
	}

	let { sheet = false, onnavigate }: Props = $props();

	const auth = getAuth();
	const id = $props.id();
	const listId = `${id}-list`;
	const optionId = (index: number) => `${id}-option-${index}`;
	const classes = combobox();

	let value = $state("");
	let focused = $state(false);
	let loading = $state(false);
	let activeIndex = $state(-1);
	let outcome = $state<{ query: string; result: RepoSearchOutcome } | null>(null);
	let input = $state<HTMLInputElement>();
	let mac = $state<boolean>();

	const query = $derived(value.trim());
	const items = $derived(outcome?.result.result?.items ?? []);
	const open = $derived((focused || sheet) && query !== "" && outcome !== null);
	const searchHref = $derived(resolve(`/search?q=${encodeURIComponent(query)}` as "/"));
	const repoHref = (repo: RepoCardData) => resolve(`/${repo.ownerHandle}/${repo.name}` as "/");

	$effect(() => {
		mac = isMac();
		if (sheet) requestAnimationFrame(() => input?.focus());
	});

	$effect(() => {
		const q = query;
		if (!q) {
			outcome = null;
			loading = false;
			return;
		}
		loading = true;
		let stale = false;
		const timer = setTimeout(async () => {
			const result = await searchRepos(createBobbinClient({ serviceUrl: auth.bobbinUrl }), {
				q,
				viewerDid: auth.currentDid ?? undefined,
				limit: QUICK_SEARCH_LIMIT
			}).catch((): RepoSearchOutcome => ({ result: null, error: "Could not search." }));
			if (stale) return;
			outcome = { query: q, result };
			activeIndex = -1;
			loading = false;
		}, QUICK_SEARCH_DELAY);
		return () => {
			stale = true;
			clearTimeout(timer);
		};
	});

	const finish = () => {
		input?.blur();
		onnavigate?.();
	};

	const onkeydown = (event: KeyboardEvent) => {
		if (event.isComposing) return;
		if (event.key === "ArrowDown" || event.key === "ArrowUp") {
			if (!open || items.length === 0) return;
			event.preventDefault();
			activeIndex = moveActive(activeIndex, event.key === "ArrowDown" ? 1 : -1, items.length);
		} else if (event.key === "Enter") {
			const repo = open ? items[activeIndex] : undefined;
			if (repo) {
				event.preventDefault();
				void goto(repoHref(repo));
			}
			finish();
		} else if (event.key === "Escape" && !sheet) {
			event.preventDefault();
			input?.blur();
		}
	};

	const onWindowKeydown = (event: KeyboardEvent) => {
		if (sheet || event.defaultPrevented || !(event.metaKey || event.ctrlKey)) return;
		if (event.key.toLowerCase() !== "k") return;
		event.preventDefault();
		input?.focus();
		input?.select();
	};

	const onfocusout = (event: FocusEvent) => {
		const container = event.currentTarget as HTMLElement;
		if (!container.contains(event.relatedTarget as Node | null)) focused = false;
	};

	const onOptionClick = (repo: RepoCardData) => {
		void goto(repoHref(repo));
		finish();
	};
</script>

<svelte:window onkeydown={onWindowKeydown} />

<form
	action={resolve("/search")}
	method="GET"
	role="search"
	class={sheet ? "flex min-h-0 flex-1 flex-col gap-xs" : "relative hidden md:block"}
	onfocusin={() => (focused = true)}
	{onfocusout}
>
	<Input
		bind:element={input}
		bind:value
		name="q"
		type="search"
		placeholder="Search"
		label="Search"
		iconLeft={Search}
		{loading}
		suffix={!sheet && mac !== undefined && !focused && !value ? shortcutLabel(mac) : undefined}
		suffixClass={kbd()}
		class={sheet ? "w-full" : "w-80"}
		size={sheet ? "regular" : "small"}
		autocomplete="off"
		enterkeyhint="search"
		role="combobox"
		aria-expanded={open}
		aria-controls={listId}
		aria-autocomplete="list"
		aria-activedescendant={open && activeIndex >= 0 ? optionId(activeIndex) : undefined}
		{onkeydown}
	/>
	{#if open && outcome}
		<div
			role="presentation"
			class={sheet
				? "flex min-h-0 flex-1 flex-col"
				: "absolute inset-x-0 top-full z-50 mt-2xs flex max-h-[calc(100dvh-4rem)] flex-col overflow-hidden rounded-md bg-background-default shadow-regular edge-ring dark:shadow-none"}
			onmousedown={(event) => event.preventDefault()}
		>
			{#if items.length > 0}
				<div
					id={listId}
					role="listbox"
					aria-label="Repositories"
					class={classes.list({
						class: sheet && "-mx-xs flex flex-col gap-xs px-none pt-none pb-xs"
					})}
				>
					{#each items as repo, index (repoKey(repo))}
						<div
							id={optionId(index)}
							role="option"
							tabindex={-1}
							aria-selected={index === activeIndex}
							data-active={index === activeIndex || undefined}
							class={classes.option({ class: sheet && "py-xs" })}
							onclick={() => onOptionClick(repo)}
							{onkeydown}
							onpointermove={() => (activeIndex = index)}
						>
							<RepoCard
								{repo}
								starButton={false}
								links={false}
								border={false}
								background={false}
								padding="none"
								class="w-full"
							/>
						</div>
					{/each}
				</div>
				<div
					class={classes.footer({
						class: [
							"flex items-center justify-between typography-paragraph-small text-foreground-muted",
							sheet ? "pt-sm" : "px-sm py-xs"
						]
					})}
				>
					<span
						>Showing {items.length} of {outcome.result.result?.total ??
							items.length}</span
					>
					<Button href={searchHref} icon={ArrowRight} iconSide="right" onclick={finish}>
						All results
					</Button>
				</div>
			{:else}
				<p id={listId} class={classes.empty({ class: sheet && "my-auto" })}>
					{#if outcome.result.error}
						{outcome.result.error}
					{:else}
						<SearchX
							class="mx-auto mb-sm size-10 text-foreground-disabled"
							aria-hidden="true"
						/>
						No results for "{outcome.query}"
					{/if}
				</p>
			{/if}
		</div>
	{/if}
</form>