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}
/>
);
}