Skip to content

Commit 2e80d06

Browse files
committed
feat: implement useNormalizedParams hook for consistent parameter handling across components
1 parent b029e5d commit 2e80d06

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

src/features/event/Event.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useEffect, useMemo, useState } from "react";
2-
import { useSearchParams } from "react-router";
1+
import { useEffect, useState } from "react";
32
import {
43
getConferenceByCode,
54
getEventById,
@@ -20,17 +19,10 @@ import LoadingPage from "@/components/LoadingPage";
2019
import ErrorPage from "@/components/ErrorPage";
2120
import { HTFooter } from "@/components/HTFooter";
2221
import EventDetails from "./EventDetails";
22+
import { useNormalizedParams } from "@/lib/utils/params";
2323

2424
export function Event() {
25-
const [searchParams] = useSearchParams();
26-
27-
const { confCode, eventId } = useMemo(
28-
() => ({
29-
confCode: searchParams.get("conf") ?? undefined,
30-
eventId: searchParams.get("event") ?? undefined,
31-
}),
32-
[searchParams]
33-
);
25+
const { confCode, eventId } = useNormalizedParams();
3426

3527
const [event, setEvent] = useState<ProcessedEvent | null>(null);
3628
const [people, setPeople] = useState<HTPerson[]>([]);

src/features/person/Person.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { useEffect, useState, Suspense, startTransition } from "react";
2-
import { useSearchParams } from "react-router";
32
import { getConferenceByCode, getEventsByIds, getSpeakerById } from "@/lib/db";
43
import type { HTConference, HTEvent, HTPerson } from "@/types/db";
54
import { ConferenceHeader } from "@/components/ConferenceHeader";
65
import LoadingPage from "@/components/LoadingPage";
76
import ErrorPage from "@/components/ErrorPage";
87
import { HTFooter } from "@/components/HTFooter";
98
import PersonDetails from "./PersonDetails";
9+
import { useNormalizedParams } from "@/lib/utils/params";
1010

1111
export function Person() {
12-
const [searchParams] = useSearchParams();
13-
const confCode = searchParams.get("conf");
14-
const personId = searchParams.get("person");
12+
const { confCode, personId } = useNormalizedParams();
1513

1614
const [person, setPerson] = useState<HTPerson | null>(null);
1715
const [conference, setConference] = useState<HTConference | null>(null);
@@ -79,10 +77,10 @@ export function Person() {
7977
<div className="min-h-dvh flex flex-col">
8078
{conference && <ConferenceHeader conference={conference} />}
8179
<main className="flex-1">
82-
{person && confCode ? (
80+
{person && conference ? (
8381
<Suspense fallback={<LoadingPage message="Loading people..." />}>
8482
<PersonDetails
85-
confCode={confCode}
83+
conference={conference}
8684
person={person}
8785
events={events}
8886
/>

src/features/schedule/Schedule.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useEffect, useState, lazy, Suspense, startTransition } from "react";
2-
import { useSearchParams } from "react-router";
32
import { getConferenceByCode, getEvents, getTags } from "@/lib/db";
43
import { buildScheduleBucketsByDay } from "@/lib/utils/schedule";
54
import type { GroupedSchedule } from "@/types/ht";
@@ -8,12 +7,12 @@ import { ConferenceHeader } from "@/components/ConferenceHeader";
87
import LoadingPage from "@/components/LoadingPage";
98
import ErrorPage from "@/components/ErrorPage";
109
import { HTFooter } from "@/components/HTFooter";
10+
import { useNormalizedParams } from "@/lib/utils/params";
1111

1212
const EventsList = lazy(() => import("./EventsList"));
1313

1414
export function Schedule() {
15-
const [searchParams] = useSearchParams();
16-
const confCode = searchParams.get("conf");
15+
const { confCode } = useNormalizedParams();
1716

1817
const [grouped, setGrouped] = useState<GroupedSchedule | null>(null);
1918
const [conference, setConference] = useState<HTConference | null>(null);

src/lib/utils/params.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useSearchParams } from "react-router";
2+
3+
export function useNormalizedParams() {
4+
const [sp] = useSearchParams();
5+
const confCode = sp.get("conf")?.trim().toUpperCase(); // case-insensitive conf
6+
const event = sp.get("event");
7+
const person = sp.get("person");
8+
const eventId = event && /^\d+$/.test(event) ? Number(event) : undefined;
9+
const personId = person && /^\d+$/.test(person) ? Number(person) : undefined;
10+
return { confCode, eventId, personId };
11+
}

0 commit comments

Comments
 (0)