Skip to content

Commit 1a84125

Browse files
authored
Fix module composer upload options appearing twice (#34920)
* Fix composer upload options appearing twice. * Cleanup * reinstate type hack
1 parent 064616b commit 1a84125

2 files changed

Lines changed: 13 additions & 17 deletions

File tree

apps/web/src/viewmodels/room/RoomUploadViewModel.test.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ describe("RoomUploadViewModel", () => {
8181
icon,
8282
onSelected,
8383
});
84-
expect(vm.getSnapshot().options).toContainEqual({ type: "org.example.test", label: "My uploader", icon });
84+
// The module option must appear exactly once, alongside the built-in local option.
85+
expect(vm.getSnapshot().options).toEqual([
86+
expect.objectContaining({ type: "local" }),
87+
{ type: "org.example.test", label: "My uploader", icon },
88+
]);
8589
vm.onUploadOptionSelected("org.example.test");
8690
expect(onSelected).toHaveBeenCalledWith(
8791
room.roomId,

apps/web/src/viewmodels/room/RoomUploadViewModel.tsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,13 @@ export class RoomUploadViewModel
7575
},
7676
);
7777
// Initial check.
78-
this.onRoomCurrentStateUpdated();
78+
this.updateOptions();
7979
// Configure upload functions
8080
for (const option of moduleComposerApi.fileUploadOptions) {
8181
this.uploadSelectFns.set(option.type, option.onSelected);
8282
}
8383
this.uploadSelectFns.set("local", this.openUploadDialog);
84-
room.on(RoomEvent.CurrentStateUpdated, this.onRoomCurrentStateUpdated);
8584
this.disposables.trackListener(room, RoomEvent.CurrentStateUpdated, this.onRoomCurrentStateUpdated);
86-
87-
moduleComposerApi.on(ModuleComposerApiEvents.UploaderOptionsChanged, this.onUploaderOptionsChanged);
8885
this.disposables.trackListener(
8986
moduleComposerApi,
9087
ModuleComposerApiEvents.UploaderOptionsChanged,
@@ -94,8 +91,12 @@ export class RoomUploadViewModel
9491
}
9592

9693
private onRoomCurrentStateUpdated = (): void => {
94+
this.updateOptions();
95+
};
96+
97+
private updateOptions(): void {
9798
const maySendMessage = this.room.maySendMessage();
98-
this.snapshot.merge({
99+
this.snapshot.set({
99100
mayDragAndDropFile: maySendMessage,
100101
options: maySendMessage
101102
? [
@@ -112,20 +113,11 @@ export class RoomUploadViewModel
112113
]
113114
: [],
114115
});
115-
};
116+
}
116117

117118
private readonly onUploaderOptionsChanged = (option: ComposerApiFileUploadOption): void => {
118119
this.uploadSelectFns.set(option.type, option.onSelected);
119-
this.snapshot.merge({
120-
options: [
121-
...this.snapshot.current.options,
122-
{
123-
type: option.type,
124-
label: option.label,
125-
icon: option.icon,
126-
},
127-
],
128-
});
120+
this.updateOptions();
129121
};
130122

131123
public setReplyToEvent = (replyToEvent?: MatrixEvent): void => {

0 commit comments

Comments
 (0)