import { Label } from "@tensamin/ui"; import { Switch as UISwitch, Input as UIInput } from "@tensamin/ui"; import { useEffect, useState } from "react"; import type { Storage } from "@tensamin/shared/data"; import { settingsStorageDefaults } from "@tensamin/shared/settings"; import { useStorage } from "@tensamin/storage/context"; type BooleanStorageKey = { [K in keyof Storage]: Storage[K] extends boolean ? K : never; }[keyof Storage]; type StringStorageKey = { [K in keyof Storage]: Storage[K] extends string ? K : never; }[keyof Storage]; type NumberStorageKey = { [K in keyof Storage]: Storage[K] extends number ? K : never; }[keyof Storage]; type InputProps = | { label: string; id: StringStorageKey; placeholder?: string; type?: "text"; } | { label: string; id: NumberStorageKey; placeholder?: string; type: "number"; }; export function Switch({ label, id, }: { label: string; id: keyof typeof settingsStorageDefaults & BooleanStorageKey; }) { const { save, load } = useStorage(); const [value, setValue] = useState(settingsStorageDefaults[id]); useEffect(() => { load(id).then((value) => setValue(value)); }, [id, load]); return (
{ setValue(value); save(id, value); }} />
); } export function Input({ label, id, placeholder, type }: InputProps) { const { save, load } = useStorage(); const [value, setValue] = useState( type === "number" ? 0 : "", ); useEffect(() => { load(id).then((loadedValue) => { if (type === "number") { if (typeof loadedValue === "number") { setValue(loadedValue); } return; } if (typeof loadedValue === "string") { setValue(loadedValue); } }); }, [id, load, type]); return (
{ if (type === "number") { const nextValue = Number(e.target.value); setValue(nextValue); save(id, nextValue); return; } const nextValue = e.target.value; setValue(nextValue); save(id, nextValue); }} />
); }