ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 1 | <script lang="ts"> |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 2 | import { onDestroy, onMount } from 'svelte'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 3 | import EmojiText from './EmojiText.svelte'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 4 | import MessageRow from './MessageRow.svelte'; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 5 | import { closeSearch, searchMessages, searchMore } from './api'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 6 | import { formatDay } from './format'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 7 | import { groupByRoom } from './search-model'; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 8 | import type { ScopeOption, SearchCoverage, SearchHit, SearchPage } from './search-model'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 9 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 10 | export let scopes: ScopeOption[]; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 11 | export let initialScope = 0; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 12 | export let self: string | null = null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 13 | export let roomName: (roomId: string) => string; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 14 | export let onOpen: (roomId: string, eventId: string, threadRoot: string | null) => void; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 15 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 16 | let input: HTMLInputElement | null = null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 17 | let query = ''; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 18 | let scopeIndex = Math.min(initialScope, scopes.length - 1); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 19 | let hits: SearchHit[] = []; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 20 | let coverage: SearchCoverage | null = null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 21 | let next: string | null = null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 22 | let searchId: string | null = null; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 23 | let searched = ''; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 24 | let busy = false; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 25 | let error = ''; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 26 | let generation = 0; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 27 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 28 | $: groups = groupByRoom(hits); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 29 | $: partial = coverage?.rooms.filter((c) => !c.start_reached) ?? []; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 30 | $: oldest = partial.reduce<number | null>((min, c) => (c.oldest_indexed_ms === null ? min : min === null ? c.oldest_indexed_ms : Math.max(min, c.oldest_indexed_ms)), null); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 31 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 32 | onMount(() => input?.focus()); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 33 | onDestroy(() => dropSearch()); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 34 | |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 35 | function dropSearch(): void { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 36 | if (searchId) void closeSearch(searchId).catch(() => undefined); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 37 | searchId = null; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 38 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 39 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 40 | async function run(more = false): Promise<void> { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 41 | const text = query.trim(); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 42 | if (!text) return; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 43 | const run = ++generation; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 44 | busy = true; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 45 | error = ''; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 46 | try { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 47 | let page: SearchPage; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 48 | if (more && searchId && next) { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 49 | page = await searchMore(searchId, next); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 50 | } else { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 51 | const session = await searchMessages(text, scopes[scopeIndex].scope); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 52 | if (run !== generation) { |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 53 | void closeSearch(session.search_id).catch(() => undefined); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 54 | return; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 55 | } |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 56 | dropSearch(); |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 57 | searchId = session.search_id; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 58 | page = session.page; |
249e439c27e5 | atagen | 2026-10-25 13:26 UTC | 59 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 60 | if (run !== generation) return; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 61 | hits = more ? [...hits, ...page.hits] : page.hits; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 62 | coverage = page.coverage; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 63 | next = page.next; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 64 | searched = text; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 65 | } catch (e) { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 66 | if (run !== generation) return; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 67 | if (!more) hits = []; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 68 | error = e instanceof Error ? e.message : 'Search failed'; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 69 | } finally { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 70 | if (run === generation) busy = false; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 71 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 72 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 73 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 74 | function pickScope(index: number): void { |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 75 | scopeIndex = index; |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 76 | if (searched) void run(); |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 77 | } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 78 | </script> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 79 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 80 | <section class="search-view" aria-label="Search messages"> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 81 | <form class="search-bar" on:submit|preventDefault={() => void run()}> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 82 | <input bind:this={input} type="search" bind:value={query} placeholder="Search messages" aria-label="Search messages" /> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 83 | <button type="submit" class="primary-btn" disabled={busy || !query.trim()}>{busy && !hits.length ? 'Searching…' : 'Search'}</button> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 84 | </form> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 85 | <div class="scopes" role="radiogroup" aria-label="Search in"> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 86 | {#each scopes as option, i (option.label)} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 87 | <button type="button" role="radio" aria-checked={i === scopeIndex} class:active={i === scopeIndex} on:click={() => pickScope(i)}> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 88 | <EmojiText text={option.label} /> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 89 | </button> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 90 | {/each} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 91 | </div> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 92 | {#if error} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 93 | <p class="search-error" role="alert"><EmojiText text={error} /></p> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 94 | {/if} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 95 | <div class="results"> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 96 | {#if searched && !busy && !error && hits.length === 0} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 97 | <p class="side-note">No messages match “<EmojiText text={searched} />”</p> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 98 | {/if} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 99 | {#each groups as group (group.room_id)} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 100 | <h3 class="result-room"><EmojiText text={roomName(group.room_id)} /></h3> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 101 | {#each group.hits as hit (hit.event_id)} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 102 | <div |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 103 | class="result" |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 104 | role="link" |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 105 | tabindex="0" |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 106 | aria-label={`Open message in ${roomName(hit.room_id)}`} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 107 | on:click={() => onOpen(hit.room_id, hit.event_id, hit.event.thread_root)} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 108 | on:keydown={(e) => e.key === 'Enter' && onOpen(hit.room_id, hit.event_id, hit.event.thread_root)} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 109 | > |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 110 | <MessageRow itemId={`search-${hit.event_id}`} event={hit.event} showSender={true} reacting={false} onReact={() => {}} readOnly={true} showSent={false} {self} /> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 111 | </div> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 112 | {/each} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 113 | {/each} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 114 | {#if next} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 115 | <button type="button" class="ghost-btn more" disabled={busy} on:click={() => void run(true)}>{busy ? 'Loading…' : 'More results'}</button> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 116 | {/if} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 117 | {#if searched && partial.length} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 118 | <p class="coverage">Searched back to {oldest ? formatDay(oldest) : 'recent history'} in {partial.length === 1 ? 'one room' : `${partial.length} rooms`}</p> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 119 | {/if} |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 120 | </div> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 121 | </section> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 122 | |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 123 | <style> |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 124 | .search-view { display: flex; flex-direction: column; min-height: 0; height: 100%; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 125 | .search-bar { display: flex; gap: 8px; padding: 12px 16px 8px; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 126 | .search-bar input { flex: 1; min-width: 0; background: var(--bg-soft); border: 1px solid var(--border); border-radius: var(--r-md); color: var(--text); padding: 8px 10px; font: inherit; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 127 | .search-bar input:focus { outline: none; border-color: var(--accent); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 128 | .scopes { display: flex; gap: 6px; padding: 0 16px 10px; border-bottom: 1px solid var(--border); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 129 | .scopes button { border: 1px solid var(--border); background: transparent; color: var(--muted); border-radius: var(--r-sm); padding: 3px 10px; font: inherit; font-size: 12.5px; cursor: pointer; transition: color var(--pop), border-color var(--pop), background var(--pop); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 130 | .scopes button:hover { color: var(--text); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 131 | .scopes button.active { color: var(--text); border-color: var(--accent); background: color-mix(in srgb, var(--accent) 16%, transparent); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 132 | .search-error { margin: 10px 16px 0; color: var(--danger); font-size: 13px; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 133 | .results { flex: 1; min-height: 0; overflow-y: auto; padding: 8px 16px 16px; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 134 | .result-room { margin: 14px 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 135 | .result { border-radius: var(--r-md); cursor: pointer; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 136 | .result:hover { background: var(--hover-faint); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 137 | .result:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 138 | .more { display: block; margin: 12px auto 0; } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 139 | .coverage { margin: 14px 0 0; text-align: center; font-size: 12px; color: var(--muted); } |
ba90983cec2e | atagen | 2026-10-25 10:51 UTC | 140 | </style> |