flotilla/src/app/components/ProfileMultiSelect.svelte

120 lines
3.3 KiB
Svelte
Raw Normal View History

2024-10-03 22:28:06 +00:00
<script lang="ts">
2025-02-05 03:37:59 +00:00
import {writable} from "svelte/store"
import type {Writable} from "svelte/store"
2024-10-08 18:39:16 +00:00
import {type Instance} from "tippy.js"
import {append, remove, uniq} from "@welshman/lib"
import {decodePubkey} from "@lib/util"
2025-02-05 03:00:48 +00:00
import Suggestions from "@lib/components/Suggestions.svelte"
2025-09-10 22:26:39 +00:00
import CloseCircle from "@assets/icons/close-circle.svg?dataurl"
import Magnifier from "@assets/icons/magnifier.svg?dataurl"
2024-10-08 18:39:16 +00:00
import Icon from "@lib/components/Icon.svelte"
import Tippy from "@lib/components/Tippy.svelte"
import Button from "@lib/components/Button.svelte"
import Badge from "@lib/components/Badge.svelte"
2025-01-02 18:04:28 +00:00
import ProfileSuggestion from "@app/editor/ProfileSuggestion.svelte"
2024-10-11 23:18:05 +00:00
import ProfileName from "@app/components/ProfileName.svelte"
2024-12-16 20:54:17 +00:00
import ProfileDetail from "@app/components/ProfileDetail.svelte"
2026-07-28 16:16:26 +00:00
import {profiles} from "@app/core"
2026-06-08 21:14:40 +00:00
import {pushModal} from "@app/modal"
2024-10-03 22:28:06 +00:00
2026-07-28 16:16:26 +00:00
type Props = {
2025-02-04 00:37:14 +00:00
value: string[]
autofocus?: boolean
term?: Writable<string>
2025-02-04 00:37:14 +00:00
}
let {value = $bindable(), term = writable(""), autofocus = false}: Props = $props()
2024-10-03 22:28:06 +00:00
2026-07-28 16:16:26 +00:00
const profileSearch = $profiles.profileSearch
2025-02-05 03:37:59 +00:00
const search = (term: string) => $profileSearch.searchValues(term)
2025-01-03 00:12:46 +00:00
2024-10-03 22:28:06 +00:00
const selectPubkey = (pubkey: string) => {
2025-02-05 03:37:59 +00:00
term.set("")
2025-02-04 00:37:14 +00:00
popover?.hide()
2024-10-03 22:28:06 +00:00
value = uniq(append(pubkey, value))
}
const removePubkey = (pubkey: string) => {
value = remove(pubkey, value)
}
const onInput = (e: any) => {
const pubkey = decodePubkey(e.target.value)
if (pubkey) {
selectPubkey(pubkey)
}
}
2024-10-03 22:28:06 +00:00
const onKeyDown = (e: Event) => {
2026-07-28 00:13:14 +00:00
if (instance?.onKeyDown(e)) {
2024-10-03 22:28:06 +00:00
e.preventDefault()
}
}
let label: Element | undefined = $state()
2025-02-05 03:37:59 +00:00
let popover: Instance | undefined = $state()
let instance: any = $state()
2025-02-04 00:37:14 +00:00
$effect(() => {
// @ts-ignore
oninput?.($term)
2025-02-05 03:37:59 +00:00
if ($term) {
2024-10-03 22:28:06 +00:00
popover?.show()
} else {
popover?.hide()
}
2025-02-04 00:37:14 +00:00
})
2024-10-03 22:28:06 +00:00
</script>
<div class="flex flex-col gap-1">
2024-10-03 22:28:06 +00:00
<div>
{#each value as pubkey (pubkey)}
2024-12-16 20:54:17 +00:00
{@const onClick = () => pushModal(ProfileDetail, {pubkey})}
<Badge variant="neutral" class="flex-inline mr-1 mb-1 gap-1">
<Button class="flex items-center" onclick={() => removePubkey(pubkey)}>
2025-09-10 22:26:39 +00:00
<Icon icon={CloseCircle} size={4} class="-ml-1 mt-px" />
2024-10-03 22:28:06 +00:00
</Button>
<Button onclick={onClick}>
2024-10-11 23:18:05 +00:00
<ProfileName {pubkey} />
2024-12-16 20:54:17 +00:00
</Button>
</Badge>
2024-10-03 22:28:06 +00:00
{/each}
</div>
<label class="input flex w-full items-center gap-2" bind:this={label}>
2025-09-10 22:26:39 +00:00
<Icon icon={Magnifier} />
2025-02-04 00:37:14 +00:00
<!-- svelte-ignore a11y_autofocus -->
2024-10-08 18:39:16 +00:00
<input
2024-11-16 16:04:09 +00:00
{autofocus}
2024-10-08 18:39:16 +00:00
class="grow"
type="text"
placeholder="Search for profiles..."
2025-02-05 03:37:59 +00:00
bind:value={$term}
oninput={onInput}
2025-02-04 00:37:14 +00:00
onkeydown={onKeyDown} />
2024-10-03 22:28:06 +00:00
</label>
<Tippy
bind:popover
bind:instance
component={Suggestions}
props={{
term,
2025-01-03 00:12:46 +00:00
search,
2024-10-03 22:28:06 +00:00
select: selectPubkey,
2025-01-02 18:04:28 +00:00
component: ProfileSuggestion,
class: "rounded-2xl",
style: `left: 4px; width: ${label?.clientWidth + 12}px`,
2024-10-03 22:28:06 +00:00
}}
params={{
trigger: "manual",
interactive: true,
placement: "bottom",
getReferenceClientRect: () => label!.getBoundingClientRect(),
onShow: (instance: Instance) => {
instance.popper.style.width = `${label!.getBoundingClientRect().width + 8}px`
},
2024-10-08 18:39:16 +00:00
}} />
2024-10-03 22:28:06 +00:00
</div>