mirror of
https://github.com/zulip/zulip.git
synced 2025-11-09 00:18:12 +00:00
drafts: Don't restore drafts being actively sent or scheduled.
This commit is contained in:
@@ -35,6 +35,7 @@ const draft_schema = z.intersection(
|
||||
z.object({
|
||||
content: z.string(),
|
||||
updatedAt: z.number(),
|
||||
is_sending_saving: z.boolean().default(false),
|
||||
}),
|
||||
z.discriminatedUnion("type", [
|
||||
z.object({
|
||||
@@ -60,6 +61,7 @@ const possibly_buggy_draft_schema = z.intersection(
|
||||
z.object({
|
||||
content: z.string(),
|
||||
updatedAt: z.number(),
|
||||
is_sending_saving: z.boolean().default(false),
|
||||
}),
|
||||
z.discriminatedUnion("type", [
|
||||
z.object({
|
||||
@@ -303,6 +305,7 @@ export function snapshot_message(): LocalStorageDraft | undefined {
|
||||
type: "private",
|
||||
reply_to: recipient,
|
||||
private_message_recipient: recipient,
|
||||
is_sending_saving: false,
|
||||
};
|
||||
}
|
||||
assert(message.type === "stream");
|
||||
@@ -311,6 +314,7 @@ export function snapshot_message(): LocalStorageDraft | undefined {
|
||||
type: "stream",
|
||||
stream_id: compose_state.stream_id(),
|
||||
topic: compose_state.topic(),
|
||||
is_sending_saving: false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -375,12 +379,15 @@ function maybe_notify(no_notify: boolean): void {
|
||||
type UpdateDraftOptions = {
|
||||
no_notify?: boolean;
|
||||
update_count?: boolean;
|
||||
is_sending_saving?: boolean;
|
||||
};
|
||||
|
||||
export function update_draft(opts: UpdateDraftOptions = {}): string | undefined {
|
||||
const draft_id = $("textarea#compose-textarea").data("draft-id");
|
||||
const old_draft = draft_model.getDraft(draft_id);
|
||||
|
||||
const no_notify = opts.no_notify ?? false;
|
||||
const draft = snapshot_message();
|
||||
const draft_id = $("textarea#compose-textarea").data("draft-id");
|
||||
|
||||
if (draft === undefined) {
|
||||
// The user cleared the compose box, which means
|
||||
@@ -392,6 +399,12 @@ export function update_draft(opts: UpdateDraftOptions = {}): string | undefined
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (opts.is_sending_saving !== undefined) {
|
||||
draft.is_sending_saving = opts.is_sending_saving;
|
||||
} else {
|
||||
draft.is_sending_saving = old_draft ? old_draft.is_sending_saving : false;
|
||||
}
|
||||
|
||||
if (draft_id !== undefined) {
|
||||
// We don't save multiple drafts of the same message;
|
||||
// just update the existing draft.
|
||||
@@ -507,6 +520,7 @@ export function get_last_draft_based_on_compose_state(): LocalStorageDraftWithId
|
||||
);
|
||||
return drafts_for_compose_state
|
||||
.sort((draft_a, draft_b) => draft_a.updatedAt - draft_b.updatedAt)
|
||||
.filter((draft) => !draft.is_sending_saving)
|
||||
.pop();
|
||||
}
|
||||
|
||||
@@ -619,6 +633,20 @@ export function format_draft(draft: LocalStorageDraftWithId): FormattedDraft | u
|
||||
export function initialize(): void {
|
||||
remove_old_drafts();
|
||||
|
||||
// It's possible that drafts will get still have
|
||||
// `is_sending_saving` set to true if the page was
|
||||
// refreshed in the middle of sending a message. We
|
||||
// reset the field on page reload to ensure that drafts
|
||||
// don't get stuck in that state.
|
||||
const current_drafts = draft_model.get();
|
||||
for (const draft_id of Object.keys(current_drafts)) {
|
||||
const draft = current_drafts[draft_id];
|
||||
if (draft.is_sending_saving) {
|
||||
draft.is_sending_saving = false;
|
||||
draft_model.editDraft(draft_id, draft);
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener("beforeunload", () => {
|
||||
update_draft();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user