Skip to content

Commit e155022

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

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
@@ -546,19 +546,40 @@ export class MessageComposer extends WithSubscriptions {
546546
}
547547
});
548548

549-
private subscribeMessageComposerConfigStateChanged = () =>
550-
this.configState.subscribeWithSelector(
551-
({ text }) => [text] as const,
552-
([currentText]) => {
553-
if (this.textComposer.text === '' && currentText.defaultValue) {
549+
private subscribeMessageComposerConfigStateChanged = () => {
550+
let draftUnsubscribeFunctions: Unsubscribe[] | null;
551+
552+
const unsubscribe = this.configState.subscribeWithSelector(
553+
(currentValue) => ({
554+
textDefaultValue: currentValue.text.defaultValue,
555+
draftsEnabled: currentValue.drafts.enabled,
556+
}),
557+
({ textDefaultValue, draftsEnabled }) => {
558+
if (this.textComposer.text === '' && textDefaultValue) {
554559
this.textComposer.insertText({
555-
text: currentText.defaultValue,
560+
text: textDefaultValue,
556561
selection: { start: 0, end: 0 },
557562
});
558563
}
564+
565+
if (draftsEnabled && !draftUnsubscribeFunctions) {
566+
draftUnsubscribeFunctions = [
567+
this.subscribeDraftUpdated(),
568+
this.subscribeDraftDeleted(),
569+
];
570+
} else if (!draftsEnabled && draftUnsubscribeFunctions) {
571+
draftUnsubscribeFunctions.forEach((fn) => fn());
572+
draftUnsubscribeFunctions = null;
573+
}
559574
},
560575
);
561576

577+
return () => {
578+
draftUnsubscribeFunctions?.forEach((unsubscribe) => unsubscribe());
579+
unsubscribe();
580+
};
581+
};
582+
562583
setQuotedMessage = (quotedMessage: LocalMessage | null) => {
563584
this.state.partialNext({ quotedMessage });
564585
};

test/unit/MessageComposer/messageComposer.test.ts

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

0 commit comments

Comments
 (0)