Merge PR 'client: discard drafts that are just the default state' (#182)

Reviewed-on: FoundKeyGang/FoundKey#182
This commit is contained in:
Norm 2022-10-03 02:53:13 -04:00
commit 0b26d96776
Signed by untrusted user: norm
GPG key ID: 7123E30E441E80DE

View file

@ -121,9 +121,9 @@ const cwInputEl = $ref<HTMLInputElement | null>(null);
const hashtagsInputEl = $ref<HTMLInputElement | null>(null); const hashtagsInputEl = $ref<HTMLInputElement | null>(null);
const visibilityButton = $ref<HTMLElement | null>(null); const visibilityButton = $ref<HTMLElement | null>(null);
let posting = $ref(false); let posting: boolean = $ref(false);
let text = $ref(props.initialText ?? ''); let text: string = $ref(props.initialText ?? '');
let files = $ref(props.initialFiles ?? []); let files: foundkey.entities.DriveFile[] = $ref(props.initialFiles ?? []);
let poll = $ref<{ let poll = $ref<{
choices: string[]; choices: string[];
multiple: boolean; multiple: boolean;
@ -502,21 +502,48 @@ function onDrop(ev): void {
//#endregion //#endregion
} }
// Save a copy of the initial data to detect whether it has been changed.
// Cloning the data to make sure there are no remaining references.
const initialDraftData = JSON.parse(JSON.stringify({
text,
useCw,
cw,
visibility,
localOnly,
files,
}));
function saveDraft() { function saveDraft() {
const draftData = JSON.parse(localStorage.getItem('drafts') || '{}'); const draftData = JSON.parse(localStorage.getItem('drafts') || '{}');
draftData[draftKey] = { if (
updatedAt: new Date(), initialDraftData.text === text
data: { && initialDraftData.useCw === useCw
text, // don't compare cw for equality if it is disabled, since it won't be sent
useCw, && (!useCw || initialDraftData.cw === cw)
cw, && initialDraftData.visibility === visibility
visibility, && initialDraftData.localOnly === localOnly
localOnly, && initialDraftData.files.each((file, i) => file.id === files[i].id)
files, // initial state is always poll == null
poll, && poll == null
}, ) {
}; // This is the same as the initial draft data, no need to save it.
// If it was saved before, delete it.
delete draftData[draftKey];
} else {
draftData[draftKey] = {
updatedAt: new Date(),
data: {
text,
useCw,
cw,
visibility,
localOnly,
files,
poll,
},
};
}
localStorage.setItem('drafts', JSON.stringify(draftData)); localStorage.setItem('drafts', JSON.stringify(draftData));
} }