Skip to content
Python
2.4 KB
Raw Blame
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { announceCloudChanged, cloudLogin, waitForCloudSignIn } from "../../api";

// The signed-out state of every one-click pane: a REAL sign-in button, not a
// hint pointing at another page. Sign-in completes in the system browser; this
// component then polls until the status flips and broadcasts CLOUD_CHANGED, so
// even poll-less hosts (the Sources rail's inline pane) re-render signed in —
// relying on "some other section's 5s poll" left the rail stuck on the prompt
// (FB-013).
export function CloudSignInInline({
  blurb,
  onSignedIn,
}: {
  blurb?: string;
  // Fires once the poll confirms sign-in landed, after announceCloudChanged() —
  // lets a caller (e.g. a one-click connect that just discovered its session had
  // expired) resume the flow it was in instead of leaving the user to notice the
  // button reappeared and click it again (issue #658).
  onSignedIn?: () => void;
}) {
  const { t } = useTranslation();
  const [waiting, setWaiting] = useState(false);
  const cancelRef = useRef<(() => void) | null>(null);
  useEffect(() => () => cancelRef.current?.(), []);
  return (
    <div className="space-y-1.5">
      <button
        className="w-full px-3 py-2 rounded-lg border border-accent text-accent text-ui font-medium hover:bg-accentSoft/40"
        data-testid="inline-cloud-sign-in"
        onClick={async () => {
          setWaiting(true);
          await cloudLogin();
          cancelRef.current?.();
          cancelRef.current = waitForCloudSignIn((s) => {
            setWaiting(false);
            if (s?.signed_in) {
              announceCloudChanged();
              onSignedIn?.();
            }
          });
        }}
      >
        {waiting ? t("cloud.check_browser") : t("cloud.sign_in")}
      </button>
      <div className="text-meta text-faint">
        {blurb || t("misc.cloud_signin.blurb")}
      </div>
    </div>
  );
}

// The UNKNOWN state: the status fetch hasn't resolved (or is being retried).
// Rendering the sign-in prompt here told signed-in users they weren't (FB-013) —
// pending must look like pending.
export function CloudStatusPending() {
  const { t } = useTranslation();
  return (
    <div
      className="text-meta text-faint py-2 text-center"
      data-testid="cloud-status-pending"
    >
      {t("cloud.checking")}
    </div>
  );
}