mirror of
https://github.com/Such-Software/smirk-website.git
synced 2026-12-01 12:14:12 +01:00
fix: race condition on Connect with Smirk button + sync TS types
The extension's window.smirk injection runs as `<script type=module>`,
which browsers defer until after DOMContentLoaded. The previous
detection (sync check + once at +500ms) could race past that on
heavier Next.js pages, leaving the Connect button stuck greyed-out
even though the extension was installed and unlocked.
Three-layer fix in both `app/page.tsx` and `app/tip/[id]/TipPageClient.tsx`:
1. Immediate sync check (fast path: extension already there).
2. Listen for `smirk-ready` CustomEvent (extension v0.2.5+ dispatches it).
3. Polling fallback at 100ms intervals up to 3s, for older
extensions or any future browser quirk. Cheap; stops as soon as
window.smirk appears.
`hasExtension` stays `null` (initial) until either we resolve `true`
or hit the 3s timeout and resolve `false` — so the install prompt
doesn't flash on first paint while polling is still in flight.
Also updated `types/smirk.d.ts` to current extension API surface:
asset-scoped `connect(assets?: SmirkAsset[])`, `getAddresses`,
`requestPayment`, and the `smirk-ready` WindowEventMap entry.
Per-asset return fields are nullable now since the user can approve
a subset of assets at connect time (cross-chain deanon mitigation).
This commit is contained in:
+45
-6
@@ -26,13 +26,52 @@ export default function Home() {
|
||||
const [checkingSession, setCheckingSession] = useState(true);
|
||||
const [userCount, setUserCount] = useState<number | null>(null);
|
||||
|
||||
// Check for extension on mount
|
||||
// Check for extension on mount.
|
||||
//
|
||||
// The Smirk extension injects via `<script type="module">`, which
|
||||
// the browser DEFERS until after DOMContentLoaded — so a single
|
||||
// immediate-or-500ms check could race past the injection on a busy
|
||||
// Next.js page (the cause of an old "Connect" stays-greyed-out
|
||||
// bug). Three layers cover all timings:
|
||||
//
|
||||
// 1. Immediate sync check (fast path: extension already there).
|
||||
// 2. Listen for the `smirk-ready` CustomEvent (extension v0.2.5+).
|
||||
// 3. Polling fallback up to ~3s, for older extensions + any
|
||||
// future browser quirk. Cheap; stops as soon as found.
|
||||
useEffect(() => {
|
||||
const check = () => setHasExtension(!!window.smirk);
|
||||
// Check immediately and after a delay (extension might inject late)
|
||||
check();
|
||||
const timeout = setTimeout(check, 500);
|
||||
return () => clearTimeout(timeout);
|
||||
let cancelled = false;
|
||||
const found = () => {
|
||||
if (!cancelled && window.smirk) {
|
||||
setHasExtension(true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
// Layer 1: immediate.
|
||||
if (found()) return;
|
||||
|
||||
// Layer 2: event from the extension (v0.2.5+).
|
||||
const onReady = () => found();
|
||||
window.addEventListener('smirk-ready', onReady);
|
||||
|
||||
// Layer 3: polling fallback (100ms intervals up to 3s).
|
||||
const poll = setInterval(() => {
|
||||
if (found()) clearInterval(poll);
|
||||
}, 100);
|
||||
|
||||
// Final timeout: definitively mark as missing if still not seen.
|
||||
const giveUp = setTimeout(() => {
|
||||
clearInterval(poll);
|
||||
if (!cancelled && !window.smirk) setHasExtension(false);
|
||||
}, 3000);
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
window.removeEventListener('smirk-ready', onReady);
|
||||
clearInterval(poll);
|
||||
clearTimeout(giveUp);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// Fetch user count on mount
|
||||
|
||||
Reference in New Issue
Block a user