flotilla/src/app/components/ProfileMultiSelect.svelte

90 lines
2.4 KiB
Svelte
Raw Normal View History

2024-10-03 22:28:06 +00:00
<script lang="ts">
2024-10-08 18:39:16 +00:00
import type {SvelteComponent} from "svelte"
import {type Instance} from "tippy.js"
import {append, remove, uniq} from "@welshman/lib"
import {profileSearch} from "@welshman/app"
import Icon from "@lib/components/Icon.svelte"
import Tippy from "@lib/components/Tippy.svelte"
import Button from "@lib/components/Button.svelte"
import Suggestions from "@lib/editor/Suggestions.svelte"
import SuggestionProfile from "@lib/editor/SuggestionProfile.svelte"
import Name from "@app/components/Name.svelte"
2024-10-09 19:45:13 +00:00
import ProfileDetail from "@app/components/ProfileDetail.svelte"
import {pushDrawer} from "@app/modal"
2024-10-03 22:28:06 +00:00
export let value: string[]
let term = ""
let input: Element
let popover: Instance
let instance: SvelteComponent
2024-10-09 19:45:13 +00:00
const onClick = (pubkey: string) => pushDrawer(ProfileDetail, {pubkey})
2024-10-03 22:28:06 +00:00
const selectPubkey = (pubkey: string) => {
term = ""
popover.hide()
value = uniq(append(pubkey, value))
}
const removePubkey = (pubkey: string) => {
value = remove(pubkey, value)
}
const onKeyDown = (e: Event) => {
if (instance.onKeyDown(e)) {
e.preventDefault()
}
}
$: {
if (term) {
popover?.show()
} else {
popover?.hide()
}
}
</script>
<div class="flex flex-col gap-2">
<div>
{#each value as pubkey (pubkey)}
2024-10-08 18:39:16 +00:00
<div class="flex-inline badge badge-neutral mr-1 gap-1">
2024-10-03 22:28:06 +00:00
<Button class="flex items-center" on:click={() => removePubkey(pubkey)}>
<Icon icon="close-circle" size={4} class="-ml-1 mt-px" />
</Button>
2024-10-09 19:45:13 +00:00
<Button on:click={() => onClick(pubkey)}>
2024-10-03 22:28:06 +00:00
<Name {pubkey} />
2024-10-09 19:45:13 +00:00
</Button>
2024-10-03 22:28:06 +00:00
</div>
{/each}
</div>
<label class="input input-bordered flex w-full items-center gap-2" bind:this={input}>
<Icon icon="magnifer" />
2024-10-08 18:39:16 +00:00
<input
class="grow"
type="text"
placeholder="Search for profiles..."
bind:value={term}
on:keydown={onKeyDown} />
2024-10-03 22:28:06 +00:00
</label>
<Tippy
bind:popover
bind:instance
component={Suggestions}
props={{
term,
select: selectPubkey,
search: profileSearch,
component: SuggestionProfile,
2024-10-08 18:39:16 +00:00
class: "rounded-box",
2024-10-03 22:28:06 +00:00
style: `left: 4px; width: ${input?.clientWidth + 12}px`,
}}
params={{
trigger: "manual",
interactive: true,
2024-10-08 18:39:16 +00:00
maxWidth: "none",
2024-10-03 22:28:06 +00:00
getReferenceClientRect: () => input.getBoundingClientRect(),
2024-10-08 18:39:16 +00:00
}} />
2024-10-03 22:28:06 +00:00
</div>