Skip to content

Commit

Permalink
move queue
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfeinsilber committed Jun 13, 2024
1 parent 343191b commit e00dc40
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/screens/game/LetterQueue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,50 @@ import { lettersForToday } from '../../util/lottery/letters'
import { currentStartLetterIndex, currentWord } from '../../store/game'
import { getLettersWithPoints } from '../../util/game/getLettersWithPoints'
import { AnimatePresence, motion } from 'framer-motion'
import { useMemo } from 'react'
import { useEffect, useMemo, useState } from 'react'
import { findWordsMatchingOrder } from '../../util/solution/generateSolution'

export const LetterQueue = () => {
const word = useAtomValue(currentWord)
const startLetterIndex = useAtomValue(currentStartLetterIndex)
const letters = lettersForToday()
const storeStartLetterIndex = useAtomValue(currentStartLetterIndex)
const [startLetterIndex, setStartLetterIndex] = useState(storeStartLetterIndex)
const letters = useMemo(() => lettersForToday(), [])

const numberOfCharactersInQueueThatCanBeUsed = useMemo(() => {
const words = findWordsMatchingOrder([...letters].splice(startLetterIndex))
return words.length
}, [letters.length, startLetterIndex])

const lettersWithPoints = getLettersWithPoints({
letters,
currentStartLetterIndex: startLetterIndex,
currentWord: word
})
const lettersWithPoints = useMemo(
() =>
getLettersWithPoints({
letters,
currentStartLetterIndex: storeStartLetterIndex,
currentWord: word
}),
[letters, storeStartLetterIndex, word]
)

const possibleLetters = useMemo(() => {
const start = startLetterIndex
const end = start + numberOfCharactersInQueueThatCanBeUsed

return letters.filter((_, idx) => {
return idx >= start && idx < end
})
}, [letters, numberOfCharactersInQueueThatCanBeUsed, startLetterIndex])

const filteredLetters = useMemo(
() =>
letters.filter((_, idx) => {
return idx >= storeStartLetterIndex
}),
[letters, storeStartLetterIndex]
)

const filteredLetters = letters.filter((_, idx) => {
return idx >= startLetterIndex
})
useEffect(() => {
setStartLetterIndex(storeStartLetterIndex)
}, [storeStartLetterIndex])

return (
<AnimatePresence>
Expand All @@ -36,14 +58,18 @@ export const LetterQueue = () => {
layout
className="flex shrink-0 flex-col items-center"
key={'queue' + letter + startLetterIndex}
style={{ cursor: 'pointer' }}
onClick={() => {
setStartLetterIndex(storeStartLetterIndex + idx)
}}
>
<Letter
size={50}
letter={letter}
status={
lettersWithPoints.includes(letter)
? ILetterStatus.point
: idx < numberOfCharactersInQueueThatCanBeUsed
: possibleLetters.includes(letter)
? ILetterStatus.possible
: ILetterStatus.default
}
Expand Down

0 comments on commit e00dc40

Please sign in to comment.