-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added ability for users to select initial cube by default (#330)
* feat: allow users to select a default starting cube * fix: updating settings if they differ from the old ones * fix: refactor styles, highlight selected cube, and close select on outside click * refactor styles to match menu schema. --------- Co-authored-by: Bryan Lundberg <bryanlundberg@outlook.com>
- Loading branch information
1 parent
26dde3b
commit a3dacbd
Showing
10 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/components/menu-settings/MenuSelectDefaultStartCube.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useTranslations } from "next-intl"; | ||
import { useRef, useState } from "react"; | ||
import { useSettingsModalStore } from "@/store/SettingsModalStore"; | ||
import useClickOutside from "@/hooks/useClickOutside"; | ||
import { useTimerStore } from "@/store/timerStore"; | ||
import { MiniatureIcon } from "../Select"; | ||
import { AnimatePresence, motion } from "framer-motion"; | ||
import loadSettings from "@/lib/loadSettings"; | ||
import { Cube } from "@/interfaces/Cube"; | ||
|
||
export default function MenuSelectDefaultStartCube() { | ||
const { settings, setSettings } = useSettingsModalStore(); | ||
const t = useTranslations("Index"); | ||
|
||
const [open, setOpen] = useState(false); | ||
const { cubes } = useTimerStore(); | ||
const componentRef = useRef<HTMLButtonElement | null>(null); | ||
|
||
const handleClose = () => { | ||
setOpen(false); | ||
}; | ||
|
||
const handleCubeSelect = (cube: Cube | null) => { | ||
const currentSettings = loadSettings(); | ||
currentSettings.preferences.defaultCube.cube = cube; | ||
setSettings(currentSettings); | ||
window.localStorage.setItem("settings", JSON.stringify(currentSettings)); | ||
handleClose(); | ||
}; | ||
|
||
const defaultCube = settings.preferences.defaultCube.cube; | ||
|
||
useClickOutside(componentRef, handleClose); | ||
|
||
return ( | ||
<div className="flex justify-between items-center mb-1 w-full"> | ||
<div className="ms-12">{t("Settings-menu.default-cube")}</div> | ||
<div className="relative me-6 w-fit mx-auto"> | ||
<button | ||
onClick={() => setOpen(!open)} | ||
ref={componentRef} | ||
className={`text-md border rounded-md transition duration-200 hover:bg-neutral-200 text-neutral-950 hover:border-neutral-400 h-10 px-2 w-40 | ||
${ | ||
open | ||
? "border-neutral-400 bg-neutral-200" | ||
: "bg-neutral-50 border-neutral-200 " | ||
}`} | ||
> | ||
<div className="flex items-center gap-2"> | ||
{defaultCube && <MiniatureIcon category={defaultCube.category} />} | ||
<div className="truncate" title={defaultCube?.name}> | ||
{defaultCube ? defaultCube.name : t("Inputs.none")} | ||
</div> | ||
</div> | ||
</button> | ||
<AnimatePresence> | ||
{open && ( | ||
<motion.div | ||
initial={{ y: 0, scale: 0.9, opacity: 0.8 }} | ||
animate={{ y: 0, scale: 1, opacity: 1 }} | ||
exit={{ x: 0, scale: 0.9, opacity: 0 }} | ||
id="list-options" | ||
className="absolute overflow-auto max-h-[400px] p-1 top-10 mt-1 right-0 w-full h-auto border rounded-md bg-neutral-200 border-neutral-400 text-neutral-900" | ||
> | ||
<div | ||
onClick={() => handleCubeSelect(null)} | ||
className={`cursor-pointer transition duration-200 p-1 select-none rounded-md ps-2 flex overflow-hidden ${ | ||
defaultCube === null | ||
? "bg-neutral-700 text-neutral-50" | ||
: "text-neutral-900 hover:bg-neutral-500 hover:text-neutral-100" | ||
}`} | ||
> | ||
<div className="overflow-hidden">{t("Inputs.none")}</div> | ||
</div> | ||
{cubes?.map((cube) => ( | ||
<div | ||
key={cube.id} | ||
onClick={() => handleCubeSelect(cube)} | ||
className={`cursor-pointer transition duration-200 p-1 select-none rounded-md ps-2 flex overflow-hidden ${ | ||
defaultCube?.id === cube.id | ||
? "bg-neutral-700 text-neutral-50" | ||
: "text-neutral-900 hover:bg-neutral-500 hover:text-neutral-100" | ||
}`} | ||
> | ||
<div className="flex justify-start gap-3"> | ||
<MiniatureIcon category={cube.category} /> | ||
<div className="overflow-hidden">{cube.name}</div> | ||
</div> | ||
</div> | ||
))} | ||
</motion.div> | ||
)} | ||
</AnimatePresence> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters