persist drafts in memory (#155)

Co-authored-by: userAdityaa <aditya.chaudhary1558@gmail.com>
Co-committed-by: userAdityaa <aditya.chaudhary1558@gmail.com>
This commit is contained in:
2026-04-07 12:06:29 +00:00
committed by hodlbod
parent 0547e9513f
commit 30c2a6ef79
12 changed files with 257 additions and 55 deletions
+30 -12
View File
@@ -22,8 +22,21 @@
import {pushToast} from "@app/util/toast"
import {PROTECTED} from "@app/core/state"
import {canEnforceNip70} from "@app/core/commands"
import {DraftKey} from "@app/util/drafts"
import type {PollType} from "@app/util/polls"
type DraftOption = {
id: string
value: string
}
type PollDraft = {
title?: string
pollType?: PollType
endsAt?: number
options?: DraftOption[]
}
type Props = {
url: string
h?: string
@@ -31,12 +44,10 @@
const {url, h}: Props = $props()
const shouldProtect = canEnforceNip70(url)
const draftKey = new DraftKey<PollDraft>(`poll:${url}:${h ?? ""}`)
const draft = draftKey.get()
type DraftOption = {
id: string
value: string
}
const shouldProtect = canEnforceNip70(url)
const back = () => history.back()
@@ -129,17 +140,24 @@
event: makeEvent(Poll, {content: title.trim(), tags}),
})
draftKey.clear()
history.back()
}
let title = $state("")
let pollType = $state<PollType>("singlechoice")
let endsAt = $state<number | undefined>()
let options = $state<DraftOption[]>([
{id: randomId(), value: "Yes"},
{id: randomId(), value: "No"},
])
let title = $state(draft?.title ?? "")
let pollType = $state<PollType>(draft?.pollType ?? "singlechoice")
let endsAt = $state<number | undefined>(draft?.endsAt)
let options = $state<DraftOption[]>(
draft?.options ?? [
{id: randomId(), value: "Yes"},
{id: randomId(), value: "No"},
],
)
let draggedOptionId = $state<string | undefined>()
$effect(() => {
draftKey.set({title, pollType, endsAt, options})
})
</script>
<Modal tag="form" onsubmit={preventDefault(submit)}>