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.

2.9 KB
Raw Blame
import { Match } from "effect";
import { useSyncExternalStore } from "react";
import { PauseIcon, PlayIcon, SquareIcon, Volume2Icon } from "lucide-react";
import {
  sameReplyPlaybackIdentity,
  type ReplyPlaybackController,
  type ReplyPlaybackRequest,
} from "@akeru/client-runtime/reply-playback";

import { useI18n } from "../../i18n";
import { Button } from "../ui/button";

export interface ReplyPlaybackControlsProps {
  readonly controller: ReplyPlaybackController;
  readonly request: ReplyPlaybackRequest;
  readonly unavailableReason?: string;
  readonly disclosure?: string;
}

export function ReplyPlaybackControls({
  controller,
  request,
  unavailableReason,
  disclosure,
}: ReplyPlaybackControlsProps) {
  const { t } = useI18n();

  const snapshot = useSyncExternalStore(
    controller.subscribe,
    controller.getSnapshot,
    controller.getSnapshot,
  );

  const state =
    snapshot.status !== "idle" && sameReplyPlaybackIdentity(snapshot.identity, request.identity)
      ? snapshot.status
      : "idle";

  const label = Match.value(state).pipe(
    Match.when("loading", () => t("Preparing audio")),
    Match.when("playing", () => t("Pause readout")),
    Match.when("paused", () => t("Resume readout")),
    Match.when("error", () => t("Retry readout")),
    Match.orElse(() => t("Read aloud")),
  );

  const activate = () => {
    if (state === "playing") controller.pause();
    else if (state === "paused") void controller.resume();
    else if (state === "error") void controller.retry();
    else void controller.start(request);
  };

  return (
    <div className="flex min-w-0 flex-wrap items-center gap-1" aria-label={t("Reply playback")}>
      <Button
        aria-label={label}
        aria-busy={state === "loading"}
        title={unavailableReason ?? disclosure ?? label}
        disabled={Boolean(unavailableReason) || state === "loading"}
        size="xs"
        variant="ghost"
        onClick={activate}
      >
        {Match.value(state).pipe(
          Match.when("playing", () => <PauseIcon className="size-3.5" />),
          Match.when("paused", () => <PlayIcon className="size-3.5" />),
          Match.orElse(() => <Volume2Icon className="size-3.5" />),
        )}
        {label}
      </Button>
      {state === "loading" || state === "playing" || state === "paused" ? (
        <Button
          aria-label={t("Stop readout")}
          size="icon-xs"
          variant="ghost"
          onClick={controller.stop}
        >
          <SquareIcon className="size-3.5" />
        </Button>
      ) : null}
      {state === "error" ? (
        <span role="status" className="text-xs text-muted-foreground">
          {t("Audio could not play. Retry, or check voice settings.")}
        </span>
      ) : null}
      {disclosure ? <span className="text-xs text-muted-foreground">{disclosure}</span> : null}
      {unavailableReason ? (
        <span className="text-xs text-muted-foreground">{unavailableReason}</span>
      ) : null}
    </div>
  );
}