-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99dce62
commit a6011d9
Showing
12 changed files
with
336 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { useVirtualizer } from '@tanstack/react-virtual'; | ||
import { useLiveQuery } from 'dexie-react-hooks'; | ||
import { useMemo, useRef, useState } from 'react'; | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
} from '@/components/ui/dialog'; | ||
import { Solve, db } from '@/lib/db'; | ||
import { cn } from '@/lib/utils'; | ||
import { Button } from '../ui/button'; | ||
import { Plus, Trash2, X } from 'lucide-react'; | ||
import DrawScramble from './drawScramble'; | ||
import { ScrollArea } from '../ui/scroll-area'; | ||
import { Separator } from '../ui/separator'; | ||
|
||
function convertTimeToText(time: number) { | ||
if (time == -1) return 'DNF'; | ||
|
||
const minutes = Math.floor(time / 60000); | ||
const seconds = Math.floor((time % 60000) / 1000); | ||
const hundredths = Math.floor((time % 1000) / 10); | ||
|
||
let res = minutes > 0 ? `${minutes}:` : ''; | ||
res += `${seconds < 10 && minutes > 0 ? '0' : ''}${seconds}.`; | ||
res += `${hundredths < 10 ? '0' : ''}${hundredths}`; | ||
|
||
return res; | ||
} | ||
|
||
function SolveDialog({ solve, idx, close }: { solve: Solve, idx: number, close: (open: boolean) => void }) { | ||
return ( | ||
<Dialog open={true} onOpenChange={close}> | ||
<DialogContent className="sm:max-w-[425px]"> | ||
<DialogHeader> | ||
<DialogTitle>Solve #{idx}</DialogTitle> | ||
<DialogDescription> | ||
{new Date(solve.timeStamp).toLocaleString()} | ||
</DialogDescription> | ||
</DialogHeader> | ||
<h2 className='text-3xl font-bold'>{convertTimeToText(solve.time)}</h2> | ||
<h2 className='text-l font-bold'>{solve.scramble}</h2> | ||
<div className="flex"> | ||
<DrawScramble scramble={solve.scramble} className='w-full h-32 mx-auto' /> | ||
<DrawScramble scramble={solve.scramble + ' ' + solve.solution} className='w-full h-32 mx-auto' /> | ||
</div> | ||
<ul className='rounded-md border p-2'> | ||
<li className="font-medium">Algs in solve:</li> | ||
<ScrollArea className='h-64'> | ||
{solve.parsed.map((alg, i) => ( | ||
<li key={i + " " + alg}> | ||
{alg} | ||
</li> | ||
))} | ||
</ScrollArea> | ||
</ul> | ||
<DialogFooter> | ||
<Button variant="destructive" type="submit">Delete</Button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} | ||
|
||
export default function TimeList({ className }: { className: string }) { | ||
const parentRef = useRef<HTMLDivElement | null>(null); | ||
|
||
const data = useLiveQuery(() => db.solves.reverse().toArray()); | ||
|
||
const fakeFullData = useMemo( | ||
() => Array.from({ length: 5 }, () => data ?? []).flat(), | ||
[data] | ||
); | ||
|
||
const rowVirtualizer = useVirtualizer({ | ||
count: fakeFullData.length, | ||
getScrollElement: () => parentRef.current, | ||
estimateSize: () => 45, | ||
}); | ||
|
||
const items = rowVirtualizer.getVirtualItems(); | ||
|
||
const classes = cn('no-scrollbar', className); | ||
|
||
const padAmountForIdx = fakeFullData.length.toString().length; | ||
|
||
const [selectedSolve, setSelectedSolve] = useState<number | null>(null); | ||
|
||
return ( | ||
<div | ||
ref={parentRef} | ||
className={classes} | ||
style={{ | ||
overflowY: 'auto', | ||
contain: 'strict', | ||
}} | ||
> | ||
<div | ||
style={{ | ||
height: rowVirtualizer.getTotalSize(), | ||
width: '100%', | ||
position: 'relative', | ||
}} | ||
> | ||
<div | ||
style={{ | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
transform: `translateY(${items[0]?.start ?? 0}px)`, | ||
}} | ||
> | ||
{rowVirtualizer.getVirtualItems().map(item => { | ||
const solve = fakeFullData[item.index]; | ||
const timeText = convertTimeToText(solve.time); | ||
|
||
const reverseIdx = fakeFullData.length - item.index - 1; | ||
|
||
let mo3: string | undefined; | ||
if (reverseIdx < 2) mo3 = '-'; | ||
else { | ||
const prevSolves = fakeFullData.slice(reverseIdx - 2, reverseIdx); | ||
const mean = | ||
prevSolves.reduce((acc, cur) => acc + cur.time, 0) / 3; | ||
mo3 = convertTimeToText(mean); | ||
} | ||
|
||
return ( | ||
<div | ||
key={item.key} | ||
className="flex gap-2 rounded-md font-mono py-1 pl-2 my-1 hover:bg-gray-900 cursor-pointer" | ||
ref={rowVirtualizer.measureElement} | ||
data-index={item.index} | ||
onClick={() => setSelectedSolve(item.index)} | ||
> | ||
<div className="text-gray-500 my-auto"> | ||
<pre>{reverseIdx.toString().padStart(padAmountForIdx, ' ')}.</pre> | ||
</div> | ||
<div className="text-right px-1 my-auto"> | ||
<pre>{timeText.padStart(7, ' ')}</pre> | ||
</div> | ||
<div className="text-right px-1 my-auto"> | ||
<pre>{mo3.padStart(7, ' ')}</pre> | ||
</div> | ||
<div className="text-right px-1 grow"> | ||
<Button | ||
variant="ghost" | ||
size="sm" | ||
> | ||
<X className="size-4" /> | ||
</Button> | ||
<Button | ||
variant="ghost" | ||
size="sm" | ||
> | ||
<Plus className="size-4" /> | ||
</Button> | ||
<Button | ||
variant="ghost" | ||
size="sm" | ||
> | ||
<Trash2 className="size-4" /> | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
{selectedSolve !== null && ( | ||
<SolveDialog | ||
solve={fakeFullData[selectedSolve]} | ||
idx={fakeFullData.length - 1 - selectedSolve} | ||
close={(open) => setSelectedSolve(open ? selectedSolve : null)} | ||
/> | ||
)} | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as React from "react" | ||
import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area" | ||
|
||
import { cn } from "@/lib/utils" | ||
|
||
const ScrollArea = React.forwardRef< | ||
React.ElementRef<typeof ScrollAreaPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root> | ||
>(({ className, children, ...props }, ref) => ( | ||
<ScrollAreaPrimitive.Root | ||
ref={ref} | ||
className={cn("relative overflow-hidden", className)} | ||
{...props} | ||
> | ||
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]"> | ||
{children} | ||
</ScrollAreaPrimitive.Viewport> | ||
<ScrollBar /> | ||
<ScrollAreaPrimitive.Corner /> | ||
</ScrollAreaPrimitive.Root> | ||
)) | ||
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName | ||
|
||
const ScrollBar = React.forwardRef< | ||
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>, | ||
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar> | ||
>(({ className, orientation = "vertical", ...props }, ref) => ( | ||
<ScrollAreaPrimitive.ScrollAreaScrollbar | ||
ref={ref} | ||
orientation={orientation} | ||
className={cn( | ||
"flex touch-none select-none transition-colors", | ||
orientation === "vertical" && | ||
"h-full w-2.5 border-l border-l-transparent p-[1px]", | ||
orientation === "horizontal" && | ||
"h-2.5 flex-col border-t border-t-transparent p-[1px]", | ||
className | ||
)} | ||
{...props} | ||
> | ||
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" /> | ||
</ScrollAreaPrimitive.ScrollAreaScrollbar> | ||
)) | ||
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName | ||
|
||
export { ScrollArea, ScrollBar } |
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,28 @@ | ||
import * as SeparatorPrimitive from '@radix-ui/react-separator'; | ||
import * as React from 'react'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
const Separator = React.forwardRef< | ||
React.ElementRef<typeof SeparatorPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof SeparatorPrimitive.Root> | ||
>( | ||
( | ||
{ className, orientation = 'horizontal', decorative = true, ...props }, | ||
ref | ||
) => ( | ||
<SeparatorPrimitive.Root | ||
ref={ref} | ||
decorative={decorative} | ||
orientation={orientation} | ||
className={cn( | ||
'shrink-0 bg-border', | ||
orientation === 'horizontal' ? 'h-[1px] w-full' : 'h-full w-[1px]', | ||
className | ||
)} | ||
{...props} | ||
/> | ||
) | ||
); | ||
Separator.displayName = SeparatorPrimitive.Root.displayName; | ||
|
||
export { Separator }; |
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
Oops, something went wrong.