| 14 | import { name as ModuleName } from "../package.json"; |
| 15 | |
| 16 | class WidgetToggleModule implements Module { |
| 17 | public static readonly moduleApiVersion = "^1.12.0"; |
| 18 | private config?: WidgetTogglesConfig; |
| 19 | |
| 20 | public constructor(private api: Api) {} |
| 21 | |
| 22 | public async load(): Promise<void> { |
| 23 | const rawConfig = this.api.config.get(CONFIG_KEY); |
| 24 | if (!rawConfig) { |
| 25 | console.debug(`No configuration found for module "${ModuleName}", skipping initialization.`); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | try { |
| 30 | this.config = WidgetTogglesConfig.parse(rawConfig); |
| 31 | } catch (e) { |
| 32 | console.error("Failed to init module", e); |
| 33 | throw new Error(`Errors in module configuration for widget toggles module`); |
| 34 | } |
| 35 | |
| 36 | this.api.extras.addRoomHeaderButtonCallback((roomId: string) => { |
| 37 | const widgets = this.api.widget.getWidgetsInRoom(roomId); |
| 38 | const toggleElements: JSX.Element[] = []; |
| 39 | |
| 40 | for (const widget of widgets) { |
| 41 | if (this.config?.types.includes(widget.type)) { |
| 42 | toggleElements.push( |
| 43 | <WidgetToggle |
| 44 | app={widget} |
| 45 | roomId={roomId} |
| 46 | widgetApi={this.api.widget} |
| 47 | i18nApi={this.api.i18n} |
| 48 | />, |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (toggleElements.length === 0) return undefined; |
| 54 | |
| 55 | // XXX: We shouldn't have to add another TooltipProvider here, it should |
| 56 | // be using the one in MatrixChat in Element Web, but thanks to the fact |
| 57 | // that contexts are "magically" identified by class, it doesn't pick up the |
| 58 | // context because we use a different copy of compound-web. We'll probably |
| 59 | // need to fix this at some point, possibly by moving compound's tooltip stuff |
| 60 | // out to its own mini-module that can be provided at runtime by Element Web, |
| 61 | // unless React make contexts more sensible. |
| 62 | // Annoyingly this does actually cause the tooltips to behave a bit weirdly: |
| 63 | // there's a delay before the tooltip appears when yo move between these buttons |
| 64 | // and the rest of the header buttons, whereas moving between the other header |
| 65 | // buttons, the tooltips appear straight away once one has appeared. |
| 66 | return <TooltipProvider>{toggleElements}</TooltipProvider>; |
| 67 | }); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | export default WidgetToggleModule satisfies ModuleFactory; |
nothing calls this directly
no outgoing calls
no test coverage detected