client: discard drafts that are just the default state

Changelog: Changed
This commit is contained in:
Johann150 2022-10-02 15:52:39 +02:00
parent be19ea610f
commit 2e07477398
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1

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,9 +502,43 @@ 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,
poll,
}));
function saveDraft() { function saveDraft() {
const draftData = JSON.parse(localStorage.getItem('drafts') || '{}'); const draftData = JSON.parse(localStorage.getItem('drafts') || '{}');
if (
initialDraftData.text === text
&& initialDraftData.useCw === useCw
// don't compare cw for equality if it is disabled, since it won't be sent
&& (!useCw || initialDraftData.cw === cw)
&& initialDraftData.visibility === visibility
&& initialDraftData.localOnly === localOnly
&& initialDraftData.files.each((file, i) => file.id === files[i].id)
&& (
(initialDraftData.poll == null && poll == null)
|| (
initialDraftData.poll.choices.each((choice, i) => choice === choices[i])
&& initialDraftData.poll.multiple === poll.multiple
&& initialDraftData.poll.expiresAt === poll.expiresAt
&& initialDraftData.poll.expiredAfter === poll.expiredAfter
)
)
) {
// 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] = { draftData[draftKey] = {
updatedAt: new Date(), updatedAt: new Date(),
data: { data: {
@ -517,6 +551,7 @@ function saveDraft() {
poll, poll,
}, },
}; };
}
localStorage.setItem('drafts', JSON.stringify(draftData)); localStorage.setItem('drafts', JSON.stringify(draftData));
} }