feat: Persist composer state in localStorage
Some checks failed
CodeQL / Analyze (javascript) (push) Failing after 0s
Deploy to GitHub Pages / build (push) Failing after 0s
Docker / build (push) Failing after 0s
Mirror to Codeberg / Mirror (push) Failing after 0s
Deploy to GitHub Pages / deploy (push) Has been skipped

This commit is contained in:
Jesse Wierzbinski 2025-08-28 08:08:14 +02:00
parent b510782a30
commit e055e2bc8f
No known key found for this signature in database
2 changed files with 35 additions and 2 deletions

View file

@ -69,7 +69,7 @@ const send = async () => {
watch( watch(
props, props,
async (props) => { async (props) => {
if (props.relation) { if (props.relation && !store.relation) {
store.stateFromRelation( store.stateFromRelation(
props.relation.type, props.relation.type,
props.relation.note, props.relation.note,

View file

@ -116,7 +116,6 @@ export const useComposerStore = (key: ComposerStateKey) =>
if (relationType === "edit") { if (relationType === "edit") {
this.content = source?.text || note.content; this.content = source?.text || note.content;
this.rawContent = source?.text || ""; this.rawContent = source?.text || "";
console.log(note.media_attachments);
this.files = await Promise.all( this.files = await Promise.all(
note.media_attachments.map(async (file) => ({ note.media_attachments.map(async (file) => ({
id: crypto.randomUUID(), id: crypto.randomUUID(),
@ -257,4 +256,38 @@ export const useComposerStore = (key: ComposerStateKey) =>
} }
}, },
}, },
persist: {
serializer: {
serialize(data) {
// Delete file references before storing to avoid large storage usage
const newFiles = (data as ComposerState).files.map((f) => {
const { file, ...rest } = f;
return rest;
});
return JSON.stringify({ ...data, files: newFiles });
},
deserialize(str) {
return JSON.parse(str);
},
},
storage: {
// Store everything in "composer" key to avoid creating too many entries
getItem(key) {
return JSON.stringify(
JSON.parse(localStorage.getItem("composer") || "{}")[
key
],
);
},
setItem(key, value) {
const composer = JSON.parse(
localStorage.getItem("composer") || "{}",
);
composer[key] = JSON.parse(value);
localStorage.setItem("composer", JSON.stringify(composer));
},
},
},
}); });