11"use client" ;
22
3- import { useQuery } from "@tanstack/react-query" ;
4- import { Check , Copy , Loader2 } from "lucide-react" ;
3+ import { Check , Copy } from "lucide-react" ;
54import { useTranslations } from "next-intl" ;
6- import { useState } from "react" ;
5+ import { type JSX , useState } from "react" ;
76import {
87 Tabs ,
98 TabsContent ,
@@ -18,33 +17,26 @@ import {
1817 CardHeader ,
1918 CardTitle ,
2019} from "@/components/ui/card" ;
21- import {
22- type Algorithm ,
23- cn ,
24- codeHighlightOptions ,
25- getAlgorithmCode ,
26- } from "@/lib/utils" ;
20+ import { type Algorithm , cn , getAlgorithmCode } from "@/lib/utils" ;
2721import { RippleButton } from "./animate-ui/ripple-button" ;
2822
2923interface AlgorithmExplanationProps {
3024 algorithm : Algorithm ;
3125 iterations : number ;
3226 swaps : number ;
27+ highlightedAlgorithmCode : JSX . Element ;
3328 className ?: string ;
3429}
3530
3631export default function AlgorithmExplanation ( {
3732 algorithm,
3833 iterations,
3934 swaps,
35+ highlightedAlgorithmCode,
4036 className,
4137} : AlgorithmExplanationProps ) {
4238 const t = useTranslations ( "algorithm-explanation" ) ;
4339
44- const { data, isPending, isError } = useQuery (
45- codeHighlightOptions ( algorithm ) ,
46- ) ;
47-
4840 const [ copied , setCopied ] = useState < string | null > ( null ) ;
4941
5042 const copyCode = ( code : string ) => {
@@ -260,47 +252,35 @@ export default function AlgorithmExplanation({
260252 </ TabsContent >
261253
262254 < TabsContent value = "code" className = "group relative mt-4" >
263- { isPending ? (
264- < p className = "text-muted-foreground text-xs" >
265- < Loader2 className = "mx-auto animate-spin text-primary" />
266- </ p >
267- ) : isError ? (
268- < p className = "text-muted-foreground text-xs" >
269- There was an error loading the code.
270- </ p >
271- ) : (
272- data
273- ) }
274- { data && (
275- < RippleButton
276- onClick = { ( ) => copyCode ( getAlgorithmCode ( algorithm ) ) }
277- variant = "ghost"
278- size = "sm"
279- className = { cn (
280- "absolute top-1 right-1 flex size-8 scale-0 items-center justify-center bg-background/60 p-0 transition-all duration-200 ease-snappy touch-only:group-focus-within:scale-100 touch-only:group-focus-within:opacity-100 group-hover:scale-100 group-hover:opacity-100" ,
281- copied === getAlgorithmCode ( algorithm ) &&
282- "scale-100 opacity-100" ,
283- ) }
284- type = "button"
285- >
286- < div className = "relative size-4" >
287- < Check
288- className = { cn (
289- "absolute inset-0 scale-0 text-primary opacity-0 transition-all duration-200 ease-snappy" ,
290- copied === getAlgorithmCode ( algorithm ) &&
291- "scale-100 opacity-100" ,
292- ) }
293- />
294- < Copy
295- className = { cn (
296- "absolute inset-0 scale-100 opacity-100 transition-all duration-200 ease-snappy" ,
297- copied === getAlgorithmCode ( algorithm ) &&
298- "scale-0 opacity-0" ,
299- ) }
300- />
301- </ div >
302- </ RippleButton >
303- ) }
255+ { highlightedAlgorithmCode }
256+ < RippleButton
257+ onClick = { ( ) => copyCode ( getAlgorithmCode ( algorithm ) ) }
258+ variant = "ghost"
259+ size = "sm"
260+ className = { cn (
261+ "absolute top-1 right-1 flex size-8 scale-0 items-center justify-center bg-background/60 p-0 transition-all duration-200 ease-snappy focus-visible:scale-100 focus-visible:opacity-100 touch-only:group-focus-within:scale-100 touch-only:group-focus-within:opacity-100 group-hover:scale-100 group-hover:opacity-100" ,
262+ copied === getAlgorithmCode ( algorithm ) &&
263+ "scale-100 opacity-100" ,
264+ ) }
265+ type = "button"
266+ >
267+ < div className = "relative size-4" >
268+ < Check
269+ className = { cn (
270+ "absolute inset-0 scale-0 text-primary opacity-0 transition-all duration-200 ease-snappy" ,
271+ copied === getAlgorithmCode ( algorithm ) &&
272+ "scale-100 opacity-100" ,
273+ ) }
274+ />
275+ < Copy
276+ className = { cn (
277+ "absolute inset-0 scale-100 opacity-100 transition-all duration-200 ease-snappy" ,
278+ copied === getAlgorithmCode ( algorithm ) &&
279+ "scale-0 opacity-0" ,
280+ ) }
281+ />
282+ </ div >
283+ </ RippleButton >
304284 </ TabsContent >
305285 </ TabsContents >
306286 </ Tabs >
0 commit comments