Commit 60276788 authored by Grant's avatar Grant
Browse files

fix some types being incorrect

parent 4ef7eaf3
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -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 (
+1 −1
Original line number Diff line number Diff line
@@ -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(() => {
+1 −1
Original line number Diff line number Diff line
@@ -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)
   */
+16 −1
Original line number Diff line number Diff line
@@ -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);