Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

1.8 KB
Raw Blame
import { Predicate } from "effect";
import type { MessageId, ScopedThreadRef } from "@akeru/contracts";
import { useCallback } from "react";

import { useI18n } from "~/i18n";
import { threadEnvironment } from "../../state/threads";
import { useAtomCommand } from "../../state/use-atom-command";
import type { MessageReactionOption } from "../chat/MessageControls";
import { toastManager } from "../ui/toast";

export type MessageReactionHandler = (
  messageId: MessageId,
  current: MessageReactionOption | null,
  next: MessageReactionOption | null,
) => void;

/** A stable reaction updater for memoized rows; it only changes with the linked thread. */
export function useMessageReactionUpdater(threadRef: ScopedThreadRef | null) {
  const { t } = useI18n();

  const setMessageReaction = useAtomCommand(threadEnvironment.setMessageReaction, {
    reportFailure: false,
  });

  return useCallback<MessageReactionHandler>(
    (messageId, current, next) => {
      if (!threadRef) return;

      const dispatch = (emoji: MessageReactionOption, present: boolean) =>
        setMessageReaction({
          environmentId: threadRef.environmentId,
          input: { threadId: threadRef.threadId, messageId, emoji, present },
        });

      void (async () => {
        if (current && current !== next) {
          const removed = await dispatch(current, false);

          if (Predicate.isTagged(removed, "Failure")) {
            toastManager.add({ type: "error", title: t("Could not update reaction") });

            return;
          }
        }

        if (next) {
          const added = await dispatch(next, true);

          if (Predicate.isTagged(added, "Failure")) {
            toastManager.add({ type: "error", title: t("Could not update reaction") });
          }
        }
      })();
    },
    [setMessageReaction, t, threadRef],
  );
}