Skip to content

Commit f97d1e8

Browse files
committed
fix: recompute QR pick URL when session ID changes
Session ID is now generated on modal open (not mount), so the URL hook must react to changes. Restructured usePickUrl to derive the URL via useMemo and only use an effect for the localhost LAN IP fetch.
1 parent b89142a commit f97d1e8

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

apps/app/src/components/qr-modal/index.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import { useEffect, useState } from "react";
3+
import { useEffect, useMemo, useState } from "react";
44
import { QRCodeSVG } from "qrcode.react";
55

66
/* ------------------------------------------------------------------ */
@@ -11,28 +11,24 @@ function usePickUrl(sessionId: string) {
1111
typeof window !== "undefined" &&
1212
(window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1");
1313

14-
const fallbackUrl =
15-
typeof window !== "undefined"
16-
? `${window.location.origin}/pick?session=${sessionId}`
17-
: "";
18-
19-
const [url, setUrl] = useState(() => (isLocalhost ? "" : fallbackUrl));
14+
// LAN IP resolved once (only used on localhost)
15+
const [lanIp, setLanIp] = useState<string | null>(null);
2016

2117
useEffect(() => {
2218
if (!isLocalhost) return;
2319
fetch("/api/pick/ip")
2420
.then((r) => r.json())
25-
.then((data) => {
26-
if (data.ip) {
27-
setUrl(`http://${data.ip}:${window.location.port}/pick?session=${sessionId}`);
28-
} else {
29-
setUrl(fallbackUrl);
30-
}
31-
})
32-
.catch(() => setUrl(fallbackUrl));
33-
}, [sessionId, isLocalhost, fallbackUrl]);
21+
.then((data) => { if (data.ip) setLanIp(data.ip); })
22+
.catch(() => {});
23+
}, [isLocalhost]);
3424

35-
return url;
25+
return useMemo(() => {
26+
if (!sessionId || typeof window === "undefined") return "";
27+
if (isLocalhost && lanIp) {
28+
return `http://${lanIp}:${window.location.port}/pick?session=${sessionId}`;
29+
}
30+
return `${window.location.origin}/pick?session=${sessionId}`;
31+
}, [sessionId, isLocalhost, lanIp]);
3632
}
3733

3834
/* ------------------------------------------------------------------ */

0 commit comments

Comments
 (0)