Skip to content

Commit 3e88c35

Browse files
committed
refactor: Update NetworkInformation interface and Navigator type
- Updated the NetworkInformation interface to include additional properties: downlinksMax and saveData - Updated the Navigator type to include a connection property of type NetworkInformation - These changes ensure that the NetworkInformation interface and Navigator type accurately reflect the properties and structure of the network information data
1 parent d4397f8 commit 3e88c35

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
export const hasSupport = (): boolean =>
2-
Boolean('connection' in window.navigator);
1+
export interface NetworkInformation {
2+
effectiveType: string;
3+
rtt: number;
4+
downlink: number;
5+
downlinksMax?: number;
6+
saveData: boolean;
7+
type: string;
8+
}
9+
interface Navigator {
10+
connection?: NetworkInformation;
11+
}
312

4-
function run() {
5-
let conn = window.navigator.connection;
13+
export const hasSupport = (): boolean => 'connection' in window.navigator;
614

15+
function run() {
716
if (hasSupport()) {
8-
return conn;
17+
return (window.navigator as Navigator).connection;
918
}
10-
11-
return conn;
19+
return null;
1220
}
1321

1422
export default run;

src/modules/demos/network-information/index.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ import React from 'react';
22

33
import { NotSupported, Tag } from 'components';
44

5-
import run, { hasSupport } from '../../apis/network-information';
5+
import run, {
6+
hasSupport,
7+
NetworkInformation,
8+
} from '../../apis/network-information';
69

710
const UNKNOWN_STRING = 'Unknown';
811

912
function Network() {
10-
const [data] = React.useState<
11-
NetworkInformation & {
12-
effectiveType?: 'readonly';
13-
rtt?: 'readonly';
14-
downlink?: 'readonly';
15-
downlinksMax?: 'readonly';
16-
saveData?: 'readonly';
17-
}
18-
>(run());
13+
const [data] = React.useState<NetworkInformation | null | undefined>(run());
1914

2015
if (!hasSupport()) {
2116
return <NotSupported />;

0 commit comments

Comments
 (0)