Skip to content

Commit 900178b

Browse files
Apply post-review adjustments
Co-authored-by: Martin Cupela <martin.cupela@gmail.com>
1 parent 18d62f3 commit 900178b

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

src/messageComposer/configuration/types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@ export type AttachmentManagerConfig = {
4141
/** Function that allows to customize the upload request. */
4242
doUploadRequest?: UploadRequestFn;
4343
};
44-
export type LinkPreviewConfig = {
45-
/** Custom function to react to link preview dismissal */
46-
onLinkPreviewDismissed?: (linkPreview: LinkPreview) => void;
47-
};
48-
export type LinkPreviewsManagerConfig = LinkPreviewConfig & {
44+
45+
export type LinkPreviewsManagerConfig = {
4946
/** Number of milliseconds to debounce firing the URL enrichment queries when typing. The default value is 1500(ms). */
5047
debounceURLEnrichmentMs: number;
5148
/** Allows for toggling the URL enrichment and link previews in `MessageInput`. By default, the feature is disabled. */
5249
enabled: boolean;
5350
/** Custom function to identify URLs in a string and request OG data */
5451
findURLFn: (text: string) => string[];
52+
/** Custom function to react to link preview dismissal */
53+
onLinkPreviewDismissed?: (linkPreview: LinkPreview) => void;
5554
};
5655

5756
export type MessageComposerConfig = {

src/messageComposer/messageComposer.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,19 +543,40 @@ export class MessageComposer extends WithSubscriptions {
543543
}
544544
});
545545

546-
private subscribeMessageComposerConfigStateChanged = () =>
547-
this.configState.subscribeWithSelector(
548-
({ text }) => [text] as const,
549-
([currentText]) => {
550-
if (this.textComposer.text === '' && currentText.defaultValue) {
546+
private subscribeMessageComposerConfigStateChanged = () => {
547+
let draftUnsubscribeFunctions: Unsubscribe[] | null;
548+
549+
const unsubscribe = this.configState.subscribeWithSelector(
550+
(currentValue) => ({
551+
textDefaultValue: currentValue.text.defaultValue,
552+
draftsEnabled: currentValue.drafts.enabled,
553+
}),
554+
({ textDefaultValue, draftsEnabled }) => {
555+
if (this.textComposer.text === '' && textDefaultValue) {
551556
this.textComposer.insertText({
552-
text: currentText.defaultValue,
557+
text: textDefaultValue,
553558
selection: { start: 0, end: 0 },
554559
});
555560
}
561+
562+
if (draftsEnabled && !draftUnsubscribeFunctions) {
563+
draftUnsubscribeFunctions = [
564+
this.subscribeDraftUpdated(),
565+
this.subscribeDraftDeleted(),
566+
];
567+
} else if (!draftsEnabled && draftUnsubscribeFunctions) {
568+
draftUnsubscribeFunctions.forEach((fn) => fn());
569+
draftUnsubscribeFunctions = null;
570+
}
556571
},
557572
);
558573

574+
return () => {
575+
draftUnsubscribeFunctions?.forEach((unsubscribe) => unsubscribe());
576+
unsubscribe();
577+
};
578+
};
579+
559580
setQuotedMessage = (quotedMessage: LocalMessage | null) => {
560581
this.state.partialNext({ quotedMessage });
561582
};

test/unit/MessageComposer/messageComposer.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,5 +1053,43 @@ describe('MessageComposer', () => {
10531053
spy.mockRestore();
10541054
});
10551055
});
1056+
1057+
it('should toggle the registration of draft WS event subscriptions when drafts are disabled / enabled', () => {
1058+
const { messageComposer } = setup({
1059+
config: { drafts: { enabled: false } },
1060+
});
1061+
1062+
const unsubscribeDraftUpdated = vi.fn();
1063+
const unsubscribeDraftDeleted = vi.fn();
1064+
1065+
// @ts-expect-error - we are testing private properties
1066+
const subscribeDraftUpdatedSpy = vi
1067+
.spyOn(messageComposer, 'subscribeDraftUpdated')
1068+
.mockImplementation(() => unsubscribeDraftUpdated);
1069+
// @ts-expect-error - we are testing private properties
1070+
const subscribeDraftDeletedSpy = vi
1071+
.spyOn(messageComposer, 'subscribeDraftDeleted')
1072+
.mockImplementation(() => unsubscribeDraftDeleted);
1073+
1074+
messageComposer.registerSubscriptions();
1075+
1076+
expect(subscribeDraftUpdatedSpy).not.toHaveBeenCalled();
1077+
expect(subscribeDraftDeletedSpy).not.toHaveBeenCalled();
1078+
1079+
messageComposer.updateConfig({ drafts: { enabled: true } });
1080+
1081+
expect(subscribeDraftUpdatedSpy).toHaveBeenCalledTimes(1);
1082+
expect(subscribeDraftDeletedSpy).toHaveBeenCalledTimes(1);
1083+
1084+
subscribeDraftUpdatedSpy.mockClear();
1085+
subscribeDraftDeletedSpy.mockClear();
1086+
1087+
messageComposer.updateConfig({ drafts: { enabled: false } });
1088+
1089+
expect(unsubscribeDraftUpdated).toHaveBeenCalledTimes(1);
1090+
expect(unsubscribeDraftDeleted).toHaveBeenCalledTimes(1);
1091+
expect(subscribeDraftUpdatedSpy).not.toHaveBeenCalled();
1092+
expect(subscribeDraftDeletedSpy).not.toHaveBeenCalled();
1093+
});
10561094
});
10571095
});

0 commit comments

Comments
 (0)