diff --git a/web/src/api/client.ts b/web/src/api/client.ts index f64973e..5b9e8fe 100644 --- a/web/src/api/client.ts +++ b/web/src/api/client.ts @@ -116,6 +116,8 @@ export default class Client { this.store.applySendComplete(ev.data) } else if (ev.command === "image_auth_token") { this.store.imageAuthToken = ev.data + } else if (ev.command === "typing") { + this.store.applyTyping(ev.data) } } diff --git a/web/src/api/statestore/hooks.ts b/web/src/api/statestore/hooks.ts index d2763b3..61d80e6 100644 --- a/web/src/api/statestore/hooks.ts +++ b/web/src/api/statestore/hooks.ts @@ -27,6 +27,10 @@ export function useRoomTimeline(room: RoomStateStore): (MemDBEvent | null)[] { ) } +export function useRoomTyping(room: RoomStateStore): string[] { + return useSyncExternalStore(room.typingSub.subscribe, () => room.typing) +} + export function useRoomState( room?: RoomStateStore, type?: EventType, stateKey: string | undefined = "", ): MemDBEvent | null { diff --git a/web/src/api/statestore/main.ts b/web/src/api/statestore/main.ts index 08b63a9..548f3ca 100644 --- a/web/src/api/statestore/main.ts +++ b/web/src/api/statestore/main.ts @@ -32,6 +32,7 @@ import { SendCompleteData, SyncCompleteData, SyncRoom, + TypingEventData, UnknownEventContent, UserID, roomStateGUIDToString, @@ -382,6 +383,15 @@ export class StateStore { } } + applyTyping(typing: TypingEventData) { + const room = this.rooms.get(typing.room_id) + if (!room) { + // TODO log or something? + return + } + room.applyTyping(typing.user_ids) + } + doGarbageCollection() { const maxLastOpened = Date.now() - window.gcSettings.lastOpenedCutoff let deletedEvents = 0 diff --git a/web/src/api/statestore/room.ts b/web/src/api/statestore/room.ts index 70d1f59..86c65d2 100644 --- a/web/src/api/statestore/room.ts +++ b/web/src/api/statestore/room.ts @@ -90,10 +90,12 @@ export class RoomStateStore { timelineCache: (MemDBEvent | null)[] = [] state: Map> = new Map() stateLoaded = false + typing: UserID[] = [] fullMembersLoaded = false readonly eventsByRowID: Map = new Map() readonly eventsByID: Map = new Map() readonly timelineSub = new Subscribable() + readonly typingSub = new Subscribable() readonly stateSubs = new MultiSubscribable() readonly eventSubs = new MultiSubscribable() readonly requestedEvents: Set = new Set() @@ -418,6 +420,11 @@ export class RoomStateStore { } } + applyTyping(users: string[]) { + this.typing = users + this.typingSub.notify() + } + doGarbageCollection() { const memberEventsToKeep = new Set() const eventsToKeep = new Set() diff --git a/web/src/ui/composer/TypingNotifications.css b/web/src/ui/composer/TypingNotifications.css new file mode 100644 index 0000000..fdac12b --- /dev/null +++ b/web/src/ui/composer/TypingNotifications.css @@ -0,0 +1,19 @@ +div.typing-notifications { + grid-area: typing; + height: 1.6rem; + margin: 0 1.6rem; + display: flex; + gap: 0.5rem; + align-items: center; + font-size: 0.9rem; + + > div.avatars { + display: flex; + align-items: center; + margin-left: 0.35rem; + + > img { + margin-left: -0.35rem; + } + } +} diff --git a/web/src/ui/composer/TypingNotifications.tsx b/web/src/ui/composer/TypingNotifications.tsx new file mode 100644 index 0000000..499f94f --- /dev/null +++ b/web/src/ui/composer/TypingNotifications.tsx @@ -0,0 +1,71 @@ +// gomuks - A Matrix client written in Go. +// Copyright (C) 2024 Sumner Evans +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +import { JSX, use } from "react" +import { PulseLoader } from "react-spinners" +import { getAvatarURL } from "@/api/media.ts" +import { useRoomTyping } from "@/api/statestore" +import { MemberEventContent } from "@/api/types/mxtypes.ts" +import { oxfordHumanJoinReact } from "@/util/reactjoin.tsx" +import ClientContext from "../ClientContext.ts" +import { useRoomContext } from "../roomview/roomcontext.ts" +import "./TypingNotifications.css" + +const TypingNotifications = () => { + const roomCtx = useRoomContext() + const client = use(ClientContext)! + const room = roomCtx.store + const typing = useRoomTyping(room).filter(u => u !== client.userID) + let loader: JSX.Element | null = null + if (typing.length > 0) { + loader = + } + const avatars: JSX.Element[] = [] + const memberNames: string[] = [] + for (let i = 0; i < 5 && i < typing.length; i++) { + const sender = typing[i] + const memberEvt = room.getStateEvent("m.room.member", sender) + const member = (memberEvt?.content ?? null) as MemberEventContent | null + if (!memberEvt) { + use(ClientContext)?.requestMemberEvent(room, sender) + } + avatars.push() + memberNames.push(member?.displayname ?? sender) + } + + let description: JSX.Element | null = null + if (typing.length > 4) { + description =
{typing.length} users are typing
+ } else if (typing.length > 0) { + description =
+ {oxfordHumanJoinReact(memberNames)} + {typing.length === 1 ? " is " : " are "} + typing +
+ } + + return
+
{avatars}
+ {description} + {loader} +
+} + +export default TypingNotifications diff --git a/web/src/ui/roomview/RoomView.css b/web/src/ui/roomview/RoomView.css index 9bc1efd..3f696f9 100644 --- a/web/src/ui/roomview/RoomView.css +++ b/web/src/ui/roomview/RoomView.css @@ -9,6 +9,7 @@ div.room-view { "messageview" 1fr "autocomplete" 0 "input" auto + "typing" auto / 1fr; contain: strict; } diff --git a/web/src/ui/roomview/RoomView.tsx b/web/src/ui/roomview/RoomView.tsx index 47398cc..fef442a 100644 --- a/web/src/ui/roomview/RoomView.tsx +++ b/web/src/ui/roomview/RoomView.tsx @@ -16,6 +16,7 @@ import { JSX, useRef } from "react" import { RoomStateStore } from "@/api/statestore" import MessageComposer from "../composer/MessageComposer.tsx" +import TypingNotifications from "../composer/TypingNotifications.tsx" import RightPanel, { RightPanelProps } from "../rightpanel/RightPanel.tsx" import TimelineView from "../timeline/TimelineView.tsx" import RoomViewHeader from "./RoomViewHeader.tsx" @@ -38,6 +39,7 @@ const RoomView = ({ room, rightPanelResizeHandle, rightPanel }: RoomViewProps) = + {rightPanelResizeHandle} {rightPanel && }