From 602767889bf1f77fbf1f02daba99133b5371f65e Mon Sep 17 00:00:00 2001 From: Grant Date: Tue, 18 Jun 2024 19:00:15 -0600 Subject: [PATCH] fix some types being incorrect --- .../src/components/Toolbar/CanvasMeta.tsx | 5 +++-- .../client/src/components/Toolbar/Palette.tsx | 2 +- .../src/components/Toolbar/UndoButton.tsx | 2 +- packages/client/src/contexts/AppContext.tsx | 17 ++++++++++++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/client/src/components/Toolbar/CanvasMeta.tsx b/packages/client/src/components/Toolbar/CanvasMeta.tsx index b318c57..f7818f7 100644 --- a/packages/client/src/components/Toolbar/CanvasMeta.tsx +++ b/packages/client/src/components/Toolbar/CanvasMeta.tsx @@ -27,7 +27,7 @@ const getTimeLeft = (pixels: { available: number }, config: ClientConfig) => { }; const PlaceCountdown = () => { - const { pixels, config } = useAppContext(); + const { pixels, config } = useAppContext(); const [timeLeft, setTimeLeft] = useState(getTimeLeft(pixels, config)); useEffect(() => { @@ -69,7 +69,8 @@ const OnlineCount = () => { }; export const CanvasMeta = () => { - const { canvasPosition, cursorPosition, pixels, config } = useAppContext(); + const { canvasPosition, cursorPosition, pixels, config } = + useAppContext(); const { isOpen, onOpen, onOpenChange } = useDisclosure(); return ( diff --git a/packages/client/src/components/Toolbar/Palette.tsx b/packages/client/src/components/Toolbar/Palette.tsx index 5f98651..91e74d4 100644 --- a/packages/client/src/components/Toolbar/Palette.tsx +++ b/packages/client/src/components/Toolbar/Palette.tsx @@ -6,7 +6,7 @@ import { faXmark } from "@fortawesome/free-solid-svg-icons"; import { IPaletteContext } from "@sc07-canvas/lib/src/net"; export const Palette = () => { - const { config, user } = useAppContext(); + const { config, user } = useAppContext(); const [pallete, setPallete] = useState({}); useEffect(() => { diff --git a/packages/client/src/components/Toolbar/UndoButton.tsx b/packages/client/src/components/Toolbar/UndoButton.tsx index 07944c1..284dfe2 100644 --- a/packages/client/src/components/Toolbar/UndoButton.tsx +++ b/packages/client/src/components/Toolbar/UndoButton.tsx @@ -4,7 +4,7 @@ import network from "../../lib/network"; import { useEffect, useState } from "react"; export const UndoButton = () => { - const { undo, config } = useAppContext(); + const { undo, config } = useAppContext(); /** * percentage of time left (0 <= x <= 1) */ diff --git a/packages/client/src/contexts/AppContext.tsx b/packages/client/src/contexts/AppContext.tsx index 48d6ff9..0af5145 100644 --- a/packages/client/src/contexts/AppContext.tsx +++ b/packages/client/src/contexts/AppContext.tsx @@ -65,7 +65,22 @@ interface IMapOverlay { const appContext = createContext({} as any); -export const useAppContext = () => useContext(appContext); +type WithRequiredProperty = Type & { + [Property in Key]-?: Type[Property]; +}; + +type AppContext = ConfigExists extends true + ? WithRequiredProperty + : IAppContext; + +/** + * Get app context + * + * @template ConfigExists If the config is already known to be available in this context + * @returns + */ +export const useAppContext = () => + useContext>(appContext as any); export const AppContext = ({ children }: PropsWithChildren) => { const [config, setConfig] = useState(undefined as any);