From d5677cf7e80dc6393a50474441d42345259d34a9 Mon Sep 17 00:00:00 2001 From: Remmy Cat Stock <3317423+remmycat@users.noreply.github.com> Date: Thu, 27 Oct 2022 23:12:48 +0200 Subject: [PATCH] Remove useOnce hook --- app/components/MapPoolSelector.tsx | 15 +++++++++++---- app/hooks/useOnce.ts | 10 ---------- app/routes/maps.tsx | 5 +---- 3 files changed, 12 insertions(+), 18 deletions(-) delete mode 100644 app/hooks/useOnce.ts diff --git a/app/components/MapPoolSelector.tsx b/app/components/MapPoolSelector.tsx index d02d340a1..fa342f8a8 100644 --- a/app/components/MapPoolSelector.tsx +++ b/app/components/MapPoolSelector.tsx @@ -18,7 +18,6 @@ import type { CalendarEvent } from "~/db/types"; import type { SerializedMapPoolEvent } from "~/routes/calendar/map-pool-events"; import { assertType } from "~/utils/types"; import { MapPoolEventsCombobox } from "./Combobox"; -import { useOnce } from "~/hooks/useOnce"; export type MapPoolSelectorProps = { mapPool: MapPool; @@ -46,8 +45,8 @@ export function MapPoolSelector({ initialEvent ? "event" : detectTemplate(mapPool) ); - const initialSerializedEvent: SerializedMapPoolEvent | undefined = useOnce( - () => + const [initialSerializedEvent, setInitialSerializedEvent] = React.useState( + (): SerializedMapPoolEvent | undefined => initialEvent && { ...initialEvent, serializedMapPool: mapPool.serialized, @@ -67,7 +66,15 @@ export function MapPoolSelector({ const handleTemplateChange = (template: MapPoolTemplateValue) => { setTemplate(template); - if (template === "none" || template === "event") { + if (template === "none") { + return; + } + + if (template === "event") { + // If the user selected the "event" option, the _initial_ event passed via + // props is likely not the current state and should not be prefilled + // anymore. + setInitialSerializedEvent(undefined); return; } diff --git a/app/hooks/useOnce.ts b/app/hooks/useOnce.ts deleted file mode 100644 index 4296bf40a..000000000 --- a/app/hooks/useOnce.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { useMemo } from "react"; - -/** - * Utility hook for calling `useMemo(f, [])`, when you're sure it needs no - * revalidation but feel bad for getting shamed by eslint everytime :D - */ -export function useOnce(factory: () => T) { - // eslint-disable-next-line react-hooks/exhaustive-deps - return useMemo(factory, []); -} diff --git a/app/routes/maps.tsx b/app/routes/maps.tsx index 2123637f7..d1fbcebe7 100644 --- a/app/routes/maps.tsx +++ b/app/routes/maps.tsx @@ -30,7 +30,6 @@ import { calendarEventPage, ipLabsMaps } from "~/utils/urls"; import { type SendouRouteHandle } from "~/utils/remix"; import { MapPoolSelector, MapPoolStages } from "~/components/MapPoolSelector"; import { EditIcon } from "~/components/icons/Edit"; -import { useOnce } from "~/hooks/useOnce"; import { getUser } from "~/modules/auth"; import type { CalendarEvent } from "~/db/types"; @@ -145,7 +144,7 @@ function useSearchParamPersistedMapPool() { const data = useLoaderData(); const [searchParams, setSearchParams] = useSearchParams(); - const initialMapPool = useOnce(() => { + const [mapPool, setMapPool] = React.useState(() => { if (searchParams.has("pool")) { return new MapPool(searchParams.get("pool")!); } @@ -157,8 +156,6 @@ function useSearchParamPersistedMapPool() { return MapPool.ANARCHY; }); - const [mapPool, setMapPool] = React.useState(initialMapPool); - const handleMapPoolChange = ( newMapPool: MapPool, event?: Pick