Skip to content

Commit 2eab1e8

Browse files
authored
fixed create key click twice bug (#5095)
* fixed create key click twice bug * clean up
1 parent dcf9e6d commit 2eab1e8

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

web/components/templates/quickstart/quickstartPage.tsx

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ import {
1717
Loader,
1818
Mail,
1919
MessageSquare,
20-
MoveUpRight,
2120
Send,
2221
UserPlus,
2322
Zap,
2423
} from "lucide-react";
2524
import Link from "next/link";
26-
import { useEffect, useState } from "react";
25+
import { useCallback, useEffect, useState } from "react";
2726
import { useOrgOnboarding } from "../../../services/hooks/useOrgOnboarding";
2827
import { useOrg } from "../../layout/org/organizationContext";
2928
import { QuickstartStepCard } from "../../onboarding/QuickstartStep";
@@ -59,10 +58,9 @@ const QuickstartPage = () => {
5958
const { setNotification } = useNotification();
6059
const { addKey } = useKeys();
6160
const [quickstartKey, setQuickstartKey] = useLocalStorage<string | undefined>(
62-
`${org?.currentOrg?.id}_quickstartKey`,
61+
"quickstartKey",
6362
undefined,
6463
);
65-
const [isCreatingKey, setIsCreatingKey] = useState(false);
6664
const [isProviderSheetOpen, setIsProviderSheetOpen] = useState(false);
6765
const [isPaymentModalOpen, setIsPaymentModalOpen] = useState(false);
6866
const [isHelixDialogOpen, setIsHelixDialogOpen] = useState(false);
@@ -103,27 +101,29 @@ const QuickstartPage = () => {
103101
});
104102
}, []);
105103

106-
const handleCreateKey = async () => {
104+
const handleCreateKey = useCallback(async () => {
107105
try {
108106
let isEu = false;
109107
if (typeof window !== "undefined") {
110108
isEu = window.location.hostname.includes("eu.");
111109
}
112-
setIsCreatingKey(true);
113-
const { apiKey } = await addKey.mutateAsync({
114-
permission: "rw",
115-
keyName: "Quickstart",
116-
isEu,
117-
});
118-
if (apiKey) {
119-
setQuickstartKey(apiKey);
120-
}
110+
111+
addKey.mutateAsync(
112+
{
113+
permission: "rw",
114+
keyName: "Quickstart",
115+
isEu,
116+
},
117+
{
118+
onSuccess: (key) => {
119+
setQuickstartKey(key.apiKey);
120+
},
121+
},
122+
);
121123
} catch (error) {
122124
console.error("Failed to create API key:", error);
123-
} finally {
124-
setIsCreatingKey(false);
125125
}
126-
};
126+
}, [addKey, setQuickstartKey]);
127127

128128
const handleHelixSubmit = (message: string) => {
129129
const helpMessage = {
@@ -312,13 +312,16 @@ const QuickstartPage = () => {
312312
</div>
313313

314314
{/* BYOK Option - Simple text link */}
315-
<div className="flex items-center justify-start pl-4 pt-4 pb-2">
315+
<div className="flex items-center justify-start pb-2 pt-4">
316316
<button
317317
onClick={() => setIsProviderSheetOpen(true)}
318318
className="group flex items-center gap-1 text-sm text-muted-foreground transition-colors hover:text-foreground"
319319
>
320320
<span>or use your own provider keys</span>
321-
<ArrowRight size={14} className="transition-transform group-hover:translate-x-0.5" />
321+
<ArrowRight
322+
size={14}
323+
className="transition-transform group-hover:translate-x-0.5"
324+
/>
322325
{hasProviderKeys && (
323326
<span className="ml-1 inline-flex items-center gap-1 rounded-full bg-green-100 px-2 py-0.5 text-xs font-medium text-green-700 dark:bg-green-900 dark:text-green-300">
324327
<Zap size={10} />
@@ -354,11 +357,11 @@ const QuickstartPage = () => {
354357
<div className="flex flex-col gap-2">
355358
<Button
356359
onClick={handleCreateKey}
357-
disabled={isCreatingKey}
360+
disabled={addKey.isPending}
358361
className="w-fit"
359362
variant="outline"
360363
>
361-
{isCreatingKey ? "Creating..." : "Create API Key"}
364+
{addKey.isPending ? "Creating..." : "Create API Key"}
362365
</Button>
363366
</div>
364367
)}
@@ -588,7 +591,10 @@ const QuickstartPage = () => {
588591
<SheetHeader>
589592
<SheetTitle>Add Provider Keys</SheetTitle>
590593
<SheetDescription>
591-
Add your own provider API keys (BYOK). When "Enable for AI Gateway" is toggled on, requests will attempt to use these keys first, then automatically fall back to Helicone credits if they fail.
594+
Add your own provider API keys (BYOK). When "Enable for AI
595+
Gateway" is toggled on, requests will attempt to use these keys
596+
first, then automatically fall back to Helicone credits if they
597+
fail.
592598
</SheetDescription>
593599
</SheetHeader>
594600
<div className="mt-6">

web/services/hooks/localStorage.tsx

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@ export function useLocalStorage<T>(
88
onNothingStored?: (_setStored: (_t: T) => void) => void,
99
): [T, (_t: T) => void] {
1010
const org = useOrg();
11-
1211
const orgId = org?.currentOrg?.id ?? "";
13-
const orgKey = `${orgId}_${key}`; // Updated key to include orgId
12+
const orgKey = `${orgId}_${key}`;
1413
const [storedValue, setStoredValue] = useState<T>(initialValue);
1514

1615
const setValue = useCallback(
1716
(value: T) => {
1817
try {
19-
const valueToStore =
20-
value instanceof Function ? value(storedValue) : value;
21-
setStoredValue(valueToStore);
22-
if (typeof window !== "undefined") {
23-
window.localStorage.setItem(orgKey, JSON.stringify(valueToStore)); // Use orgKey
24-
}
18+
setStoredValue((prevValue) => {
19+
const valueToStore =
20+
value instanceof Function ? value(prevValue) : value;
21+
// Only save to localStorage if we have a valid orgId and the value is not undefined
22+
if (
23+
typeof window !== "undefined" &&
24+
orgId &&
25+
valueToStore !== undefined
26+
) {
27+
window.localStorage.setItem(orgKey, JSON.stringify(valueToStore));
28+
}
29+
return valueToStore;
30+
});
2531
} catch (error) {
2632
logger.error(
2733
{
@@ -32,13 +38,19 @@ export function useLocalStorage<T>(
3238
);
3339
}
3440
},
35-
[orgKey, storedValue], // Updated dependency to orgKey
41+
// eslint-disable-next-line react-hooks/exhaustive-deps
42+
[key, orgKey, orgId],
3643
);
3744

3845
useEffect(() => {
46+
// Only try to read from localStorage if we have a valid orgId
47+
if (!orgId) {
48+
return;
49+
}
50+
3951
try {
4052
const item =
41-
typeof window !== "undefined" && window.localStorage.getItem(orgKey); // Use orgKey
53+
typeof window !== "undefined" && window.localStorage.getItem(orgKey);
4254
if (!item) {
4355
throw new Error("No item stored");
4456
}
@@ -50,7 +62,7 @@ export function useLocalStorage<T>(
5062
onNothingStored && onNothingStored(setValue);
5163
}
5264
// eslint-disable-next-line react-hooks/exhaustive-deps
53-
}, [orgKey, onNothingStored, setValue]); // Updated dependency to orgKey
65+
}, [key, orgKey, onNothingStored, setValue, orgId]);
5466

5567
return [storedValue, setValue];
5668
}

0 commit comments

Comments
 (0)