import { useContext, useEffect, useRef, useState } from "react"; import { Button, Checkbox, CheckboxGroup, Col, Divider, Drawer, Grid, HStack, Image, Row, SelectPicker, Slider, Text, VStack, } from "rsuite"; import { SettingsContext } from "../App"; import { formatNumberSuffix } from "../lib/useDate"; import { WallpaperIcon } from "lucide-react"; import wallpapers from "../lib/wallpapers.json" with { type: "json" }; export default function Settings({ open, setOpen, }: { open: boolean; setOpen: (open: boolean) => void; }) { const { settings, setSettings } = useContext(SettingsContext); const [options, setOptions] = useState<(string | number)[]>([ "show-seconds", "show-date", "center-date", "vignette", "auto-text", ]); const [wallpaperOpen, setWallpaperOpen] = useState(false); const restoredRef = useRef(false); const [timeSize, setTimeSize] = useState(100); const [dateSize, setDateSize] = useState(60); const [dateFormat, setDateFormat] = useState("short"); const [fontWeight, setFontWeight] = useState(600); const [fontFamily, setFontFamily] = useState("Martian Mono"); const [backgroundBrightness, setBackgroundBrightness] = useState(100); const [wallpaper, setWallpaper] = useState( "https://hcpss-space-assets.s3.us-east-1.amazonaws.com/wallpapers/zhang-liven-BiLh-YznD0I-unsplash.jpg", ); useEffect(() => { setSettings((prev) => { const newSettings = { ...prev, options: options as string[], timeSize, dateSize, dateFormat, fontWeight, fontFamily, backgroundBrightness, wallpaper, }; return newSettings; }); }, [ options, timeSize, dateSize, dateFormat, fontWeight, fontFamily, backgroundBrightness, wallpaper, ]); const today = new Date(); const weekday = today.toLocaleDateString("en-us", { weekday: "long" }); const dateFormats = [ { label: `${weekday}, ${today.toLocaleDateString("en-us", { month: "long" })} ${formatNumberSuffix(today.getDate())}`, value: "full", }, { label: `${today.toLocaleDateString("en-us", { month: "long" })} ${today.getDate()}`, value: "short", }, { label: `${today.getMonth() + 1}/${today.getDate()}/${today.getFullYear().toString().slice(-2)}`, value: "numeric", }, ]; const fonts = [ { label: "Martian Mono", value: "Martian Mono" }, { label: "Geist Mono", value: "Geist Mono" }, { label: "Aleo", value: "Aleo" }, ]; useEffect(() => { const savedSettings = localStorage.getItem("clock-settings"); if (savedSettings) { const settings = JSON.parse(savedSettings); setSettings(settings); setOptions(settings.options); setTimeSize(settings.timeSize); setDateSize(settings.dateSize); setDateFormat(settings.dateFormat); setFontWeight(settings.fontWeight); setFontFamily(settings.fontFamily); setBackgroundBrightness(settings.backgroundBrightness); setWallpaper(settings.wallpaper); } restoredRef.current = true; }, []); useEffect(() => { if (restoredRef.current) { localStorage.setItem("clock-settings", JSON.stringify(settings)); } }, [settings]); return ( <> { setWallpaperOpen(false); setOpen(true); }} size={"xs"} > Wallpaper {wallpapers.map((wallpaper) => ( setWallpaper(wallpaper)} /> ))} setOpen(false)} size={"xs"}> Settings Show seconds Show AM/PM Show date Center date text Vignette Hide buttons in fullscreen Auto text size {!options.includes("auto-text") && ( Time Date )} Date format setDateFormat(v!)} cleanable={false} /> Font family setFontFamily(v!)} cleanable={false} /> Font weight Background brightness ); }