Skip to content

Open source CapCut alternative.

TypeScript

fix(site): the claim dialog closes at once and sits in the top third

David Le4eec485cc699
diff --git a/site/src/cut/components/ClaimCreditsDialog.tsx b/site/src/cut/components/ClaimCreditsDialog.tsx
index 96292b2b..60aec3a6 100644
--- a/site/src/cut/components/ClaimCreditsDialog.tsx
+++ b/site/src/cut/components/ClaimCreditsDialog.tsx
@@ -6,8 +6,9 @@
// link-prefetching mail scanners open URLs on the recipient's behalf, and a
// claim on load would start the credit's lifetime before the person saw it.
// Closing drops the token from the address, so a reload opens nothing.
-import { usePathname, useRouter, useSearchParams } from "next/navigation";
+import { useSearchParams } from "next/navigation";
import { Loader2, Sparkle } from "lucide-react";
+import { useState } from "react";
import { Button } from "@/components/ui/button";
import {
Dialog,
@@ -33,38 +34,45 @@ export function ClaimCreditsDialog() {
}
function AddressedDialog() {
- const params = useSearchParams();
- const token = params.get(PARAM);
- const router = useRouter();
- const pathname = usePathname();
+ const token = useSearchParams().get(PARAM);
if (!token) return null;
- // Only the token leaves the address; an open folder stays open.
- const close = () => {
- const rest = new URLSearchParams(params);
- rest.delete(PARAM);
- const search = rest.toString();
- router.replace(search ? `${pathname}?${search}` : pathname);
- };
- return <OpenDialog key={token} token={token} onClose={close} />;
+ return <OpenDialog key={token} token={token} />;
+}
+
+// Only the token leaves the address; an open folder stays open. The native
+// history call syncs into the router without a server round trip, so the
+// close holds whatever the connection is doing.
+function dropTokenFromAddress() {
+ const url = new URL(window.location.href);
+ url.searchParams.delete(PARAM);
+ window.history.replaceState(null, "", url);
}
-function OpenDialog({ token, onClose }: { token: string; onClose: () => void }) {
+function OpenDialog({ token }: { token: string }) {
const offer = useCreditOffer(token);
const claim = useClaimCreditOffer();
+ const [open, setOpen] = useState(true);
const done = claim.data ?? (offer.data?.claimed ? { expiresAt: null } : null);
// The dialog opens once the offer is known, at the size it keeps: the body
// reserves two lines and every state ends in one row of buttons.
if (offer.isPending) return null;
+ const onClose = () => {
+ setOpen(false);
+ dropTokenFromAddress();
+ };
let title = "Claim your credits";
let body: string;
let footer: React.ReactNode;
if (offer.isError) {
- title = "This link is no longer valid";
+ const status = offer.error instanceof ApiError ? offer.error.status : null;
+ title = status === 410 ? "This offer has expired" : "This link is no longer valid";
body =
- offer.error instanceof ApiError && offer.error.status === 403
+ status === 403
? "This offer belongs to a different account. Sign in with the address the email was sent to."
- : "The offer it pointed to is gone.";
+ : status === 410
+ ? "The claim window for this offer has closed."
+ : "The offer it pointed to is gone.";
footer = <Button onClick={onClose}>OK</Button>;
} else if (done) {
body = `${offer.data.credits} in AI credits is on your account.${
@@ -74,7 +82,7 @@ function OpenDialog({ token, onClose }: { token: string; onClose: () => void })
} else {
body = `You have ${offer.data.credits} in AI credits. They\u2019re waiting for you.${
offer.data.lifetime ? ` Once claimed, it\u2019s good for ${offer.data.lifetime}.` : ""
- }`;
+ }${offer.data.closesAt ? ` Claim by ${formatCreditExpiry(new Date(offer.data.closesAt))}.` : ""}`;
footer = (
<>
<Button variant="ghost" onClick={onClose}>
@@ -89,8 +97,8 @@ function OpenDialog({ token, onClose }: { token: string; onClose: () => void })
}
return (
- <Dialog open onOpenChange={(open) => !open && onClose()}>
- <DialogContent className="sm:max-w-md">
+ <Dialog open={open} onOpenChange={(next) => !next && onClose()}>
+ <DialogContent className="top-[18%] min-h-80 translate-y-0 grid-rows-[auto_1fr] sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2 text-lg">
<Sparkle className="size-5 shrink-0 fill-violet-500/25 text-violet-500" />
@@ -98,11 +106,13 @@ function OpenDialog({ token, onClose }: { token: string; onClose: () => void })
</DialogTitle>
<DialogDescription className="min-h-10">{body}</DialogDescription>
</DialogHeader>
- <DialogFooter className="mx-0 mb-0 items-center border-0 bg-transparent p-0">
+ <DialogFooter className="mx-0 mb-0 items-center self-end border-0 bg-transparent p-0">
{claim.isError && (
<p className="text-sm text-destructive sm:mr-auto">
{claim.error instanceof ApiError && claim.error.status === 403
? "This offer belongs to a different account. Sign in with the address the email was sent to."
+ : claim.error instanceof ApiError && claim.error.status === 410
+ ? "The claim window for this offer has closed."
: "That didn\u2019t go through. Try again."}
</p>
)}