|
| 1 | +import { useCallback, useContext, useEffect, useMemo } from 'react' |
| 2 | + |
| 3 | +import { useWindowSize } from 'src/hooks' |
| 4 | +import { TuringTreeControllerContext } from 'src/contexts/' |
| 5 | +import { FlipIcon } from 'src/components/Icons/icons' |
| 6 | + |
| 7 | +interface Props { |
| 8 | + setCurrentMove?: (move: [string, string] | null) => void |
| 9 | +} |
| 10 | + |
| 11 | +export const TuringBoardController: React.FC<Props> = ({ |
| 12 | + setCurrentMove, |
| 13 | +}: Props) => { |
| 14 | + const { width } = useWindowSize() |
| 15 | + const { |
| 16 | + orientation, |
| 17 | + setOrientation, |
| 18 | + setCurrentIndex, |
| 19 | + currentIndex, |
| 20 | + plyCount, |
| 21 | + goToRootNode, |
| 22 | + goToPreviousNode, |
| 23 | + goToNextNode, |
| 24 | + gameTree, |
| 25 | + } = useContext(TuringTreeControllerContext) |
| 26 | + |
| 27 | + const toggleBoardOrientation = useCallback(() => { |
| 28 | + setOrientation(orientation === 'white' ? 'black' : 'white') |
| 29 | + }, [orientation, setOrientation]) |
| 30 | + |
| 31 | + const hasPrevious = useMemo(() => currentIndex > 0, [currentIndex]) |
| 32 | + |
| 33 | + const hasNext = useMemo( |
| 34 | + () => currentIndex < plyCount - 1, |
| 35 | + [currentIndex, plyCount], |
| 36 | + ) |
| 37 | + |
| 38 | + const getFirst = useCallback(() => { |
| 39 | + goToRootNode() |
| 40 | + if (setCurrentMove) setCurrentMove(null) |
| 41 | + }, [goToRootNode, setCurrentMove]) |
| 42 | + |
| 43 | + const getPrevious = useCallback(() => { |
| 44 | + goToPreviousNode() |
| 45 | + if (setCurrentMove) setCurrentMove(null) |
| 46 | + }, [goToPreviousNode, setCurrentMove]) |
| 47 | + |
| 48 | + const getNext = useCallback(() => { |
| 49 | + goToNextNode() |
| 50 | + if (setCurrentMove) setCurrentMove(null) |
| 51 | + }, [goToNextNode, setCurrentMove]) |
| 52 | + |
| 53 | + const getLast = useCallback(() => { |
| 54 | + const mainLine = gameTree.getMainLine() |
| 55 | + if (mainLine.length > 0) { |
| 56 | + setCurrentIndex(mainLine.length - 1) |
| 57 | + } |
| 58 | + if (setCurrentMove) setCurrentMove(null) |
| 59 | + }, [gameTree, setCurrentIndex, setCurrentMove]) |
| 60 | + |
| 61 | + useEffect(() => { |
| 62 | + if (width <= 670) return |
| 63 | + const onKeyDown = (e: KeyboardEvent) => { |
| 64 | + switch (e.key) { |
| 65 | + case 'Left': |
| 66 | + case 'ArrowLeft': |
| 67 | + if (hasPrevious) getPrevious() |
| 68 | + break |
| 69 | + case 'Right': |
| 70 | + case 'ArrowRight': |
| 71 | + if (hasNext) getNext() |
| 72 | + break |
| 73 | + case 'Down': |
| 74 | + case 'ArrowDown': |
| 75 | + if (hasNext) getLast() |
| 76 | + break |
| 77 | + case 'Up': |
| 78 | + case 'ArrowUp': |
| 79 | + if (hasPrevious) getFirst() |
| 80 | + break |
| 81 | + case 'f': |
| 82 | + toggleBoardOrientation() |
| 83 | + break |
| 84 | + default: |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + window.addEventListener('keydown', onKeyDown) |
| 89 | + return () => window.removeEventListener('keydown', onKeyDown) |
| 90 | + }, [ |
| 91 | + getFirst, |
| 92 | + getLast, |
| 93 | + getNext, |
| 94 | + getPrevious, |
| 95 | + hasNext, |
| 96 | + hasPrevious, |
| 97 | + toggleBoardOrientation, |
| 98 | + width, |
| 99 | + currentIndex, |
| 100 | + ]) |
| 101 | + |
| 102 | + return ( |
| 103 | + <div className="flex w-full flex-row items-center gap-[1px] rounded"> |
| 104 | + <button |
| 105 | + onClick={toggleBoardOrientation} |
| 106 | + className="flex h-7 flex-1 items-center justify-center rounded-sm bg-button-secondary transition duration-200 hover:bg-human-3 disabled:bg-button-secondary/40" |
| 107 | + > |
| 108 | + {FlipIcon} |
| 109 | + </button> |
| 110 | + <button |
| 111 | + onClick={getFirst} |
| 112 | + disabled={!hasPrevious} |
| 113 | + className="flex h-7 flex-1 items-center justify-center rounded-sm bg-button-secondary transition duration-200 hover:bg-human-3 disabled:bg-button-secondary/40" |
| 114 | + > |
| 115 | + ‹‹‹ |
| 116 | + </button> |
| 117 | + <button |
| 118 | + onClick={getPrevious} |
| 119 | + disabled={!hasPrevious} |
| 120 | + className="flex h-7 flex-1 items-center justify-center rounded-sm bg-button-secondary transition duration-200 hover:bg-human-3 disabled:bg-button-secondary/40" |
| 121 | + > |
| 122 | + ‹ |
| 123 | + </button> |
| 124 | + <button |
| 125 | + onClick={getNext} |
| 126 | + disabled={!hasNext} |
| 127 | + className="flex h-7 flex-1 items-center justify-center rounded-sm bg-button-secondary transition duration-200 hover:bg-human-3 disabled:bg-button-secondary/40" |
| 128 | + > |
| 129 | + › |
| 130 | + </button> |
| 131 | + <button |
| 132 | + onClick={getLast} |
| 133 | + disabled={!hasNext} |
| 134 | + className="flex h-7 flex-1 items-center justify-center rounded-sm bg-button-secondary transition duration-200 hover:bg-human-3 disabled:bg-button-secondary/40" |
| 135 | + > |
| 136 | + ››› |
| 137 | + </button> |
| 138 | + </div> |
| 139 | + ) |
| 140 | +} |
0 commit comments