import { createStore, produce } from "solid-js/store"; import { emitDraftEvent } from "./draft-events"; import type { Interaction } from "./interaction"; import { readStored, writeStored } from "./storage"; /** * Posts being written, by draft id: new posts and quotes, listed in their account's Drafts, and * replies, kept on the post they answer. Kept in storage, so a draft outlives a reload, or closing * the window, until it's sent or thrown away. */ /** A post as a record points at another: its address, and the version of it, by content. */ export type StrongRef = { uri: string; cid: string }; /** A post after the first in a thread being written: its words; its pictures are kept apart. */ export type ThreadPost = { id: string; text: string }; export type Draft = { /** The account it's being written from. */ account: string; /** Its first post's words. */ text: string; /** For a thread: the posts after the first, in order, each answering the one before. */ more?: ThreadPost[]; updatedAt: number; /** For a reply: the post it answers, and the post its thread starts from. */ reply?: { root: StrongRef; parent: StrongRef }; /** For a quote: the post it quotes, shown under what's written. */ quote?: StrongRef; /** * Who may reply to it and quote it, once chosen for it; until then, the account's own setting * on Bluesky. Not for a reply, which follows its thread's. */ interaction?: Interaction; /** Who wrote the post it quotes, for the list of drafts. */ quoting?: string; /** * Warnings on its posts' pictures or video, by post (the first's under ""), as Bluesky's * self-labels: "sexual", "nudity", "porn", "graphic-media". */ labels?: Record; /** Whether it has pictures or a video, which are kept elsewhere (see draft-images). */ media?: boolean; /** Its id among the account's drafts on Bluesky, once it's there (see remote-drafts). */ remoteId?: string; /** When it was last the same as on Bluesky: changed since, it's to be sent again. */ syncedAt?: number; /** * Written on another device with pictures or a video, which stayed there: where Bluesky says * they are, sent back as they were, so changing the words here doesn't lose them. */ mediaElsewhere?: Record; }; /** Whether there's anything in a draft to keep: words, pictures or a video, or a post quoted. */ export const worthKeeping = (draft: Draft) => !!draft.mediaElsewhere || !!draft.text.trim() || !!draft.media || !!draft.quote || !!draft.more?.some((post) => post.text.trim()); export const isReply = (id: string, draft: Draft) => !!draft.reply || id.startsWith("reply:"); const KEY = "parfait:drafts"; // Started and left empty: nothing to come back to. const load = (): Record => Object.fromEntries( Object.entries(readStored>(KEY, {})).filter(([, draft]) => worthKeeping(draft), ), ); const [drafts, setDrafts] = createStore>(load()); const save = () => writeStored(KEY, drafts); export const draftOf = (id: string): Draft | undefined => drafts[id]; /** An account's new posts and quotes being written, the most recently touched first. */ export const draftsOf = (account: string | undefined) => Object.entries(drafts) .filter( ([id, draft]) => draft.account === account && !isReply(id, draft) && worthKeeping(draft), ) .map(([id, draft]) => ({ id, ...draft })) .sort((a, b) => b.updatedAt - a.updatedAt); /** Hands a draft to another account: it's listed in that account's Drafts from now on. */ export const moveDraft = (id: string, account: string) => { const before = drafts[id]; if (!before || before.account === account) return; // On Bluesky, it leaves the first account's drafts and joins the other's, as a new one. emitDraftEvent({ type: "removed", id, draft: { ...before }, moved: true }); setDrafts(id, { account, updatedAt: Date.now(), remoteId: undefined, syncedAt: undefined }); save(); emitDraftEvent({ type: "changed", id }); }; /** Who may reply to a draft once it's posted, and quote it, as chosen for it. */ export const setDraftInteraction = (id: string, account: string, interaction: Interaction) => { setDrafts(id, (draft) => ({ ...draft, text: draft?.text ?? "", account: draft?.account ?? account, interaction, updatedAt: Date.now(), })); save(); emitDraftEvent({ type: "changed", id }); }; /** The warnings on one of a draft's posts' media: none, to take them off. */ export const setDraftLabels = (id: string, account: string, post: string, values: string[]) => { setDrafts(id, (draft) => { const labels = { ...draft?.labels, [post]: values }; if (!values.length) delete labels[post]; return { ...draft, text: draft?.text ?? "", account: draft?.account ?? account, labels, updatedAt: Date.now(), }; }); save(); emitDraftEvent({ type: "changed", id }); }; /** Notes whether a draft has pictures or a video, so the list can tell it's worth keeping. */ export const setDraftMedia = (id: string, media: boolean) => { if (!drafts[id] || !!drafts[id].media === media) return; setDrafts(id, { media, updatedAt: Date.now() }); save(); emitDraftEvent({ type: "changed", id }); }; /** Something a draft holds besides its words changed, like a picture's description. */ export const touchDraft = (id: string) => { if (!drafts[id]) return; setDrafts(id, "updatedAt", Date.now()); save(); emitDraftEvent({ type: "changed", id }); }; export const updateDraft = (id: string, account: string, text: string) => { // Opening a draft sets its words to what they already were: that's no change. const before = drafts[id]; if (before && before.text === text && before.account === account) return; // What it answers, if anything, stays as it was. setDrafts(id, (draft) => ({ ...draft, account, text, updatedAt: Date.now() })); save(); emitDraftEvent({ type: "changed", id }); }; /** The posts after the first, as they are now; none left, it's a single post again. */ export const updateDraftMore = (id: string, account: string, more: ThreadPost[]) => { const before = drafts[id]?.more ?? []; if ( before.length === more.length && before.every((post, i) => post.id === more[i].id && post.text === more[i].text) ) return; setDrafts(id, (draft) => ({ ...draft, text: draft?.text ?? "", account: draft?.account ?? account, more: more.length ? more.map((post) => ({ ...post })) : undefined, updatedAt: Date.now(), })); save(); emitDraftEvent({ type: "changed", id }); }; /** * Where each post of a draft keeps its pictures and video (see draft-images): the first under * the draft's own id, as before threads; the others under theirs. */ export const postStorageId = (draft: string, post?: string) => (post ? `${draft}~${post}` : draft); /** Every place a draft keeps pictures or a video, its first post's and the rest's. */ export const storageIdsOf = (id: string) => [ id, ...(drafts[id]?.more ?? []).map((post) => postStorageId(id, post.id)), ]; export const discardDraft = (id: string) => { const before = drafts[id]; setDrafts( produce((all) => { delete all[id]; }), ); save(); if (before) emitDraftEvent({ type: "removed", id, draft: { ...before } }); }; /** Notes that a draft is on Bluesky, as it was at `syncedAt`; or no longer, with no id. */ export const setDraftRemote = (id: string, remoteId: string | undefined, syncedAt?: number) => { if (!drafts[id]) return; setDrafts(id, { remoteId, syncedAt }); save(); }; /** Every draft, by id: for matching them with Bluesky's. */ export const allDrafts = () => Object.entries(drafts).map(([id, draft]) => ({ id, ...draft })); /** * A draft found on Bluesky, written elsewhere or changed there, kept here as given: under a new * id, or over one already here. Not announced as a change: it's as on Bluesky already. */ export const keepRemoteDraft = (id: string, draft: Draft) => { setDrafts(id, draft); save(); }; /** Drafts gone from Bluesky (sent or thrown away elsewhere), gone here too, quietly. */ export const forgetDraft = (id: string) => { setDrafts( produce((all) => { delete all[id]; }), ); save(); }; /** A new draft quoting a post, as an account; its id, for its tab. */ export const startQuote = (account: string, quote: StrongRef, quoting?: string) => { const id = newDraftId(); setDrafts(id, { account, text: "", updatedAt: Date.now(), quote, quoting }); save(); emitDraftEvent({ type: "changed", id }); return id; }; /** A new draft's id: its page's address, /compose/. */ export const newDraftId = () => Date.now().toString(36) + Math.random().toString(36).slice(2, 6);