Chat drawer: native Matrix client on matrix-js-sdk instead of the Element iframe
Problem and intended result
The chat drawer embeds Element Web (apps/blog/components/matrix-chat-widget.tsx). Element is a full client:
- a 5.1 MB cold load, including a 2 MB rust-crypto wasm it always starts, though E2EE is off for denser's rooms (
io.element.e2ee.force_disable); - its own UI and theme inside an iframe;
- no unread count posted to denser, so there's no badge.
The first open takes 5.5–10 s cold (#1307 (closed)). The warm-up from #1314 (closed) and the login token from #1315 (closed) shorten this, but the download and crypto start stay.
Intended result: a chat drawer built into denser on matrix-js-sdk, with denser's own UI (@hive/ui), theme and translations. Its code loads only when the drawer warms or opens, it uses no crypto, and it shows an unread badge. Element stays available as "Open full chat" for everything the drawer doesn't do.
Scope
- Setting
MATRIX_CHAT_CLIENT(public env,packages/ui/config/chat.ts):native(default) orelement(today's iframe, kept as is). - Client:
matrix-js-sdkin a lazily imported chunk, loaded by the warm-up trigger of #1314 (closed) or on opening;- no crypto: don't call
initRustCrypto, and don't bundle the wasm (check the chunk contents); - sign in with the login token from #1315 (closed) (
m.login.token), with a stabledevice_idper browser so repeat sign-ins don't pile up devices; - keep the access token in memory, or in sessionStorage through the TTL utilities, never in plain localStorage;
- sync with a lazy-loading members filter and a small timeline limit.
- Drawer UI:
- opens in the default room (
defaultRoom), with a compact list of the user's joined rooms to switch between; - timeline with sender display name and avatar (Synapse media thumbnails), time and plain text;
- links are linkified and safe (
rel="noopener noreferrer", no HTML from messages other than what a sanitizer allows forformatted_body, or plainbodyonly); - scroll back loads older messages (
/messagespagination); - a composer sends
m.text(Enter sends, Shift+Enter makes a new line), with sending and failed states and retry; - new messages arrive live while the drawer is open.
- opens in the default room (
- Unread count on the chat button, from the sync's notification counts. It clears as the room is read; send read receipts.
- Encrypted rooms, or event types the drawer can't show, get a one-line placeholder with "Open full chat".
- Sign-out, account switch and session end stop the client, drop its state and delete its device, so the existing server-side device cleanup still covers anything left.
- Every user-visible string is translated in all blog locales. The drawer follows denser's light and dark themes and is keyboard and screen-reader usable.
- Files under about 300 lines each: client hook, room list, timeline, composer, message item, drawer container.
Verification
- Unit tests: message-to-view mapping (text, formatted/sanitized, unknown and encrypted placeholder), unread count from sync, pagination state, composer send and fail.
- Fixture specs with a stubbed homeserver: login token → login; first sync shows
#generalmessages; sending appends the message; scroll back loads older ones; the badge shows unread and clears on open;MATRIX_CHAT_CLIENT=elementkeeps the iframe; sign-out stops the client. - Bundle check: the drawer chunk is lazy (not in the first-load JS of
/trending) and contains no.wasm. - After landing, the operator measures the first open live (target under 1 s after warm-up, and under 2 s cold) and tests send and receive between two users.