Skip to content

Commit c43d73e

Browse files
refactor: Update useQueryParams hook to accept optional streamUrl parameter (#420)
The useQueryParams hook in the P2PVideoDemo component has been updated to accept an optional streamUrl parameter. This allows for more flexibility in setting the initial stream URL when using the hook. The defaultParams object in the hook has also been modified to include the streamUrl parameter. This refactor improves the reusability and configurability of the useQueryParams hook.
1 parent 81ca8d5 commit c43d73e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

packages/p2p-media-loader-demo/src/components/P2PVideoDemo.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const P2PVideoDemo = ({
6666
p2pUploaded: 0,
6767
});
6868

69-
const { queryParams, setURLQueryParams } = useQueryParams();
69+
const { queryParams, setURLQueryParams } = useQueryParams(streamUrl);
7070

7171
const trackers = useMemo(
7272
() => queryParams.trackers.split(","),
@@ -121,7 +121,7 @@ export const P2PVideoDemo = ({
121121

122122
return PlayerComponent ? (
123123
<PlayerComponent
124-
streamUrl={streamUrl ?? queryParams.streamUrl}
124+
streamUrl={queryParams.streamUrl}
125125
announceTrackers={trackers}
126126
swarmId={queryParams.swarmId === "" ? undefined : queryParams.swarmId}
127127
onPeerConnect={onPeerConnect}

packages/p2p-media-loader-demo/src/hooks/useQueryParams.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
1-
import { useState, useEffect, useCallback, useRef } from "react";
1+
import { useState, useEffect, useCallback, useRef, useMemo } from "react";
22
import { DEFAULT_STREAM, DEFAULT_TRACKERS, PLAYERS } from "../constants";
33

44
type QueryParamsType = Record<string, string>;
55

6-
const defaultParams: QueryParamsType = {
7-
player: Object.keys(PLAYERS)[0],
8-
streamUrl: DEFAULT_STREAM,
9-
trackers: DEFAULT_TRACKERS,
10-
debug: "",
11-
swarmId: "",
12-
};
13-
14-
function getInitialParams(searchParams: URLSearchParams): QueryParamsType {
6+
function getInitialParams(
7+
searchParams: URLSearchParams,
8+
defaultParams: QueryParamsType,
9+
): QueryParamsType {
1510
return Object.keys(defaultParams).reduce((params, key) => {
1611
params[key] = searchParams.get(key) ?? defaultParams[key];
1712
return params;
1813
}, {} as QueryParamsType);
1914
}
2015

21-
export function useQueryParams() {
16+
export function useQueryParams(streamUri?: string) {
17+
const defaultParams = useMemo(() => {
18+
return {
19+
player: Object.keys(PLAYERS)[0],
20+
streamUrl: streamUri ?? DEFAULT_STREAM,
21+
trackers: DEFAULT_TRACKERS,
22+
debug: "",
23+
swarmId: "",
24+
} as QueryParamsType;
25+
}, [streamUri]);
26+
2227
const searchParamsRef = useRef(new URLSearchParams(window.location.search));
2328
const [queryParams, setQueryParams] = useState<QueryParamsType>(() =>
24-
getInitialParams(searchParamsRef.current),
29+
getInitialParams(searchParamsRef.current, defaultParams),
2530
);
2631

2732
const updateQueryParamsFromURL = useCallback(() => {
2833
const searchParams = searchParamsRef.current;
29-
const newParams = getInitialParams(searchParams);
34+
const newParams = getInitialParams(searchParams, defaultParams);
3035

3136
setQueryParams((prevParams) => {
3237
const hasChanges = Object.keys(newParams).some(
3338
(key) => prevParams[key] !== newParams[key],
3439
);
3540
return hasChanges ? newParams : prevParams;
3641
});
37-
}, []);
42+
}, [defaultParams]);
3843

3944
const setURLQueryParams = useCallback(
4045
(newParams: Partial<QueryParamsType>) => {
@@ -60,7 +65,7 @@ export function useQueryParams() {
6065

6166
updateQueryParamsFromURL();
6267
},
63-
[updateQueryParamsFromURL],
68+
[defaultParams, updateQueryParamsFromURL],
6469
);
6570

6671
useEffect(() => {

0 commit comments

Comments
 (0)