Skip to content

Commit f50a9d0

Browse files
committed
feat: keep draft id in the message composer state
1 parent 9e1ef60 commit f50a9d0

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/messageComposer/messageComposer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export type MessageComposerState = {
4040
id: string;
4141
quotedMessage: LocalMessageBase | null;
4242
pollId: string | null;
43+
draftId: string | null;
4344
};
4445

4546
export type MessageComposerConfig = {
@@ -82,18 +83,22 @@ const initState = (
8283
id: MessageComposer.generateId(),
8384
quotedMessage: null,
8485
pollId: null,
86+
draftId: null,
8587
};
8688
}
8789

8890
const quotedMessage = composition.quoted_message;
8991
let message;
92+
let draftId = null;
9093
if (compositionIsMessageDraft(composition)) {
9194
message = composition.message;
95+
draftId = composition.message.id;
9296
} else {
9397
message = composition;
9498
}
9599

96100
return {
101+
draftId,
97102
id: message.id,
98103
quotedMessage: quotedMessage
99104
? formatMessage(quotedMessage as MessageResponseBase)
@@ -180,6 +185,11 @@ export class MessageComposer {
180185
get id() {
181186
return this.state.getLatestValue().id;
182187
}
188+
189+
get draftId() {
190+
return this.state.getLatestValue().draftId;
191+
}
192+
183193
get lastChange() {
184194
return this.editingAuditState.getLatestValue().lastChange;
185195
}
@@ -539,12 +549,14 @@ export class MessageComposer {
539549
const composition = await this.composeDraft();
540550
if (!composition) return;
541551
const { draft } = composition;
552+
this.state.partialNext({ draftId: draft.id });
542553
this.logDraftUpdateTimestamp();
543554
await this.channel.createDraft(draft);
544555
};
545556

546557
deleteDraft = async () => {
547-
if (this.editedMessage || !this.config.draftsEnabled) return;
558+
if (this.editedMessage || !this.config.draftsEnabled || !this.draftId) return;
559+
this.state.partialNext({ draftId: null }); // todo: should we clear the whole state?
548560
this.logDraftUpdateTimestamp();
549561
await this.channel.deleteDraft({ parent_id: this.threadId ?? undefined });
550562
};

0 commit comments

Comments
 (0)