-
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.
move solves to historial / share solves
- Loading branch information
1 parent
1b5b184
commit 725f67a
Showing
9 changed files
with
219 additions
and
26 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
63 changes: 63 additions & 0 deletions
63
src/components/dialogs/dialog-move-historial/dialog-move-historial.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,63 @@ | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
} from "@/components/ui/dialog"; | ||
import { getAllCubes, getCubeById } from "@/db/dbOperations"; | ||
import finishSession from "@/lib/finishSession"; | ||
import { useSolveFiltersStore } from "@/store/SolvesFilters"; | ||
import { useTimerStore } from "@/store/timerStore"; | ||
import { toast } from "sonner"; | ||
|
||
export default function DialogMoveHistorial({ | ||
handleClose, | ||
}: { | ||
handleClose: () => void; | ||
}) { | ||
const { selectedCube, cubes, setCubes, setSelectedCube, setTimerStatistics } = | ||
useTimerStore(); | ||
const { tab } = useSolveFiltersStore(); | ||
|
||
const handleMoveSessionToHistorial = async () => { | ||
if (selectedCube) { | ||
await finishSession({ selectedCube, cubesDB: cubes }); | ||
const cubesDB = await getAllCubes(); | ||
setCubes(cubesDB); | ||
const currentCube = await getCubeById(selectedCube.id); | ||
setSelectedCube(currentCube); | ||
setTimerStatistics(); | ||
handleClose(); | ||
return; | ||
} | ||
|
||
toast("Unable action", { | ||
description: "Please select a cube before.", | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<DialogContent className="max-w-96 rounded-md"> | ||
<DialogHeader> | ||
<DialogTitle>Move solves to history?</DialogTitle> | ||
<DialogDescription> | ||
You will be able to access them by tapping the history switch. | ||
</DialogDescription> | ||
<DialogFooter> | ||
<div className="flex gap-1 mt-5"> | ||
<Button variant={"ghost"} onClick={handleClose}> | ||
Cancel | ||
</Button> | ||
<Button variant={"ghost"} onClick={handleMoveSessionToHistorial}> | ||
Move | ||
</Button> | ||
</div> | ||
</DialogFooter> | ||
</DialogHeader> | ||
</DialogContent> | ||
</> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { useSolveFiltersStore } from "@/store/SolvesFilters"; | ||
import { useTimerStore } from "@/store/timerStore"; | ||
import { useEffect } from "react"; | ||
|
||
const useRemoveGridHeight = () => { | ||
const { selectedCube } = useTimerStore(); | ||
const { tab } = useSolveFiltersStore(); | ||
useEffect(() => { | ||
const container = document.querySelector(".container") as HTMLElement; | ||
|
||
if (container) { | ||
container.style.setProperty("--grid-height", "auto"); | ||
} | ||
}, [selectedCube]); | ||
}, [selectedCube, tab]); | ||
}; | ||
|
||
export default useRemoveGridHeight; |
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,73 @@ | ||
import { Solve } from "@/interfaces/Solve"; | ||
import formatTime from "./formatTime"; | ||
import calculateCurrentAo from "./calculateCurrentAo"; | ||
|
||
export function shareSolves({ solves }: { solves: Solve[] }) { | ||
// retrieving last n solves | ||
const last5Solves = solves.slice(0, 5); | ||
const last12Solves = solves.slice(0, 12); | ||
|
||
// finding average of last n solves | ||
const Ao5 = calculateCurrentAo(last5Solves, 5); | ||
const Ao12 = calculateCurrentAo(last12Solves, 12); | ||
|
||
// formatting last averages | ||
const formattedAo5 = Ao5 | ||
? `${formatTime(Ao5).split(".")[0]}.${formatTime(Ao5).split(".")[1]}` | ||
: "0.0"; | ||
|
||
const formattedAo12 = Ao12 | ||
? `${formatTime(Ao12).split(".")[0]}.${formatTime(Ao12).split(".")[1]}` | ||
: "0.0"; | ||
|
||
// finding max and min time in respective solves | ||
let maxTimeAo5 = -Infinity; | ||
let minTimeAo5 = Infinity; | ||
|
||
let maxTimeAo12 = -Infinity; | ||
let minTimeAo12 = Infinity; | ||
|
||
for (let i = 0; i < last5Solves.length; i++) { | ||
maxTimeAo5 = Math.max(maxTimeAo5, last5Solves[i].time); | ||
minTimeAo5 = Math.min(minTimeAo5, last5Solves[i].time); | ||
} | ||
|
||
for (let i = 0; i < last12Solves.length; i++) { | ||
maxTimeAo12 = Math.max(maxTimeAo12, last12Solves[i].time); | ||
minTimeAo12 = Math.min(minTimeAo12, last12Solves[i].time); | ||
} | ||
|
||
// formatting each solves | ||
const formattedLast5Solves = last5Solves | ||
.map((solve, index) => { | ||
const time = `${formatTime(solve.time).split(".")[0]}.${ | ||
formatTime(solve.time).split(".")[1] | ||
}`; | ||
const scramble = `${solve.scramble}`; | ||
|
||
if (solve.time === maxTimeAo5 || solve.time === minTimeAo5) { | ||
return `${Array(4).join(" ")}${index + 1}. (${time}) ${scramble}`; | ||
} else return `${Array(4).join(" ")}${index + 1}. ${time} ${scramble}`; | ||
}) | ||
.join("\n"); | ||
|
||
const formattedLast12Solves = last12Solves | ||
.map((solve, index) => { | ||
const time = `${formatTime(solve.time).split(".")[0]}.${ | ||
formatTime(solve.time).split(".")[1] | ||
}`; | ||
const scramble = `${solve.scramble}`; | ||
|
||
if (solve.time === maxTimeAo12 || solve.time === minTimeAo12) { | ||
return `${Array(4).join(" ")}${index + 1}. (${time}) ${scramble}`; | ||
} else return `${Array(4).join(" ")}${index + 1}. ${time} ${scramble}`; | ||
}) | ||
.join("\n"); | ||
|
||
return { | ||
formattedAo5, | ||
formattedAo12, | ||
formattedLast5Solves, | ||
formattedLast12Solves, | ||
}; | ||
} |
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