~james/core

core/web/src/lib/components/search/CodeSearchView.svelte -rw-r--r-- 4.6 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
<script lang="ts">
	import type { Did } from "@atcute/lexicons";
	import { untrack } from "svelte";
	import { resolve } from "$app/paths";
	import { getAuth } from "#lib/auth.svelte.ts";
	import { loginWithReturn } from "#lib/auth/guards.ts";
	import Button from "#lib/components/ui/Button.svelte";
	import EmptyState from "#lib/components/ui/EmptyState.svelte";
	import ErrorBox from "#lib/components/ui/Error.svelte";
	import Spinner from "#lib/components/ui/Spinner.svelte";
	import Card from "#lib/components/ui/Card.svelte";
	import SearchBar from "#lib/components/search/SearchBar.svelte";
	import SearchLayout from "#lib/components/search/SearchLayout.svelte";
	import QueryToken from "#lib/components/search/QueryToken.svelte";
	import CodeResultCard from "#lib/components/search/CodeResultCard.svelte";
	import FileMatch from "#lib/components/search/FileMatch.svelte";
	import { searchUrl } from "#lib/components/search/SearchTabs.svelte";
	import { matchCount } from "#lib/components/search/chunks.ts";
	import { createCodeSearch } from "#lib/components/search/codeSearch.svelte.ts";
	import { plural } from "#lib/format.ts";

	export interface RepoScope {
		ownerHandle: string;
		repoName: string;
		repoDid?: string;
	}

	interface Props {
		query: string;
		repo?: RepoScope;
	}

	let { query, repo }: Props = $props();

	const auth = getAuth();
	const search = createCodeSearch({
		query: () => query,
		repo: () => repo?.repoDid as Did | undefined
	});

	// eslint-disable-next-line svelte/prefer-writable-derived -- draft is two-way bound to search input
	let draft = $state(untrack(() => query));

	$effect(() => {
		draft = query;
	});

	const signedIn = $derived(Boolean(auth.currentDid));

	const loginHref = $derived(
		resolve(
			loginWithReturn(
				repo
					? (`/${repo.ownerHandle}/${repo.repoName}/search${query ? `?q=${encodeURIComponent(query)}` : ""}` as "/")
					: (searchUrl("code", query) as "/")
			) as "/login"
		)
	);

	const matches = $derived(
		search.results.reduce((n, r) => n + matchCount(r.chunks ?? []) + (r.ranges?.length ?? 0), 0)
	);
</script>

<svelte:head>
	{#if repo}
		<title>Search &middot; {repo.ownerHandle}/{repo.repoName} &middot; Tangled</title>
	{:else}
		<title>Code Search &middot; Tangled</title>
	{/if}
</svelte:head>

{#snippet resultsList()}
	{#if !signedIn}
		<EmptyState message="Code search needs an account.">
			<a href={loginHref} class="underline">Sign in</a>
		</EmptyState>
	{:else if repo && !repo.repoDid}
		<EmptyState message="This repository has no DID to search by." />
	{:else if !query}
		{#if repo}
			<EmptyState
				message="Enter a search term to find files or code in this repository."
				messageDirection="vertical"
				class="px-md text-center md:px-4xl"
			>
				<span>
					Narrow it with <QueryToken>lang:go</QueryToken>,
					<QueryToken>file:readme</QueryToken>, or
					<QueryToken>case:yes</QueryToken>.
				</span>
			</EmptyState>
		{:else}
			<EmptyState message="Enter a query to search code." />
		{/if}
	{:else if search.loading}
		<Card
			padding="none"
			border={false}
			class="flex items-center justify-center p-4xl text-foreground-subtle edge-ring-inset"
			role="status"
			aria-label="Searching code"
		>
			<Spinner class="size-6" />
		</Card>
	{:else if search.error}
		<ErrorBox label={search.error} />
	{:else}
		<div class="grid grid-cols-1 gap-sm">
			{#each search.results as result (`${result.repo.did}/${result.commit}/${result.path}`)}
				{#if repo}
					<FileMatch {result} />
				{:else}
					<CodeResultCard {result} />
				{/if}
			{:else}
				<EmptyState message="No results found." />
			{/each}
		</div>

		{#if search.moreError}
			<ErrorBox label={search.moreError} />
		{/if}

		{#if search.cursor}
			<Button
				onclick={search.loadMore}
				loading={search.loadingMore}
				disabled={search.loadingMore}
				class="w-full"
			>
				Load more
			</Button>
		{/if}
	{/if}
{/snippet}

{#snippet matchSummary()}
	{#if matches > 0}
		<div
			class="border-t border-border-default py-xs typography-paragraph-small text-foreground-muted"
		>
			Found {matches}
			{plural(matches, "match", "matches")} in {search.results.length}
			{plural(search.results.length, "file", "files")}
		</div>
	{/if}
{/snippet}

{#if repo}
	<Card class="flex flex-col gap-sm">
		<SearchBar type="code" bind:query={draft} placeholder="Find files or code..." />
		{@render resultsList()}
	</Card>
{:else}
	<SearchLayout title="Code Search" query={draft} active="code" aside={matchSummary}>
		{#snippet bar()}
			<SearchBar type="code" bind:query={draft} placeholder="Search code..." mono />
		{/snippet}
		{@render resultsList()}
	</SearchLayout>
{/if}