Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
11.0 KB
Raw Blame
"use client";

// wecom-scan-install-dialog.tsx — the QR half of connecting a WeCom bot.
//
// Structure follows LarkInstallDialog, which solves the same problem: a bounded,
// one-shot polled session whose whole state collapses when the dialog closes.
// The shape is deliberately not useQuery — TanStack's refetch heuristics (window
// focus, online/offline, retry backoff) are wrong for a session with its own
// server-driven cadence and expiry.
//
// One difference from Lark worth knowing: WeCom's begin does NOT return the QR.
// It returns a session in `creating` and the install worker fetches the QR out of
// band, so this dialog renders a "preparing" state first and the QR appears on a
// later poll once the status turns `pending`.

import { useCallback, useEffect, useRef, useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
// Named import: the default export is an object, not a component, and using it
// throws "Element type is invalid" the moment the QR mounts.
import { QRCode } from "react-qr-code";
import { Button } from "@multica/ui/components/ui/button";
import { DialogFooter } from "@multica/ui/components/ui/dialog";
import { api, ApiError, errorCode } from "@multica/core/api";
import { createSafeId } from "@multica/core/utils";
import { wecomKeys } from "@multica/core/wecom";
import { useT } from "../../i18n";

// Terminal error reasons this dialog renders copy for. Anything else falls back
// to the generic message, so a new backend reason never renders blank.
//
// Every line the admin reads comes from here, in their language. The server's
// English sentences are never shown: a session's stored detail is for the
// operator, and a refused begin is told apart by its code.
const SCAN_ERROR_KEYS = [
  "expired",
  "generate_failed",
  "integration_unconfigured",
  "installation_conflict",
  "bind_failed",
  "wecom_protocol_error",
  "internal_error",
  "session_lost",
  "already_connected",
  "rate_limited",
] as const;

type ScanErrorReason = (typeof SCAN_ERROR_KEYS)[number];

// The begin refusals the server gives a code to, and the reason each one is
// shown as.
const BEGIN_ERROR_REASONS: Record<string, ScanErrorReason> = {
  wecom_agent_already_connected: "already_connected",
  wecom_scan_install_rate_limited: "rate_limited",
  wecom_scan_install_unavailable: "integration_unconfigured",
};

function isKnownScanError(reason: string | null): reason is ScanErrorReason {
  return (
    reason !== null && (SCAN_ERROR_KEYS as readonly string[]).includes(reason)
  );
}

export function WecomChannelAuthorization({
  wsId,
  agentId,
  onClose,
}: {
  wsId: string;
  agentId: string;
  onClose: () => void;
}) {
  const { t } = useT("settings");
  const qc = useQueryClient();

  const [sessionId, setSessionId] = useState<string | null>(null);
  const [pollIntervalSeconds, setPollIntervalSeconds] = useState(1);
  const [qrCodeURL, setQrCodeURL] = useState<string | null>(null);
  const [expiresInSeconds, setExpiresInSeconds] = useState<number | null>(null);
  const [status, setStatus] = useState<string>("creating");
  const [errorReason, setErrorReason] = useState<string | null>(null);
  const [beginning, setBeginning] = useState(false);
  const closedRef = useRef(false);

  const beginSession = useCallback(async () => {
    setBeginning(true);
    setStatus("creating");
    setErrorReason(null);
    setSessionId(null);
    setQrCodeURL(null);
    setExpiresInSeconds(null);
    try {
      // A fresh key per attempt: reusing one would make "get a new code" replay
      // the previous session — including an expired one — instead of starting
      // over. Within a single attempt the key is stable, so a retried or
      // double-submitted POST still collapses onto one session.
      //
      // createSafeId, not crypto.randomUUID: that one is SecureContext-gated and
      // simply absent over plain HTTP, where it threw in here and turned every
      // scan install on a self-hosted http:// deployment into "something went
      // wrong" without a request ever leaving the browser.
      const idempotencyKey = createSafeId();
      const res = await api.beginWecomInstall(wsId, agentId, idempotencyKey);
      if (closedRef.current) return;
      if (!res.session_id) {
        setStatus("error");
        setErrorReason("internal_error");
        return;
      }
      setSessionId(res.session_id);
      setStatus(res.status);
      setPollIntervalSeconds(res.poll_interval_seconds);
    } catch (e) {
      if (closedRef.current) return;
      setStatus("error");
      // A server that predates the codes still says "not enabled" with a 503.
      const code = errorCode(e);
      setErrorReason(
        (code && BEGIN_ERROR_REASONS[code]) ??
          (e instanceof ApiError && e.status === 503
            ? "integration_unconfigured"
            : "internal_error"),
      );
    } finally {
      if (!closedRef.current) setBeginning(false);
    }
  }, [wsId, agentId]);

  // Kick off on mount.
  //
  // closedRef is reset AT THE START of every mount, not only at construction:
  // StrictMode runs effects twice (mount → cleanup → mount) on the same
  // instance, so the ref survives. Without the reset, mount #1's cleanup sets
  // closedRef=true and every await in mount #2 early-exits before setting state
  // — the dialog then renders permanently empty.
  useEffect(() => {
    closedRef.current = false;
    void beginSession();
    return () => {
      closedRef.current = true;
    };
  }, [beginSession]);

  // Polling loop. Runs while the session is not terminal — through `creating`
  // too, since that is when the QR is still being fetched.
  useEffect(() => {
    if (!sessionId) return;
    if (status !== "creating" && status !== "pending") return;

    const intervalMs = Math.max(1000, pollIntervalSeconds * 1000);
    let cancelled = false;
    let timer: ReturnType<typeof setTimeout> | null = null;

    const poll = async () => {
      if (cancelled) return;
      try {
        const res = await api.getWecomInstallStatus(wsId, sessionId);
        if (cancelled) return;
        setStatus(res.status);
        setPollIntervalSeconds(res.poll_interval_seconds);
        if (res.qr_code_url) setQrCodeURL(res.qr_code_url);
        if (typeof res.expires_in_seconds === "number") {
          setExpiresInSeconds(res.expires_in_seconds);
        }
        if (res.status === "success") {
          await qc.invalidateQueries({
            queryKey: wecomKeys.installations(wsId),
          });
          toast.success(t(($) => $.channels.connect_success));
          // The close itself is deliberately NOT scheduled here: `status` is a
          // dependency of this effect, so setting it to "success" re-runs the
          // effect and the cleanup would cancel the very timer meant to close
          // the dialog. A dedicated effect below owns that.
          return;
        }
        if (res.status === "error") {
          setErrorReason(res.error_reason ?? "internal_error");
          return;
        }
        timer = setTimeout(poll, intervalMs);
      } catch (e) {
        if (cancelled) return;
        // Terminal HTTP states must not be retried: the session is gone or the
        // caller lost permission, and polling on would trap the user watching a
        // stale QR with no feedback.
        if (e instanceof ApiError) {
          // 404: purged by the retention sweep, or never existed.
          if (e.status === 404 || e.status === 401 || e.status === 403) {
            setStatus("error");
            setErrorReason("session_lost");
            return;
          }
        }
        // A blip or 5xx: poll again. The next read either confirms the session
        // or surfaces the terminal reason the worker recorded.
        timer = setTimeout(poll, intervalMs);
      }
    };

    timer = setTimeout(poll, intervalMs);
    return () => {
      cancelled = true;
      if (timer) clearTimeout(timer);
    };
  }, [wsId, sessionId, status, pollIntervalSeconds, qc, t, onClose]);

  // Close shortly after success so the success state is visible for a beat.
  // Separate from the poll effect for the reason noted above.
  useEffect(() => {
    if (status !== "success") return;
    const timer = setTimeout(onClose, 800);
    return () => clearTimeout(timer);
  }, [status, onClose]);

  const scanErrorText = isKnownScanError(errorReason)
    ? {
        expired: t(($) => $.wecom.scan_error_expired),
        generate_failed: t(($) => $.wecom.scan_error_generate_failed),
        integration_unconfigured: t(
          ($) => $.wecom.scan_error_integration_unconfigured,
        ),
        installation_conflict: t(
          ($) => $.wecom.scan_error_installation_conflict,
        ),
        bind_failed: t(($) => $.wecom.scan_error_bind_failed),
        wecom_protocol_error: t(($) => $.wecom.scan_error_wecom_protocol_error),
        internal_error: t(($) => $.wecom.scan_error_internal_error),
        session_lost: t(($) => $.wecom.scan_error_session_lost),
        already_connected: t(($) => $.wecom.scan_error_already_connected),
        rate_limited: t(($) => $.wecom.scan_error_rate_limited),
      }[errorReason]
    : t(($) => $.wecom.scan_error_internal_error);

  return (
    <>
      <div className="flex flex-col items-center gap-4 py-2">
        {(beginning || status === "creating") &&
          !qrCodeURL &&
          status !== "error" && (
            <p
              className="text-body text-muted-foreground"
              data-testid="wecom-scan-starting"
            >
              {t(($) => $.wecom.scan_starting)}
            </p>
          )}

        {status === "pending" && qrCodeURL && (
          <>
            {/* White plate regardless of theme: a QR needs light quiet zones
                  to scan, and the dark-mode surface token would invert it. */}
            <div className="rounded-md border bg-white p-3">
              <QRCode value={qrCodeURL} size={192} />
            </div>
            <p className="text-caption text-muted-foreground text-center">
              {t(($) => $.wecom.scan_hint)}
            </p>
            {expiresInSeconds !== null && expiresInSeconds > 0 && (
              <p
                className="text-caption text-muted-foreground"
                data-testid="wecom-scan-expiry"
              >
                {t(($) => $.wecom.scan_expires_in, {
                  seconds: expiresInSeconds,
                })}
              </p>
            )}
          </>
        )}

        {status === "error" && (
          <div className="text-center" data-testid="wecom-scan-error">
            <p className="text-body">{scanErrorText}</p>
          </div>
        )}
      </div>

      <DialogFooter>
        <Button variant="outline" size="sm" onClick={onClose}>
          {t(($) => $.wecom.scan_close)}
        </Button>
        {status === "error" && (
          <Button
            size="sm"
            onClick={() => void beginSession()}
            disabled={beginning}
            data-testid="wecom-scan-retry"
          >
            {t(($) => $.wecom.scan_again)}
          </Button>
        )}
      </DialogFooter>
    </>
  );
}