From 1079acb167dada7399b79d24bb756bf095a4db57 Mon Sep 17 00:00:00 2001 From: alivarastepour Date: Fri, 3 Nov 2023 09:26:11 +0330 Subject: [PATCH] fixed a type --- hooks/useTempMediaQuery.ts | 74 +++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/hooks/useTempMediaQuery.ts b/hooks/useTempMediaQuery.ts index 63d583a..ea56e22 100644 --- a/hooks/useTempMediaQuery.ts +++ b/hooks/useTempMediaQuery.ts @@ -1,5 +1,8 @@ -import { TMediaQuery } from "@/types/TMediaQuery"; -import { useEffect, useMemo } from "react"; +import { useEffect, useRef, useState } from "react"; +import { v4 as uuidv4 } from "uuid"; + +export type TMediaQuery = `${"sm" | "lg"}-${number}`; +// type TMediaQueryDispatcher = class MediaQuerySingleton { private static isInitialized: boolean = false; @@ -7,7 +10,11 @@ class MediaQuerySingleton { private static queryResult: Record = {}; private static listenerDelay: boolean = false; private static listenerDelayAmount = 100; - + public static dispatchersMap: { + [id: string]: React.Dispatch< + React.SetStateAction> + >; + }[]; constructor(listenerDelayAmount?: number) { if (MediaQuerySingleton.isInitialized) { throw Error("reinitializing a singleton class"); @@ -27,6 +34,24 @@ class MediaQuerySingleton { } } + public get queryList(): TMediaQuery[] { + return MediaQuerySingleton.queryList; + } + + public addToDispatchers( + id: string, + dispatcher: React.Dispatch< + React.SetStateAction> + > + ) { + if (Object.keys(MediaQuerySingleton.dispatchersMap).includes(id)) return; + MediaQuerySingleton.dispatchersMap.push({ [id]: dispatcher }); + } + + public get queryResult(): Record { + return MediaQuerySingleton.queryResult; + } + private static eventListenerCallback() { MediaQuerySingleton.resizeListener(false); } @@ -47,15 +72,16 @@ class MediaQuerySingleton { const res = comparator === "sm" ? width < +value : +value < width; MediaQuerySingleton.queryResult[query] = res; } + + for (const obj of MediaQuerySingleton.dispatchersMap) { + Object.values(obj)[0](MediaQuerySingleton.queryResult); + } + setTimeout(() => { MediaQuerySingleton.listenerDelay = false; }, MediaQuerySingleton.listenerDelayAmount); } - public get queryList(): TMediaQuery[] { - return MediaQuerySingleton.queryList; - } - public addQueries(queries: TMediaQuery[]) { for (const query of queries) { if (!MediaQuerySingleton.queryList.includes(query)) { @@ -64,25 +90,41 @@ class MediaQuerySingleton { } MediaQuerySingleton.resizeListener(true); } - - public get queryResult(): Record { - return MediaQuerySingleton.queryResult; - } } function mediaQueryFactory() { return new MediaQuerySingleton(); } + const mediaQuery = mediaQueryFactory(); -export const useTempMediaQuery = ({ queries }: { queries: TMediaQuery[] }) => { +const useMediaQuery = ({ + queries, +}: { + queries: T; +}): Record => { + const [result, setResult] = useState>({}); + const ref = useRef(null); + console.log("also here"); + useEffect(() => { - mediaQuery.addQueries(queries); + if (!ref.current) { + console.log("not here"); + + ref.current = uuidv4(); + } }, []); useEffect(() => { - console.log("changed"); - }, [mediaQuery.queryResult]); + if (!ref.current) return; - return useMemo(() => mediaQuery.queryResult, [mediaQuery.queryResult]); + console.log("here"); + + mediaQuery.addToDispatchers(ref.current, setResult); + mediaQuery.addQueries(queries); + }, [queries, setResult]); + + return result; }; + +export default useMediaQuery;