Skip to content

Commit 352160b

Browse files
committed
fix(desktop): recover community invite links
Signed-off-by: kenny lopez <[email protected]>
1 parent 5d89296 commit 352160b

3 files changed

Lines changed: 128 additions & 27 deletions

File tree

‎desktop/src/features/community-members/ui/CommunityInviteDialog.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function CommunityInviteDialog({
2626
const [ttlSecs, setTtlSecs] = React.useState(DEFAULT_INVITE_TTL_SECS);
2727

2828
React.useEffect(() => {
29-
if (open) setTtlSecs(DEFAULT_INVITE_TTL_SECS);
29+
// Reset after the link section has unmounted so reopening never mints an
30+
// invite with the previous dialog session's expiry.
31+
if (!open) setTtlSecs(DEFAULT_INVITE_TTL_SECS);
3032
}, [open]);
3133

3234
return (

‎desktop/src/features/community-members/ui/InviteLinkSection.tsx‎

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const MAX_USE_OPTIONS: { label: string; value: number | null }[] = [
3535
export const DEFAULT_INVITE_TTL_SECS = TTL_OPTIONS[1].value;
3636

3737
type CopyStatus = "idle" | "copying" | "copied";
38+
type GenerationStatus = "idle" | "generating" | "failed";
3839

3940
/**
4041
* Share-with-link footer for the community invite dialog.
@@ -51,18 +52,30 @@ export function InviteLinkSection({
5152
ttlSecs: number;
5253
}) {
5354
const [copyStatus, setCopyStatus] = React.useState<CopyStatus>("idle");
55+
const [generationStatus, setGenerationStatus] =
56+
React.useState<GenerationStatus>("generating");
5457
const [inviteUrl, setInviteUrl] = React.useState("");
55-
const [isGenerating, setIsGenerating] = React.useState(true);
5658
const [maxUses, setMaxUses] = React.useState<number | null>(null);
59+
const generationRequestId = React.useRef(0);
5760
const shouldReduceMotion = useReducedMotion();
5861
const ttlLabel =
5962
TTL_OPTIONS.find((option) => option.value === ttlSecs)?.label ?? "3 days";
6063
const maxUsesLabel =
6164
MAX_USE_OPTIONS.find((option) => option.value === maxUses)?.label ??
6265
"No limit";
63-
const copyLabel = copyStatus === "copied" ? "Copied" : "Copy link";
64-
const isCopying = isGenerating || copyStatus === "copying";
65-
const copyButtonWidth = copyStatus === "copied" ? "5.25rem" : "4.5rem";
66+
const isGenerating = generationStatus === "generating";
67+
const hasGenerationFailed = generationStatus === "failed";
68+
const isWorking = isGenerating || copyStatus === "copying";
69+
const copyLabel = hasGenerationFailed
70+
? "Retry"
71+
: copyStatus === "copied"
72+
? "Copied"
73+
: "Copy link";
74+
const copyButtonWidth = isWorking
75+
? "6.25rem"
76+
: copyStatus === "copied"
77+
? "5.25rem"
78+
: "4.5rem";
6679
const copyButtonTransition = shouldReduceMotion
6780
? { duration: 0 }
6881
: { duration: 0.12, ease: [0.77, 0, 0.175, 1] as const };
@@ -73,30 +86,37 @@ export function InviteLinkSection({
7386
return () => window.clearTimeout(resetTimer);
7487
}, [copyStatus]);
7588

76-
React.useEffect(() => {
77-
let isCurrent = true;
78-
79-
async function generateInviteLink() {
80-
setIsGenerating(true);
81-
setInviteUrl("");
82-
setCopyStatus("idle");
83-
try {
84-
const invite = await mintInvite({ ttlSecs, maxUses });
85-
if (isCurrent) setInviteUrl(invite.url);
86-
} catch {
87-
if (isCurrent) {
88-
toast.error("Couldn’t create an invite link. Try again.");
89-
}
90-
} finally {
91-
if (isCurrent) setIsGenerating(false);
89+
const generateInviteLink = React.useCallback(async () => {
90+
const requestId = generationRequestId.current + 1;
91+
generationRequestId.current = requestId;
92+
setGenerationStatus("generating");
93+
setInviteUrl("");
94+
setCopyStatus("idle");
95+
try {
96+
const invite = await mintInvite({ ttlSecs, maxUses });
97+
if (generationRequestId.current === requestId) {
98+
setInviteUrl(invite.url);
99+
setGenerationStatus("idle");
100+
}
101+
} catch {
102+
if (generationRequestId.current === requestId) {
103+
setGenerationStatus("failed");
104+
toast.error("Couldn’t create an invite link.");
92105
}
93106
}
107+
}, [maxUses, ttlSecs]);
94108

109+
React.useEffect(() => {
95110
void generateInviteLink();
96111
return () => {
97-
isCurrent = false;
112+
generationRequestId.current += 1;
98113
};
99-
}, [maxUses, ttlSecs]);
114+
}, [generateInviteLink]);
115+
116+
function retryInviteGeneration() {
117+
if (!hasGenerationFailed) return;
118+
void generateInviteLink();
119+
}
100120

101121
async function handleCopy() {
102122
if (!inviteUrl || isGenerating || copyStatus === "copying") return;
@@ -119,7 +139,11 @@ export function InviteLinkSection({
119139
className="h-11 pr-28 text-transparent caret-transparent selection:bg-transparent"
120140
data-testid="invite-link-url"
121141
disabled={isGenerating}
122-
placeholder="Creating invite link…"
142+
placeholder={
143+
hasGenerationFailed
144+
? "Couldn’t create invite link"
145+
: "Creating invite link…"
146+
}
123147
readOnly
124148
value={inviteUrl}
125149
/>
@@ -142,12 +166,17 @@ export function InviteLinkSection({
142166
className="h-9 w-full px-3"
143167
data-copy-status={copyStatus}
144168
data-testid="copy-invite-link"
145-
disabled={isGenerating || !inviteUrl || copyStatus === "copying"}
146-
onClick={() => void handleCopy()}
169+
disabled={
170+
!hasGenerationFailed &&
171+
(isGenerating || !inviteUrl || copyStatus === "copying")
172+
}
173+
onClick={() =>
174+
hasGenerationFailed ? retryInviteGeneration() : void handleCopy()
175+
}
147176
size="sm"
148177
type="button"
149178
>
150-
{isCopying ? (
179+
{isWorking ? (
151180
<Spinner aria-hidden="true" className="h-4 w-4 border-2" />
152181
) : copyStatus === "copied" ? (
153182
<Check aria-hidden="true" className="h-4 w-4" />

‎desktop/tests/e2e/invite-link-copy.spec.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,73 @@ test("sets a selected invite-use limit", async ({ page }) => {
9191
await page.getByTestId("copy-invite-link").click();
9292
await expect(page.getByTestId("copy-invite-link")).toContainText("Copied");
9393
});
94+
95+
test("retries a failed invite-link generation", async ({ page }) => {
96+
let attempts = 0;
97+
await page.route("**/api/invites", async (route) => {
98+
attempts += 1;
99+
if (attempts === 1) {
100+
await route.fulfill({ status: 500 });
101+
return;
102+
}
103+
104+
await route.fulfill({
105+
contentType: "application/json",
106+
json: {
107+
code: "retry-test",
108+
expires_at: Math.floor(Date.now() / 1000) + 86_400,
109+
url: "buzz://join?relay=wss%3A%2F%2Frelay.example.com&code=retry-test",
110+
},
111+
status: 200,
112+
});
113+
});
114+
115+
await page.goto("/");
116+
await openSettings(page, "community-members");
117+
await page.getByTestId("community-invite-dialog-trigger").click();
118+
119+
const linkField = page.getByTestId("invite-link-url");
120+
const copyButton = page.getByTestId("copy-invite-link");
121+
await expect(linkField).toHaveAttribute(
122+
"placeholder",
123+
"Couldn’t create invite link",
124+
);
125+
await expect(copyButton).toHaveText("Retry");
126+
await expect(copyButton).toBeEnabled();
127+
128+
await copyButton.click();
129+
await expect(linkField).toHaveValue(
130+
"buzz://join?relay=wss%3A%2F%2Frelay.example.com&code=retry-test",
131+
);
132+
await expect(copyButton).toHaveText("Copy link");
133+
expect(attempts).toBe(2);
134+
});
135+
136+
test("reopens with the default expiry before generating a new link", async ({
137+
page,
138+
}) => {
139+
await page.goto("/");
140+
await openSettings(page, "community-members");
141+
await page.getByTestId("community-invite-dialog-trigger").click();
142+
143+
await expect
144+
.poll(() => invitePayloads)
145+
.toEqual([{ ttl_secs: 3 * 24 * 60 * 60 }]);
146+
await page.getByTestId("invite-link-ttl-trigger").click();
147+
await page.getByTestId("invite-link-ttl-604800").click();
148+
await expect
149+
.poll(() => invitePayloads)
150+
.toEqual([{ ttl_secs: 3 * 24 * 60 * 60 }, { ttl_secs: 7 * 24 * 60 * 60 }]);
151+
152+
const dialog = page.getByTestId("community-invite-dialog");
153+
await dialog.getByRole("button", { name: "Close" }).click();
154+
await expect(dialog).toHaveCount(0);
155+
await page.getByTestId("community-invite-dialog-trigger").click();
156+
await expect
157+
.poll(() => invitePayloads)
158+
.toEqual([
159+
{ ttl_secs: 3 * 24 * 60 * 60 },
160+
{ ttl_secs: 7 * 24 * 60 * 60 },
161+
{ ttl_secs: 3 * 24 * 60 * 60 },
162+
]);
163+
});

0 commit comments

Comments
 (0)