import { useEffect, useRef, useState } from "react";
import { ExternalLink, Sparkle, Star, X } from "lucide-react";
import { useTranslation } from "react-i18next";
import starInvitation from "@/assets/star-invitation.webp";
import { Button } from "@/components/ui/button";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogTitle } from "@/components/ui/dialog";
import { starPromptAction } from "@/lib/api";
import { useClient } from "@/providers/ClientProvider";
import "./StarPrompt.css";
const REPOSITORY_URL = "https://github.com/HKUDS/nanobot";
const actionClassName = "h-full min-h-[46px] min-w-0 !whitespace-normal px-3 py-2.5 text-center text-[15px] font-medium max-[480px]:text-sm";
export function StarLink({ onSaved, fullWidth = false }: {
onSaved?: () => void;
fullWidth?: boolean;
}) {
const { client } = useClient();
const { t } = useTranslation();
const [error, setError] = useState(false);
const dismiss = () => {
setError(false);
void starPromptAction(client, "dismiss").then(onSaved).catch(() => setError(true));
};
const link = (
{ if (event.button === 1) dismiss(); }}>
{!fullWidth && }
{t(fullWidth ? "starPrompt.action" : "starPrompt.footerAction")}
{fullWidth ? (
) : }
);
return (
<>
{fullWidth ? : link}
{error &&
{t("starPrompt.saveError")}
}
>
);
}
export function StarPrompt({ ready }: { ready: boolean }) {
const { client } = useClient();
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [saving, setSaving] = useState(false);
const [error, setError] = useState(false);
const attempted = useRef(false);
const interacted = useRef(false);
const previousFocus = useRef(null);
const titleRef = useRef(null);
useEffect(() => {
const cancel = () => { interacted.current = true; };
const events = ["pointerdown", "keydown", "input", "wheel"] as const;
for (const event of events) document.addEventListener(event, cancel, { capture: true, passive: true });
return () => {
for (const event of events) document.removeEventListener(event, cancel, true);
};
}, []);
useEffect(() => {
if (!ready || attempted.current) return;
let cancelled = false;
const unsubscribe = client.onStatus((status) => {
if (status !== "open" || attempted.current) return;
attempted.current = true;
if (interacted.current || document.visibilityState !== "visible"
|| document.querySelector('[role="dialog"], [role="alertdialog"]')) return;
void starPromptAction(client, "claim").then(({ show }) => {
if (cancelled || !show || interacted.current || document.visibilityState !== "visible"
|| document.querySelector('[role="dialog"], [role="alertdialog"]')) return;
previousFocus.current = document.activeElement instanceof HTMLElement
? document.activeElement : null;
setOpen(true);
}).catch(() => { /* Optional invitations stay hidden when storage is unavailable. */ });
});
return () => { cancelled = true; unsubscribe(); };
}, [client, ready]);
const dismissForever = async () => {
setSaving(true);
setError(false);
try {
await starPromptAction(client, "dismiss");
setOpen(false);
} catch {
setError(true);
} finally {
setSaving(false);
}
};
return (
);
}