Skip to content

Commit e902521

Browse files
cursoragentgregnazario
authored andcommitted
fix(explorer): only check for local device when localnet is explicitly selected
- Modified useLocalnetDetection hook to accept an 'enabled' option that defaults to false - Updated NetworkSelect to always show the localnet option instead of auto-detecting - Updated LocalnetUnavailableModal to only enable localnet detection when local network is selected This prevents the browser from prompting users about connecting to local devices unless they explicitly select the localnet network from the dropdown.
1 parent 96d48e4 commit e902521

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

app/components/LocalnetUnavailableModal.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import {defaultNetworkName} from "../constants";
1515

1616
export default function LocalnetUnavailableModal() {
1717
const [networkName, setNetworkName] = useNetworkSelector();
18-
const {isAvailable, isChecked} = useLocalnetDetection();
18+
// Only check for localnet availability when local network is explicitly selected
19+
// This prevents prompting users about local device connections unless they choose localnet
20+
const {isAvailable, isChecked} = useLocalnetDetection({
21+
enabled: networkName === "local",
22+
});
1923

2024
// Show modal when on localnet, initial check is done, and it's not running
2125
const showModal = networkName === "local" && isChecked && !isAvailable;

app/components/layout/NetworkSelect.tsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ import {
99
import {useLocation} from "@tanstack/react-router";
1010
import {useNetworkSelector} from "../../global-config";
1111
import {networks, NetworkName, hiddenNetworks} from "../../constants";
12-
import {useLocalnetDetection} from "../../hooks/useLocalnetDetection";
1312
import {useNavigate} from "../../routing";
1413

1514
export default function NetworkSelect() {
1615
const theme = useTheme();
1716
const [networkName, setNetworkName] = useNetworkSelector();
18-
const {isAvailable: isLocalnetAvailable} = useLocalnetDetection();
1917
const navigate = useNavigate();
2018
const location = useLocation();
2119

@@ -32,17 +30,15 @@ export default function NetworkSelect() {
3230
});
3331
};
3432

35-
// Filter out hidden networks and "local" (shown separately as "Localnet" when available)
33+
// Filter out hidden networks and "local" (shown separately as "Localnet")
3634
const visibleNetworks = Object.keys(networks).filter(
3735
(network) =>
3836
!hiddenNetworks.includes(network as NetworkName) && network !== "local",
3937
) as NetworkName[];
4038

41-
// Check if current network is a hidden network (but not local if it's available)
39+
// Check if current network is a hidden network (excluding local which is always shown)
4240
const isHiddenNetwork =
43-
(hiddenNetworks.includes(networkName) ||
44-
(networkName === "local" && !isLocalnetAvailable)) &&
45-
!(networkName === "local" && isLocalnetAvailable);
41+
hiddenNetworks.includes(networkName) && networkName !== "local";
4642

4743
// Custom render for the selected value to show hidden network names
4844
const renderValue = (selected: string) => {
@@ -93,16 +89,10 @@ export default function NetworkSelect() {
9389
{network}
9490
</MenuItem>
9591
))}
96-
{/* Show localnet option when detected */}
97-
{isLocalnetAvailable && (
98-
<MenuItem
99-
key="local"
100-
value="local"
101-
sx={{textTransform: "capitalize"}}
102-
>
103-
localnet
104-
</MenuItem>
105-
)}
92+
{/* Always show localnet option - user must explicitly select it to trigger local device detection */}
93+
<MenuItem key="local" value="local" sx={{textTransform: "capitalize"}}>
94+
localnet
95+
</MenuItem>
10696
</Select>
10797
</FormControl>
10898
);

app/hooks/useLocalnetDetection.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,35 @@ interface LocalnetDetectionResult {
99
isChecked: boolean; // True after initial check completes
1010
}
1111

12+
interface UseLocalnetDetectionOptions {
13+
/**
14+
* Whether to actively check for localnet availability.
15+
* When false, the hook will not make any network requests.
16+
* Defaults to false to avoid prompting users about local device connections
17+
* unless they explicitly select the local network.
18+
*/
19+
enabled?: boolean;
20+
}
21+
1222
/**
1323
* Hook to detect if a local Aptos node is running.
14-
* Only runs on the client side.
24+
* Only runs on the client side and only when enabled.
1525
* Returns both availability status and whether the initial check has completed.
26+
*
27+
* @param options.enabled - Whether to actively check for localnet. Defaults to false.
1628
*/
17-
export function useLocalnetDetection(): LocalnetDetectionResult {
29+
export function useLocalnetDetection(
30+
options: UseLocalnetDetectionOptions = {},
31+
): LocalnetDetectionResult {
32+
const {enabled = false} = options;
1833
const [isLocalnetAvailable, setIsLocalnetAvailable] = useState(false);
1934
const [isChecked, setIsChecked] = useState(false);
2035

2136
useEffect(() => {
22-
// Only run on client
23-
if (typeof window === "undefined") return;
37+
// Only run on client and when enabled
38+
if (typeof window === "undefined" || !enabled) {
39+
return;
40+
}
2441

2542
const checkLocalnet = async () => {
2643
try {
@@ -59,7 +76,15 @@ export function useLocalnetDetection(): LocalnetDetectionResult {
5976
const interval = setInterval(checkLocalnet, CHECK_INTERVAL);
6077

6178
return () => clearInterval(interval);
62-
}, []);
79+
}, [enabled]);
80+
81+
// When not enabled, return unchecked state (checking hasn't started)
82+
// This is fine because the LocalnetUnavailableModal only shows the modal
83+
// when networkName === "local" (which enables this hook), so the returned
84+
// values when disabled don't affect the modal display.
85+
if (!enabled) {
86+
return {isAvailable: false, isChecked: false};
87+
}
6388

6489
return {isAvailable: isLocalnetAvailable, isChecked};
6590
}

0 commit comments

Comments
 (0)