Add instance autocomplete

This commit is contained in:
CmdrKeen committed 2023-11-27 15:39:22 -08:00
1 parent da5b390a93
commit 14d1e8f649
1 file changed
+42 -3
@@ -4,23 +4,25 @@
import type { Community, ListingType } from 'lemmy-js-client'
import { MenuButton, Search } from 'mono-svelte'
import { createEventDispatcher } from 'svelte'
import { Icon, XCircle } from 'svelte-hero-icons'
import { Icon, XCircle, ServerStack } from 'svelte-hero-icons'
import { fly } from 'svelte/transition'
let type: 'community' | 'instance' = 'community'
export let type: 'community' | 'instance' = 'community'
export let q: string = ''
export let instance: string | undefined = undefined
export let jwt: string | undefined = undefined
export let listing_type: ListingType = 'Subscribed'
export let showWhenEmpty: boolean = false
const dispatcher = createEventDispatcher<{ select: Community | undefined }>()
$: instances = type == 'instance' && getClient().getFederatedInstances()
</script>
{#if type == 'community'}
<Search
search={async (q) => {
const results = await getClient().search({
const results = await getClient(instance).search({
q: q || ' ',
type_: 'Communities',
limit: 20,
@@ -62,4 +64,41 @@
</MenuButton>
</div>
</Search>
{:else if type == 'instance'}
<Search
search={async (q) => {
const results = await instances || {}
return q ? (results.federated_instances?.linked || []).filter(
(i) => i.domain.includes(q)
) : []
}}
extractName={(i) => `${i.domain}`}
on:select
bind:query={q}
{showWhenEmpty}
{...$$restProps}
let:item
let:select
>
<div class="w-full h-full" slot="noresults">
{#if q == '' && showWhenEmpty}
<MenuButton on:click={() => dispatcher('select', undefined)}>
<div class="flex flex-col text-left">
<span>None (Start typing to search)</span>
</div>
</MenuButton>
{:else}
<span class="mx-auto my-auto">No results.</span>
{/if}
</div>
<div in:fly|global={{ y: -4, opacity: 0 }}>
<MenuButton on:click={() => select(item)}>
<Icon src={ServerStack} size="16" mini />
<div class="flex flex-col text-left">
<span>{item.domain}</span>
</div>
</MenuButton>
</div>
</Search>
{/if}