22import styles from "../styles/Home.module.scss"
33
44import React , { useEffect , useState } from "react"
5- import { changeBarsColor , generateBarHeights , getAllBars , startAnimation } from "../utils"
5+ import {
6+ changeBarsColor ,
7+ generateBarHeights ,
8+ getAllBars ,
9+ hexToRgb ,
10+ startAnimation ,
11+ } from "../utils"
612import {
713 AppTitle ,
814 AlgorithmSelector ,
@@ -14,8 +20,8 @@ import {
1420 SortButton ,
1521 SpeedControl ,
1622} from "../components"
17- import { Spacer } from "@geist-ui/react"
18- import { INACTIVE_BAR_COLOR , sortingSpeedTable } from "../constants"
23+ import { Spacer , useTheme } from "@geist-ui/react"
24+ import { sortingSpeedTable } from "../constants"
1925import { SortingAlgorithms , SortingSpeeds , SortingState } from "../types"
2026import { default as Head } from "next/head"
2127import { AppStateContext } from "../contexts/app-state"
@@ -33,36 +39,44 @@ const getBarElementsFromBarHeights = (barHeights: number[]) => {
3339}
3440
3541const Home : React . FC = ( ) => {
42+ const { palette : geistUIPalette } = useTheme ( )
43+ const palette = {
44+ active : geistUIPalette . foreground ,
45+ inactive : geistUIPalette . errorLight ,
46+ }
47+
3648 const [ barHeights , setBarHeights ] = useState < number [ ] > ( [ ] )
37- const [ sortState , setSortState ] = useState < SortingState > ( "Sort" )
38- const [ barLength , setBarLength ] = useState ( 70 )
49+ const [ sortingState , setSortingState ] = useState < SortingState > ( "Sort" )
50+ const [ arrayLength , setArrayLength ] = useState ( 70 )
3951 const [ selectedAlgorithm , setSelectedAlgorithm ] = useState < SortingAlgorithms > (
4052 "Selection"
4153 )
4254 const [ sortingSpeed , setSortingSpeed ] = useState < keyof SortingSpeeds > ( "normal" )
4355
44- useEffect ( resetBars , [ barLength ] )
56+ useEffect ( resetBars , [ arrayLength ] )
4557 useEffect ( resetBars , [ selectedAlgorithm ] )
4658
4759 function resetBars ( ) {
48- const newBarHeights = generateBarHeights ( barLength )
60+ const newBarHeights = generateBarHeights ( arrayLength )
4961 setBarHeights ( newBarHeights )
50- setSortState ( "Sort" )
62+ setSortingState ( "Sort" )
5163 requestAnimationFrame ( ( ) => {
5264 const barsDomEl = getAllBars ( )
53- if ( barsDomEl [ 5 ] . style . backgroundColor === "rgb(17, 17, 17)" ) {
54- changeBarsColor ( barsDomEl , INACTIVE_BAR_COLOR )
65+ const sampleBg = barsDomEl [ 5 ] . style . backgroundColor
66+ if ( sampleBg === hexToRgb ( palette . active ) ) {
67+ changeBarsColor ( barsDomEl , palette . inactive )
5568 }
5669 } )
5770 }
5871
5972 function triggerAnimation ( ) {
60- setSortState ( "Sorting" )
73+ setSortingState ( "Sorting" )
6174 startAnimation ( {
62- sortingAlgorithm : selectedAlgorithm ,
6375 barHeights,
76+ palette,
77+ sortingAlgorithm : selectedAlgorithm ,
6478 sortingSpeed : sortingSpeedTable [ sortingSpeed ] ,
65- callback : ( ) => setSortState ( "Sorted" ) ,
79+ callback : ( ) => setSortingState ( "Sorted" ) ,
6680 } )
6781 }
6882
@@ -73,7 +87,7 @@ const Home: React.FC = () => {
7387 </ Head >
7488
7589 < div className = { styles . container } >
76- < AppStateContext . Provider value = { { sortingState : sortState } } >
90+ < AppStateContext . Provider value = { { sortingState } } >
7791 < div className = { styles . sidebarContainer } >
7892 < Spacer y = { 1 } />
7993 < AppTitle />
@@ -83,8 +97,8 @@ const Home: React.FC = () => {
8397 selectedAlgorithm = { selectedAlgorithm }
8498 />
8599 < ArrayLengthModifier
86- onChange = { ( value ) => setBarLength ( value ) }
87- value = { barLength }
100+ onChange = { ( value ) => setArrayLength ( value ) }
101+ value = { arrayLength }
88102 />
89103 < Spacer y = { 0.5 } />
90104 < SpeedControl
0 commit comments