flotilla/src/routes/settings/profile/+page.svelte

160 lines
5.5 KiB
Svelte
Raw Normal View History

2024-10-02 23:36:08 +00:00
<script lang="ts">
2024-10-18 23:09:17 +00:00
import {nip19} from "nostr-tools"
import {hexToBytes} from "@noble/hashes/utils"
2024-10-09 23:15:09 +00:00
import {ctx} from "@welshman/lib"
2024-10-08 18:39:16 +00:00
import {
createEvent,
displayPubkey,
displayProfile,
makeProfile,
editProfile,
createProfile,
isPublishedProfile,
} from "@welshman/util"
import {pubkey, session, publishThunk, displayNip05, deriveProfile} from "@welshman/app"
2024-10-08 18:39:16 +00:00
import {slide} from "@lib/transition"
2024-10-02 23:36:08 +00:00
import Icon from "@lib/components/Icon.svelte"
2024-10-08 18:39:16 +00:00
import Field from "@lib/components/Field.svelte"
import FieldInline from "@lib/components/FieldInline.svelte"
2024-10-02 23:36:08 +00:00
import Button from "@lib/components/Button.svelte"
import Avatar from "@lib/components/Avatar.svelte"
import InputProfilePicture from "@lib/components/InputProfilePicture.svelte"
2024-10-08 18:39:16 +00:00
import Content from "@app/components/Content.svelte"
import InfoHandle from "@app/components/InfoHandle.svelte"
2024-10-02 23:36:08 +00:00
import {pushModal} from "@app/modal"
import {pushToast, clip} from "@app/toast"
2024-10-02 23:36:08 +00:00
2024-10-17 20:56:53 +00:00
const profile = deriveProfile($pubkey!)
2024-10-02 23:36:08 +00:00
const pubkeyDisplay = displayPubkey($pubkey!)
const copyNpub = () => clip(nip19.npubEncode($session!.pubkey))
const copyNsec = () => clip(nip19.nsecEncode(hexToBytes($session!.secret!)))
2024-10-17 20:56:53 +00:00
const cloneProfile = () => ({...($profile || makeProfile())})
2024-10-03 19:27:41 +00:00
2024-10-02 23:36:08 +00:00
const toggleEdit = () => {
editing = !editing
2024-10-03 19:27:41 +00:00
if (!editing) {
2024-10-17 20:56:53 +00:00
values = cloneProfile()
2024-10-03 19:27:41 +00:00
}
2024-10-02 23:36:08 +00:00
}
const stopEdit = () => {
editing = false
2024-10-17 20:56:53 +00:00
values = cloneProfile()
2024-10-02 23:36:08 +00:00
}
const saveEdit = () => {
const relays = ctx.app.router.WriteRelays().getUrls()
2024-10-17 20:56:53 +00:00
const template = isPublishedProfile(values) ? editProfile(values) : createProfile(values)
2024-10-02 23:36:08 +00:00
const event = createEvent(template.kind, template)
publishThunk({event, relays})
2024-10-02 23:36:08 +00:00
pushToast({message: "Your profile has been updated!"})
editing = false
}
let file: File
let editing = false
2024-10-17 20:56:53 +00:00
let values = cloneProfile()
$: display = editing ? values : $profile
2024-10-02 23:36:08 +00:00
</script>
<div class="content column gap-4">
2024-10-03 21:32:55 +00:00
<div class="card2 bg-alt shadow-xl">
2024-10-08 18:39:16 +00:00
<div class="flex justify-between gap-2">
<div class="flex max-w-full gap-3">
2024-10-03 21:32:55 +00:00
<div class="py-1">
2024-10-17 20:56:53 +00:00
<Avatar src={display?.picture} size={10} />
2024-10-03 21:32:55 +00:00
</div>
2024-10-08 18:39:16 +00:00
<div class="flex min-w-0 flex-col">
<div class="flex items-center gap-2">
<div class="text-bold overflow-hidden text-ellipsis">
2024-10-17 20:56:53 +00:00
{displayProfile(display, pubkeyDisplay)}
2024-10-02 23:36:08 +00:00
</div>
</div>
2024-10-08 18:39:16 +00:00
<div class="overflow-hidden text-ellipsis text-sm opacity-75">
2024-10-17 20:56:53 +00:00
{display?.nip05 ? displayNip05(display.nip05) : pubkeyDisplay}
2024-10-03 21:32:55 +00:00
</div>
2024-10-02 23:36:08 +00:00
</div>
</div>
2024-10-08 18:39:16 +00:00
<Button class="center btn btn-circle btn-neutral -mr-4 -mt-4 h-12 w-12" on:click={toggleEdit}>
2024-10-03 21:32:55 +00:00
<Icon icon="pen-new-square" />
</Button>
2024-10-02 23:36:08 +00:00
</div>
2024-10-17 20:56:53 +00:00
{#key display?.about}
<Content event={{content: display?.about || "", tags: []}} hideMedia />
2024-10-03 21:32:55 +00:00
{/key}
2024-10-02 23:36:08 +00:00
</div>
{#if editing}
2024-10-18 23:09:17 +00:00
<form class="card2 bg-alt col-4 shadow-xl" transition:slide on:submit|preventDefault={saveEdit}>
2024-10-03 21:32:55 +00:00
<div class="flex justify-center py-2">
2024-10-17 20:56:53 +00:00
<InputProfilePicture bind:file bind:url={values.picture} />
2024-10-03 21:32:55 +00:00
</div>
<Field>
<p slot="label">Username</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="user-circle" />
2024-10-17 20:56:53 +00:00
<input bind:value={values.name} class="grow" type="text" />
2024-10-03 21:32:55 +00:00
</label>
</Field>
<Field>
<p slot="label">About You</p>
2024-10-08 18:39:16 +00:00
<textarea
class="textarea textarea-bordered leading-4"
rows="3"
2024-10-17 20:56:53 +00:00
bind:value={values.about}
2024-10-08 18:39:16 +00:00
slot="input" />
2024-10-03 21:32:55 +00:00
</Field>
<Field>
<p slot="label">Nostr Address</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
2024-10-18 22:58:44 +00:00
<Icon icon="map-point" />
2024-10-17 20:56:53 +00:00
<input bind:value={values.nip05} class="grow" type="text" />
2024-10-03 21:32:55 +00:00
</label>
<p slot="info">
2024-10-08 18:39:16 +00:00
<Button class="link" on:click={() => pushModal(InfoHandle)}
>What is a nostr address?</Button>
2024-10-03 21:32:55 +00:00
</p>
</Field>
2024-10-08 18:39:16 +00:00
<div class="mt-4 flex flex-row items-center justify-between gap-4">
<Button class="btn btn-neutral" on:click={stopEdit}>Discard Changes</Button>
<Button type="submit" class="btn btn-primary">Save Changes</Button>
2024-10-02 23:36:08 +00:00
</div>
</form>
{/if}
{#if $session?.method === "nip01"}
2024-10-18 23:09:17 +00:00
<div class="card2 bg-alt col-4 shadow-xl" transition:slide>
<FieldInline>
<p slot="label">Public Key</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="link-round" />
<input value={$session.pubkey} class="grow" type="text" />
<Button class="flex items-center" on:click={copyNpub}>
<Icon icon="copy" />
</Button>
</label>
<p slot="info">
2024-10-18 23:09:17 +00:00
Your public key is your nostr user identifier. It also allows other people to authenticate
your messages.
</p>
<p></p></FieldInline>
<FieldInline>
<p slot="label">Private Key</p>
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="link-round" />
<input value={$session.secret} class="grow" type="password" />
<Button class="flex items-center" on:click={copyNsec}>
<Icon icon="copy" />
</Button>
</label>
2024-10-18 23:09:17 +00:00
<p slot="info">Your private key is your nostr password. Keep this somewhere safe!</p>
<p></p></FieldInline>
</div>
{/if}
2024-10-02 23:36:08 +00:00
</div>