Split state for searching people and notes

This commit is contained in:
Jonathan Staab 2022-12-02 04:53:54 -08:00
parent 794beda618
commit 16d1f8adc7
4 changed files with 46 additions and 37 deletions

View File

@ -76,7 +76,11 @@
<Router {url}> <Router {url}>
<div use:links class="h-full"> <div use:links class="h-full">
<div class="pt-16 text-white h-full"> <div class="pt-16 text-white h-full">
<Route path="/search" component={Search} /> <Route path="/search/:type" let:params>
{#key params.type}
<Search {...params} />
{/key}
</Route>
<Route path="/notes" component={Notes} /> <Route path="/notes" component={Notes} />
<Route path="/notes/new" component={NoteCreate} /> <Route path="/notes/new" component={NoteCreate} />
<Route path="/chat" component={Chat} /> <Route path="/chat" component={Chat} />
@ -115,7 +119,7 @@
</li> </li>
{/if} {/if}
<li class="cursor-pointer"> <li class="cursor-pointer">
<a class="block px-4 py-2 hover:bg-accent transition-all" href="/search"> <a class="block px-4 py-2 hover:bg-accent transition-all" href="/search/people">
<i class="fa-solid fa-search mr-2" /> Search <i class="fa-solid fa-search mr-2" /> Search
</a> </a>
</li> </li>

View File

@ -7,6 +7,6 @@
<div <div
class="overflow-hidden w-4 h-4 rounded-full bg-cover bg-center shrink-0 border border-solid border-white" class="overflow-hidden w-4 h-4 rounded-full bg-cover bg-center shrink-0 border border-solid border-white"
style="background-image: url({user.picture})" /> style="background-image: url({user.picture})" />
<span class="text-lg font-bold">{user.name || ''}</span> <span class="text-lg font-bold">{user.name || user.pubkey.slice(0, 8)}</span>
</a> </a>
{/if} {/if}

View File

@ -2,18 +2,19 @@
import {onMount, onDestroy} from 'svelte' import {onMount, onDestroy} from 'svelte'
import {writable} from 'svelte/store' import {writable} from 'svelte/store'
import {fly} from 'svelte/transition' import {fly} from 'svelte/transition'
import {uniqBy, identity, uniq, pluck, prop} from 'ramda' import {uniqBy, pluck, prop} from 'ramda'
import {fuzzy} from "src/util/misc" import {fuzzy} from "src/util/misc"
import Anchor from "src/partials/Anchor.svelte" import Anchor from "src/partials/Anchor.svelte"
import Input from "src/partials/Input.svelte" import Input from "src/partials/Input.svelte"
import Spinner from "src/partials/Spinner.svelte" import Spinner from "src/partials/Spinner.svelte"
import Note from "src/partials/Note.svelte" import Note from "src/partials/Note.svelte"
import {relays, Cursor} from "src/state/nostr" import {relays, Cursor} from "src/state/nostr"
import {createScroller, accounts, annotateNotes, modal} from "src/state/app" import {user} from "src/state/user"
import {createScroller, ensureAccounts, accounts, annotateNotes, modal} from "src/state/app"
const notes = writable([]) export let type
const people = writable([])
let type = writable('people') const data = writable([])
let q = '' let q = ''
let search let search
let results let results
@ -21,11 +22,7 @@
let scroller let scroller
let modalUnsub let modalUnsub
$: search = ( $: search = fuzzy($data, {keys: type === 'people' ? ["name", "about", "pubkey"] : ["content"]})
$type === 'people'
? fuzzy($people, {keys: ["name", "about"]})
: fuzzy($notes, {keys: ["content"]})
)
$: { $: {
scroller?.start() scroller?.start()
@ -33,17 +30,17 @@
} }
onMount(async () => { onMount(async () => {
cursor = new Cursor({kinds: [1]}) cursor = new Cursor({kinds: type === 'people' ? [0] : [1]})
scroller = createScroller(cursor, async chunk => { scroller = createScroller(cursor, async chunk => {
const annotated = await annotateNotes(chunk) if (type === 'people') {
const keys = uniq(pluck('pubkey', chunk)) await ensureAccounts(pluck('pubkey', chunk))
notes.update($notes => uniqBy(prop('id'), $notes.concat(annotated))) data.set(Object.values($accounts))
people.update($people => { } else {
$people = $people.concat(keys.map(k => $accounts[k]).filter(identity)) const annotated = await annotateNotes(chunk)
return uniqBy(prop('pubkey'), $people) data.update($data => uniqBy(prop('id'), $data.concat(annotated)))
}) }
}) })
// When a modal opens, suspend our subscriptions // When a modal opens, suspend our subscriptions
@ -69,49 +66,57 @@
<ul class="border-b border-solid border-dark flex max-w-xl m-auto pt-2" in:fly={{y: 20}}> <ul class="border-b border-solid border-dark flex max-w-xl m-auto pt-2" in:fly={{y: 20}}>
<li <li
class="px-8 py-4 cursor-pointer hover:border-b border-solid border-medium" class="cursor-pointer hover:border-b border-solid border-medium"
class:border-b={$type === 'people'} class:border-b={type === 'people'}>
on:click={() => type.set('people')}> <a class="block px-8 py-4 " href="/search/people">People</a>
People
</li> </li>
<li <li
class="px-8 py-4 cursor-pointer hover:border-b border-solid border-medium" class="cursor-pointer hover:border-b border-solid border-medium"
class:border-b={$type === 'notes'} class:border-b={type === 'notes'}>
on:click={() => type.set('notes')}> <a class="block px-8 py-4 " href="/search/notes">Notes</a>
Notes
</li> </li>
</ul> </ul>
<div class="max-w-xl m-auto mt-4" in:fly={{y: 20}}> <div class="max-w-xl m-auto mt-4" in:fly={{y: 20}}>
<Input bind:value={q} placeholder="Search for {$type}"> <Input bind:value={q} placeholder="Search for {type}">
<i slot="before" class="fa-solid fa-search" /> <i slot="before" class="fa-solid fa-search" />
</Input> </Input>
</div> </div>
{#if type === 'people'}
<ul class="py-8 flex flex-col gap-2 max-w-xl m-auto"> <ul class="py-8 flex flex-col gap-2 max-w-xl m-auto">
{#each (results || []) as e (e.id || e.pubkey)} {#each (results || []) as e (e.pubkey)}
{#if e.pubkey !== $user.pubkey}
<li in:fly={{y: 20}}> <li in:fly={{y: 20}}>
{#if e.isAccount}
<a href="/users/{e.pubkey}" class="flex gap-4 my-4"> <a href="/users/{e.pubkey}" class="flex gap-4 my-4">
<div <div
class="overflow-hidden w-12 h-12 rounded-full bg-cover bg-center shrink-0 border border-solid border-white" class="overflow-hidden w-12 h-12 rounded-full bg-cover bg-center shrink-0 border border-solid border-white"
style="background-image: url({e.picture})" /> style="background-image: url({e.picture})" />
<div class="flex-grow"> <div class="flex-grow">
<h1 class="text-2xl">{e.name}</h1> <h1 class="text-2xl">{e.name || e.pubkey.slice(0, 8)}</h1>
<p>{e.about || ''}</p> <p>{e.about || ''}</p>
</div> </div>
</a> </a>
{:else} <li>
{/if}
{/each}
</ul>
{/if}
{#if type === 'notes'}
<ul class="py-8 flex flex-col gap-2 max-w-xl m-auto">
{#each (results || []) as e (e.id)}
<li in:fly={{y: 20}}>
<Note interactive note={e} /> <Note interactive note={e} />
{#each e.replies as r (r.id)} {#each e.replies as r (r.id)}
<div class="ml-6 border-l border-solid border-medium"> <div class="ml-6 border-l border-solid border-medium">
<Note interactive isReply note={r} /> <Note interactive isReply note={r} />
</div> </div>
{/each} {/each}
{/if}
</li> </li>
{/each} {/each}
</ul> </ul>
{/if}
<!-- This will always be sitting at the bottom in case infinite scrolling can't keep up --> <!-- This will always be sitting at the bottom in case infinite scrolling can't keep up -->
<Spinner /> <Spinner />

View File

@ -41,7 +41,7 @@ export const ensureAccounts = async (pubkeys, {force = false} = {}) => {
) )
if (pubkeys.length) { if (pubkeys.length) {
const events = await channels.getter.all({kinds: [0], authors: pubkeys}) const events = await channels.getter.all({kinds: [0], authors: uniq(pubkeys)})
await accounts.update($accounts => { await accounts.update($accounts => {
events.forEach(e => { events.forEach(e => {
@ -50,7 +50,7 @@ export const ensureAccounts = async (pubkeys, {force = false} = {}) => {
...$accounts[e.pubkey], ...$accounts[e.pubkey],
...JSON.parse(e.content), ...JSON.parse(e.content),
refreshed: now(), refreshed: now(),
isAccount: true, isUser: true,
} }
}) })