From a1419c1a7c1ac20873aa7a390eb9fbfc43d6eb3f Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 14 Aug 2026 08:12:17 +0000 Subject: [PATCH] Wire up recent emoji to user status emoji picker (#34710) * Wire up recent emoji to user status emoji picker So recents are saved and displayed. * Add tests --- .../status/SetStatusViewModel.test.ts | 32 +++++++++++++++++++ .../viewmodels/status/SetStatusViewModel.ts | 11 +++++++ .../src/status/CustomStatusView.test.tsx | 21 ++++++++++++ .../src/status/CustomStatusView.tsx | 23 +++++++++++-- .../src/status/SetStatusView.tsx | 16 +++++++++- 5 files changed, 99 insertions(+), 4 deletions(-) diff --git a/apps/web/src/viewmodels/status/SetStatusViewModel.test.ts b/apps/web/src/viewmodels/status/SetStatusViewModel.test.ts index 2b6ebeafbb..a2c55b3847 100644 --- a/apps/web/src/viewmodels/status/SetStatusViewModel.test.ts +++ b/apps/web/src/viewmodels/status/SetStatusViewModel.test.ts @@ -24,6 +24,12 @@ import { Action } from "../../dispatcher/actions"; import { UserTab } from "../../components/views/dialogs/UserTab"; import { OwnProfileStore } from "../../stores/OwnProfileStore"; import { UPDATE_EVENT } from "../../stores/AsyncStore"; +import * as recent from "../../emojipicker/recent"; + +vi.mock("../../emojipicker/recent", () => ({ + add: vi.fn(), + get: vi.fn(), +})); const STATUS: MatrixUserStatus = { emoji: "๐Ÿงช", text: "Testing" }; @@ -43,6 +49,7 @@ describe("SetStatusViewModel", () => { setExtendedProfileProperty: vi.fn().mockResolvedValue(undefined), }); vi.mocked(mockOwnProfileStoreInstance).userStatus = undefined; + vi.mocked(recent.get).mockReturnValue([]); }); afterEach(() => { @@ -60,6 +67,12 @@ describe("SetStatusViewModel", () => { expect(vm.getSnapshot().userStatus).toBeUndefined(); }); + it("initialises snapshot from the recently used emojis", () => { + vi.mocked(recent.get).mockReturnValue(["๐Ÿงช", "๐ŸฆŽ"]); + const vm = new SetStatusViewModel({ client, ownProfileStore: mockOwnProfileStoreInstance }); + expect(vm.getSnapshot().recentEmojis).toEqual(["๐Ÿงช", "๐ŸฆŽ"]); + }); + it("updates the snapshot when OwnProfileStore emits an update", () => { const vm = new SetStatusViewModel({ client, ownProfileStore: mockOwnProfileStoreInstance }); expect(vm.getSnapshot().userStatus).toBeUndefined(); @@ -119,6 +132,25 @@ describe("SetStatusViewModel", () => { }); }); + describe("recordRecentEmoji", () => { + it("records the emoji as recently used", () => { + const vm = new SetStatusViewModel({ client, ownProfileStore: mockOwnProfileStoreInstance }); + vm.recordRecentEmoji("๐ŸŽ‰"); + expect(recent.add).toHaveBeenCalledWith("๐ŸŽ‰"); + }); + + it("updates the snapshot with the new list of recent emojis", () => { + vi.mocked(recent.get).mockReturnValue(["๐Ÿงช"]); + const vm = new SetStatusViewModel({ client, ownProfileStore: mockOwnProfileStoreInstance }); + expect(vm.getSnapshot().recentEmojis).toEqual(["๐Ÿงช"]); + + vi.mocked(recent.get).mockReturnValue(["๐ŸŽ‰", "๐Ÿงช"]); + vm.recordRecentEmoji("๐ŸŽ‰"); + + expect(vm.getSnapshot().recentEmojis).toEqual(["๐ŸŽ‰", "๐Ÿงช"]); + }); + }); + describe("clearStatus", () => { it("optimistically clears the snapshot", () => { vi.mocked(mockOwnProfileStoreInstance).userStatus = STATUS; diff --git a/apps/web/src/viewmodels/status/SetStatusViewModel.ts b/apps/web/src/viewmodels/status/SetStatusViewModel.ts index a285dcfb51..e5a2f20edd 100644 --- a/apps/web/src/viewmodels/status/SetStatusViewModel.ts +++ b/apps/web/src/viewmodels/status/SetStatusViewModel.ts @@ -15,6 +15,7 @@ import { } from "@element-hq/web-shared-components"; import { clearAllUserStatus, setUserStatus } from "../../utils/userStatus"; +import * as recent from "../../emojipicker/recent"; import { UPDATE_EVENT } from "../../stores/AsyncStore"; import dis from "../../dispatcher/dispatcher"; import { UserTab } from "../../components/views/dialogs/UserTab"; @@ -36,6 +37,10 @@ export class SetStatusViewModel public constructor(props: SetStatusViewModelProps) { super(props, { userStatus: props.ownProfileStore.userStatus, + // This is a one-time snapshot and we manually update when we add an emoji. + // It could have a listener for when an emoji gets added but in practice, this + // would be the only way a recent could possibly get added while the view is mounted. + recentEmojis: recent.get(), }); this.disposables.trackListener(props.ownProfileStore, UPDATE_EVENT, this.onProfileStoreUpdate); @@ -55,6 +60,12 @@ export class SetStatusViewModel }); }; + public recordRecentEmoji = (unicode: string): void => { + recent.add(unicode); + // Manually update the list of recent emojis as we know we've just added one. + this.snapshot.merge({ recentEmojis: recent.get() }); + }; + public clearStatus = (): void => { const oldStatus = this.snapshot.current.userStatus; diff --git a/packages/shared-components/src/status/CustomStatusView.test.tsx b/packages/shared-components/src/status/CustomStatusView.test.tsx index c041dfdda5..f521cab9f1 100644 --- a/packages/shared-components/src/status/CustomStatusView.test.tsx +++ b/packages/shared-components/src/status/CustomStatusView.test.tsx @@ -73,4 +73,25 @@ describe("CustomStatusView", () => { expect(onSave).toHaveBeenCalledWith({ emoji: "๐Ÿ˜‡", text: "Angelic" }); }); + + it("offers the recently used emoji in the picker", async () => { + render(); + + await userEvent.click(screen.getByRole("button", { name: "Choose Emoji" })); + await waitFor(() => expect(screen.getByLabelText("Emoji picker")).toBeInTheDocument()); + + expect(await screen.findByRole("heading", { name: "Frequently Used" })).toBeInTheDocument(); + }); + + it("records the chosen emoji as recently used", async () => { + const onRecordRecentEmoji = vi.fn(); + render(); + + await userEvent.click(screen.getByRole("button", { name: "Choose Emoji" })); + await waitFor(() => expect(screen.getByLabelText("Emoji picker")).toBeInTheDocument()); + + await userEvent.click(await screen.findByText("๐Ÿ˜‡")); + + expect(onRecordRecentEmoji).toHaveBeenCalledWith("๐Ÿ˜‡"); + }); }); diff --git a/packages/shared-components/src/status/CustomStatusView.tsx b/packages/shared-components/src/status/CustomStatusView.tsx index 9616bf3792..ffd7830bf9 100644 --- a/packages/shared-components/src/status/CustomStatusView.tsx +++ b/packages/shared-components/src/status/CustomStatusView.tsx @@ -29,12 +29,28 @@ export interface CustomStatusViewProps { * "Cancel" while the text is empty. */ onCancel: () => void; + /** + * Recently used emoji (unicode strings, most relevant first) to show in the + * picker's "Frequently Used" category. The category is hidden when empty or + * omitted. + */ + recentEmojis?: string[]; + /** + * Called with the chosen emoji unicode when it should be recorded as + * recently used. + */ + onRecordRecentEmoji?: (unicode: string) => void; } /** * Editor for composing a custom user status text and choosing an emoji. */ -export function CustomStatusView({ onSave, onCancel }: CustomStatusViewProps): JSX.Element { +export function CustomStatusView({ + onSave, + onCancel, + recentEmojis, + onRecordRecentEmoji, +}: CustomStatusViewProps): JSX.Element { const [emoji, setEmoji] = useState(DEFAULT_EMOJI); const [text, setText] = useState(""); const [pickerOpen, setPickerOpen] = useState(false); @@ -44,8 +60,7 @@ export function CustomStatusView({ onSave, onCancel }: CustomStatusViewProps): J const onChooseEmoji = useCallback((unicode: string): boolean => { setEmoji(unicode); setPickerOpen(false); - // Don't record custom-status emoji as recently used composer reactions. - return false; + return true; }, []); const commit = useCallback(() => { @@ -99,6 +114,8 @@ export function CustomStatusView({ onSave, onCancel }: CustomStatusViewProps): J > setPickerOpen(false)} showQuickReactions={false} /> diff --git a/packages/shared-components/src/status/SetStatusView.tsx b/packages/shared-components/src/status/SetStatusView.tsx index d87b84dc09..31e1be13b7 100644 --- a/packages/shared-components/src/status/SetStatusView.tsx +++ b/packages/shared-components/src/status/SetStatusView.tsx @@ -32,6 +32,12 @@ export interface SetStatusViewSnapshot { * The current user status, or undefined if no status is set. */ userStatus?: UserStatus; + + /** + * Recently used emoji (unicode strings, most relevant first) to offer when + * choosing an emoji for a custom status. + */ + recentEmojis?: string[]; } export interface SetStatusViewActions { @@ -51,6 +57,12 @@ export interface SetStatusViewActions { * Called when the user clears their current status. */ clearStatus: () => void; + + /** + * Called with the unicode of an emoji the user picked for a custom status, + * so it can be recorded as recently used. + */ + recordRecentEmoji?: (unicode: string) => void; } export type SetStatusViewModel = ViewModel; @@ -69,7 +81,7 @@ function StatusOption({ value }: { value: StatusValue }): React.ReactNode { } export function SetStatusView({ vm }: SetStatusViewProps): JSX.Element { - const { userStatus } = useViewModel(vm); + const { userStatus, recentEmojis } = useViewModel(vm); const [customMode, setCustomMode] = useState(false); const renderItem = useCallback((value: StatusValue | null): React.ReactNode => { @@ -90,6 +102,8 @@ export function SetStatusView({ vm }: SetStatusViewProps): JSX.Element { vm.setStatus(status); }} onCancel={() => setCustomMode(false)} + recentEmojis={recentEmojis} + onRecordRecentEmoji={vm.recordRecentEmoji} /> ); }