+page.svelte (7302B)
1 <script lang="ts"> 2 import { getContext, onMount, tick } from 'svelte'; 3 import { page } from '$app/state'; 4 import { goto } from '$app/navigation'; 5 import SearchForm from '../../lib/components/SearchForm.svelte'; 6 import ListingRow from '../../lib/components/ListingRow.svelte'; 7 import SourceStatus from '../../lib/components/SourceStatus.svelte'; 8 import Button from '../../lib/components/primitives/Button.svelte'; 9 import { 10 normalizePublicQuery, 11 queryErrorMessage, 12 readPublicQuery, 13 type PublicQuery 14 } from '../../lib/catalog/query-input.ts'; 15 import { searchHref } from '../../lib/routes'; 16 import { internalHref } from '../../lib/navigation-url.ts'; 17 import { 18 PUBLIC_RUNTIME_CONTEXT, 19 publicRuntimeReady, 20 createPublicView, 21 disposePublicView, 22 type PublicView, 23 type PublicRuntimeContext 24 } from '../../lib/runtime/public-runtime.ts'; 25 import { 26 createSearchView, 27 startSearchView, 28 moreSearchView, 29 olderSearchView, 30 closeSearchView, 31 type SearchView, 32 type SearchViewSnapshot 33 } from '../../lib/catalog/search-view.ts'; 34 import { 35 captureSearchScroll, 36 restoreSearchScroll 37 } from '../../lib/navigation-scroll.ts'; 38 let pendingScroll = $state.raw< 39 { query: string; position: number } | undefined 40 >(); 41 let mounted = false; 42 export const snapshot = { 43 capture: () => { 44 const current = readPublicQuery(page.url); 45 return { 46 query: current.ok ? current.text : undefined, 47 position: captureSearchScroll() 48 }; 49 }, 50 restore: (value: unknown) => { 51 if ( 52 typeof value !== 'object' || 53 value === null || 54 !('query' in value) || 55 typeof value.query !== 'string' || 56 !('position' in value) || 57 typeof value.position !== 'number' 58 ) 59 return; 60 const query = normalizePublicQuery(value.query); 61 if (!query.ok || query.text !== value.query) return; 62 pendingScroll = { query: query.text, position: value.position }; 63 } 64 }; 65 const context = getContext<PublicRuntimeContext>(PUBLIC_RUNTIME_CONTEXT); 66 let query = $state<PublicQuery>(normalizePublicQuery('')), 67 error = $state<string | undefined>(); 68 let owner = $state.raw<SearchView | undefined>(), 69 result = $state.raw<SearchViewSnapshot | undefined>(); 70 let activeQuery: string | undefined; 71 onMount(() => { 72 mounted = true; 73 let disposed = false; 74 const unavailable = () => { 75 if (!disposed) { 76 owner = undefined; 77 error = 'Search sources are unavailable.'; 78 } 79 }; 80 try { 81 void publicRuntimeReady(context) 82 .then((runtime) => { 83 if (disposed || !runtime) return; 84 let acquired: PublicView | undefined; 85 try { 86 acquired = createPublicView(runtime); 87 owner = createSearchView( 88 acquired, 89 { nowSeconds: () => Math.floor(Date.now() / 1000) }, 90 (value) => { 91 result = value; 92 } 93 ); 94 } catch { 95 // Only this page's partial view is owned here; the shared runtime 96 // retains its public budget and any retryable failed disposer. 97 if (acquired) disposePublicView(acquired); 98 unavailable(); 99 } 100 }) 101 .catch(unavailable); 102 } catch { 103 unavailable(); 104 } 105 return () => { 106 disposed = true; 107 mounted = false; 108 pendingScroll = undefined; 109 if (owner) closeSearchView(owner); 110 }; 111 }); 112 $effect(() => { 113 query = readPublicQuery(page.url); 114 error = undefined; 115 }); 116 $effect(() => { 117 if (owner && query.ok && activeQuery !== query.text) { 118 const next = query.text; 119 activeQuery = next; 120 try { 121 startSearchView(owner, next); 122 } catch { 123 error = 'Search could not start. Please try again.'; 124 } 125 } 126 }); 127 $effect(() => { 128 const saved = pendingScroll, 129 current = result; 130 if ( 131 !saved || 132 !current?.listings.length || 133 !query.ok || 134 saved.query !== query.text || 135 current.query !== saved.query 136 ) 137 return; 138 void tick().then(() => { 139 const currentQuery = readPublicQuery(page.url); 140 if ( 141 mounted && 142 pendingScroll === saved && 143 result?.generation === current.generation && 144 page.url.pathname === '/search' && 145 currentQuery.ok && 146 currentQuery.text === saved.query && 147 result.query === saved.query 148 ) { 149 restoreSearchScroll(saved.position); 150 pendingScroll = undefined; 151 } 152 }); 153 }); 154 function search(input: string) { 155 pendingScroll = undefined; 156 const next = normalizePublicQuery(input); 157 if (!next.ok) { 158 error = queryErrorMessage(next.error); 159 return; 160 } 161 const target = internalHref(searchHref(next.text)); 162 if (target === undefined) { 163 error = queryErrorMessage('invalid_query'); 164 return; 165 } 166 error = undefined; 167 if (owner && query.ok && query.text === next.text) { 168 try { 169 startSearchView(owner, next); 170 } catch { 171 error = 'Search could not start. Please try again.'; 172 } 173 return; 174 } 175 if (target !== undefined) void goto(target); 176 } 177 function more() { 178 pendingScroll = undefined; 179 if (owner) moreSearchView(owner); 180 } 181 function older() { 182 pendingScroll = undefined; 183 if (owner) { 184 try { 185 olderSearchView(owner); 186 } catch { 187 error = 188 'Older listings could not be checked. These results may be incomplete.'; 189 } 190 } 191 } 192 const noSources = $derived( 193 result?.scopes 194 .filter((scope) => scope.kind === 'search') 195 .every( 196 (scope) => 197 scope.result.sources.length === 0 || 198 scope.result.sources.every( 199 (source) => source.state === 'error' || source.state === 'closed' 200 ) 201 ) ?? false 202 ); 203 const refresh = $derived( 204 (noSources || (result?.refresh === 'error' && !result.scopes.length)) && 205 !result?.listings.length 206 ? 'unavailable' 207 : (result?.refresh ?? 'unavailable') 208 ); 209 </script> 210 211 <svelte:head><title>Search food — HarvestCircle</title></svelte:head> 212 <div class="page page--reading stack"> 213 <h1>Search food</h1> 214 <SearchForm 215 onsubmit={search} 216 initialValue={query.ok ? query.text : ''} 217 error={error ?? (query.ok ? undefined : queryErrorMessage(query.error))} 218 /> 219 <SourceStatus 220 {refresh} 221 gap={result?.continuation.gap ?? false} 222 scopes={result?.scopes ?? []} 223 /> 224 {#if result?.listings.length} 225 <p> 226 {result.listings.length} listings shown in the sources checked. Newest updates 227 first. 228 </p> 229 <ul class="stack"> 230 {#each result.listings as listing (listing.href)}<li> 231 <ListingRow {listing} /> 232 </li>{/each} 233 </ul> 234 {:else if refresh === 'bounded-eose'}<p> 235 No matches in the sources checked. Try fewer words or search older 236 listings. 237 </p>{/if} 238 <div class="cluster"> 239 {#if result?.hasMore}<Button label="Show more" onclick={more} />{/if} 240 {#if result?.continuation.until !== undefined}<Button 241 label="Search older listings" 242 onclick={older} 243 disabled={result.continuation.active || result.continuation.gap} 244 />{/if} 245 </div> 246 </div>