Skip to content
Palette.tsx 2.45 KiB
Newer Older
Grant's avatar
Grant committed
import { useEffect } from "react";
Grant's avatar
Grant committed
import { useAppContext } from "../../contexts/AppContext";
import { Canvas } from "../../lib/canvas";
Grant's avatar
Grant committed
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faXmark } from "@fortawesome/free-solid-svg-icons";
Grant's avatar
Grant committed
import { KeybindManager } from "../../lib/keybinds";
Grant's avatar
Grant committed
import { Button, Link } from "@nextui-org/react";
Grant's avatar
Grant committed

Grant's avatar
Grant committed
export const Palette = () => {
Grant's avatar
Grant committed
  const { config, user, cursor, setCursor } = useAppContext<true>();
Grant's avatar
Grant committed

  useEffect(() => {
Grant's avatar
Grant committed
    Canvas.instance?.updateCursor(cursor.color);
  }, [cursor]);
Grant's avatar
Grant committed

Grant's avatar
Grant committed
  useEffect(() => {
    const handleDeselect = () => {
      setCursor((v) => ({
        ...v,
        color: undefined,
      }));
    };

    KeybindManager.addListener("DESELECT_COLOR", handleDeselect);

    return () => {
      KeybindManager.removeListener("DESELECT_COLOR", handleDeselect);
    };
  }, []);

Grant's avatar
Grant committed
  return (
Ategon Dev's avatar
Ategon Dev committed
    <div id="pallete" className="bg-[#fff] dark:bg-[#000]">
Grant's avatar
Grant committed
      <div className="pallete-colors">
        <button
          aria-label="Deselect Color"
          className="pallete-color--deselect"
          title="Deselect Color"
          onClick={() => {
Grant's avatar
Grant committed
            setCursor(({ color, ...cursor }) => {
              return cursor;
Grant's avatar
Grant committed
            });
          }}
        >
          <FontAwesomeIcon icon={faXmark} />
        </button>
        {config.pallete.colors.map((color) => (
          <button
            key={color.id}
            aria-label={color.name}
Grant's avatar
Grant committed
            className={["pallete-color", color.id === cursor.color && "active"]
Grant's avatar
Grant committed
              .filter((a) => a)
              .join(" ")}
            style={{
              backgroundColor: "#" + color.hex,
            }}
            title={color.name}
            onClick={() => {
Grant's avatar
Grant committed
              setCursor((cursor) => {
Grant's avatar
Grant committed
                return {
Grant's avatar
Grant committed
                  ...cursor,
Grant's avatar
Grant committed
                  color: color.id,
                };
              });
            }}
          ></button>
        ))}
      </div>

      {!user && (
        <div className="pallete-user-overlay">
          {import.meta.env.VITE_INCLUDE_EVENT_INFO ? (
            <>The event has ended</>
Ategon Dev's avatar
Ategon Dev committed
            <div className="flex gap-3 items-center">
              You are not logged in
              <Button
                as={Link}
                href="/api/login"
                className="user-login"
                variant="faded"
              >
Grant's avatar
Grant committed
              </Button>
Ategon Dev's avatar
Ategon Dev committed
            </div>
Grant's avatar
Grant committed
        </div>
      )}
    </div>
  );
};