fix some types being incorrect

This commit is contained in:
Grant 2024-06-18 19:00:15 -06:00
parent 4ef7eaf33f
commit 602767889b
4 changed files with 21 additions and 5 deletions

View File

@ -27,7 +27,7 @@ const getTimeLeft = (pixels: { available: number }, config: ClientConfig) => {
};
const PlaceCountdown = () => {
const { pixels, config } = useAppContext();
const { pixels, config } = useAppContext<true>();
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<true>();
const { isOpen, onOpen, onOpenChange } = useDisclosure();
return (

View File

@ -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<true>();
const [pallete, setPallete] = useState<IPaletteContext>({});
useEffect(() => {

View File

@ -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<true>();
/**
* percentage of time left (0 <= x <= 1)
*/

View File

@ -65,7 +65,22 @@ interface IMapOverlay {
const appContext = createContext<IAppContext>({} as any);
export const useAppContext = () => useContext(appContext);
type WithRequiredProperty<Type, Key extends keyof Type> = Type & {
[Property in Key]-?: Type[Property];
};
type AppContext<ConfigExists extends boolean> = ConfigExists extends true
? WithRequiredProperty<IAppContext, "config">
: IAppContext;
/**
* Get app context
*
* @template ConfigExists If the config is already known to be available in this context
* @returns
*/
export const useAppContext = <ConfigExists extends boolean = false>() =>
useContext<AppContext<ConfigExists>>(appContext as any);
export const AppContext = ({ children }: PropsWithChildren) => {
const [config, setConfig] = useState<ClientConfig>(undefined as any);