|
| 1 | +import React, { |
| 2 | + useRef, |
| 3 | + useMemo, |
| 4 | + Dispatch, |
| 5 | + useState, |
| 6 | + useEffect, |
| 7 | + useContext, |
| 8 | + SetStateAction, |
| 9 | +} from 'react' |
| 10 | +import { motion } from 'framer-motion' |
| 11 | + |
| 12 | +import { Tournament } from 'src/components' |
| 13 | +import { AnalysisListContext } from 'src/contexts' |
| 14 | + |
| 15 | +interface AnalysisGameListProps { |
| 16 | + currentId: string[] | null |
| 17 | + loadNewTournamentGame: ( |
| 18 | + newId: string[], |
| 19 | + setCurrentMove?: Dispatch<SetStateAction<number>>, |
| 20 | + ) => Promise<void> |
| 21 | + loadNewLichessGames: ( |
| 22 | + id: string, |
| 23 | + pgn: string, |
| 24 | + setCurrentMove?: Dispatch<SetStateAction<number>>, |
| 25 | + ) => Promise<void> |
| 26 | + loadNewUserGames: ( |
| 27 | + id: string, |
| 28 | + type: 'play' | 'hand' | 'brain', |
| 29 | + setCurrentMove?: Dispatch<SetStateAction<number>>, |
| 30 | + ) => Promise<void> |
| 31 | +} |
| 32 | + |
| 33 | +export const ClientAnalysisGameList: React.FC<AnalysisGameListProps> = ({ |
| 34 | + currentId, |
| 35 | + loadNewTournamentGame, |
| 36 | + loadNewLichessGames, |
| 37 | + loadNewUserGames, |
| 38 | +}) => { |
| 39 | + const { |
| 40 | + analysisPlayList, |
| 41 | + analysisHandList, |
| 42 | + analysisBrainList, |
| 43 | + analysisLichessList, |
| 44 | + analysisTournamentList, |
| 45 | + } = useContext(AnalysisListContext) |
| 46 | + |
| 47 | + const listKeys = useMemo(() => { |
| 48 | + return analysisTournamentList |
| 49 | + ? Array.from(analysisTournamentList.keys()).sort( |
| 50 | + (a, b) => |
| 51 | + b?.split('---')?.[1]?.localeCompare(a?.split('---')?.[1] ?? '') ?? |
| 52 | + 0, |
| 53 | + ) |
| 54 | + : [] |
| 55 | + }, [analysisTournamentList]) |
| 56 | + |
| 57 | + const initialOpenIndex = useMemo(() => { |
| 58 | + if (analysisTournamentList && currentId) { |
| 59 | + return listKeys.map((m) => m?.split('---')?.[0]).indexOf(currentId[0]) |
| 60 | + } else { |
| 61 | + return null |
| 62 | + } |
| 63 | + }, [analysisTournamentList, currentId, listKeys]) |
| 64 | + |
| 65 | + const [selected, setSelected] = useState< |
| 66 | + 'tournament' | 'pgn' | 'play' | 'hand' | 'brain' |
| 67 | + >('pgn') |
| 68 | + const [loadingIndex, setLoadingIndex] = useState<number | null>(null) |
| 69 | + const [openIndex, setOpenIndex] = useState<number | null>(initialOpenIndex) |
| 70 | + |
| 71 | + const openElement = useRef<HTMLDivElement>(null) |
| 72 | + const selectedGameElement = useRef<HTMLButtonElement>(null) |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + setLoadingIndex(null) |
| 76 | + }, [selected]) |
| 77 | + |
| 78 | + return analysisTournamentList ? ( |
| 79 | + <div className="flex flex-col items-start justify-start overflow-hidden rounded bg-background-1"> |
| 80 | + <div className="flex w-full flex-col"> |
| 81 | + <div className="grid select-none grid-cols-5 border-b-2 border-white border-opacity-10"> |
| 82 | + <Header |
| 83 | + label="Play" |
| 84 | + name="play" |
| 85 | + selected={selected} |
| 86 | + setSelected={setSelected} |
| 87 | + /> |
| 88 | + <Header |
| 89 | + label="Hand" |
| 90 | + name="hand" |
| 91 | + selected={selected} |
| 92 | + setSelected={setSelected} |
| 93 | + /> |
| 94 | + <Header |
| 95 | + label="Brain" |
| 96 | + name="brain" |
| 97 | + selected={selected} |
| 98 | + setSelected={setSelected} |
| 99 | + /> |
| 100 | + <Header |
| 101 | + label="Lichess" |
| 102 | + name="pgn" |
| 103 | + selected={selected} |
| 104 | + setSelected={setSelected} |
| 105 | + /> |
| 106 | + <Header |
| 107 | + label="WC Matches" |
| 108 | + name="tournament" |
| 109 | + selected={selected} |
| 110 | + setSelected={setSelected} |
| 111 | + /> |
| 112 | + </div> |
| 113 | + <div className="red-scrollbar flex max-h-64 flex-col overflow-y-scroll md:max-h-[60vh]"> |
| 114 | + {selected === 'tournament' ? ( |
| 115 | + <> |
| 116 | + {listKeys.map((id, i) => ( |
| 117 | + <Tournament |
| 118 | + key={i} |
| 119 | + id={id} |
| 120 | + index={i} |
| 121 | + openIndex={openIndex} |
| 122 | + currentId={currentId} |
| 123 | + openElement={openElement as React.RefObject<HTMLDivElement>} |
| 124 | + setOpenIndex={setOpenIndex} |
| 125 | + loadingIndex={loadingIndex} |
| 126 | + setLoadingIndex={setLoadingIndex} |
| 127 | + selectedGameElement={ |
| 128 | + selectedGameElement as React.RefObject<HTMLButtonElement> |
| 129 | + } |
| 130 | + loadNewTournamentGame={loadNewTournamentGame} |
| 131 | + analysisTournamentList={analysisTournamentList} |
| 132 | + /> |
| 133 | + ))} |
| 134 | + </> |
| 135 | + ) : ( |
| 136 | + <> |
| 137 | + {(selected === 'play' |
| 138 | + ? analysisPlayList |
| 139 | + : selected === 'hand' |
| 140 | + ? analysisHandList |
| 141 | + : selected === 'brain' |
| 142 | + ? analysisBrainList |
| 143 | + : analysisLichessList |
| 144 | + ).map((game, index) => { |
| 145 | + const selectedGame = currentId && currentId[0] === game.id |
| 146 | + return ( |
| 147 | + <button |
| 148 | + key={index} |
| 149 | + onClick={async () => { |
| 150 | + setLoadingIndex(index) |
| 151 | + if (game.type === 'pgn') { |
| 152 | + await loadNewLichessGames(game.id, game.pgn as string) |
| 153 | + } else { |
| 154 | + await loadNewUserGames( |
| 155 | + game.id, |
| 156 | + game.type as 'play' | 'hand' | 'brain', |
| 157 | + ) |
| 158 | + } |
| 159 | + setLoadingIndex(null) |
| 160 | + }} |
| 161 | + className={`group flex w-full cursor-pointer items-center gap-2 pr-2 ${selectedGame ? 'bg-background-2 font-bold' : index % 2 === 0 ? 'bg-background-1/30 hover:bg-background-2' : 'bg-background-1/10 hover:bg-background-2'}`} |
| 162 | + > |
| 163 | + <div |
| 164 | + className={`flex h-full w-10 items-center justify-center py-1 ${selectedGame ? 'bg-background-3' : 'bg-background-2 group-hover:bg-white/5'}`} |
| 165 | + > |
| 166 | + <p className="text-secondary">{index + 1}</p> |
| 167 | + </div> |
| 168 | + <div className="flex flex-1 items-center justify-between overflow-hidden py-1"> |
| 169 | + <p className="overflow-hidden text-ellipsis whitespace-nowrap text-primary"> |
| 170 | + {game.label} |
| 171 | + </p> |
| 172 | + <p className="whitespace-nowrap text-secondary"> |
| 173 | + {game.result} |
| 174 | + </p> |
| 175 | + </div> |
| 176 | + </button> |
| 177 | + ) |
| 178 | + })} |
| 179 | + </> |
| 180 | + )} |
| 181 | + </div> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + ) : null |
| 185 | +} |
| 186 | + |
| 187 | +function Header({ |
| 188 | + name, |
| 189 | + label, |
| 190 | + selected, |
| 191 | + setSelected, |
| 192 | +}: { |
| 193 | + label: string |
| 194 | + name: 'tournament' | 'play' | 'hand' | 'brain' | 'pgn' |
| 195 | + selected: 'tournament' | 'play' | 'hand' | 'brain' | 'pgn' |
| 196 | + setSelected: (name: 'tournament' | 'play' | 'hand' | 'brain' | 'pgn') => void |
| 197 | +}) { |
| 198 | + return ( |
| 199 | + <button |
| 200 | + onClick={() => setSelected(name)} |
| 201 | + className={`relative flex items-center justify-center py-0.5 ${selected === name ? 'bg-human-4/30' : 'bg-background-1/80 hover:bg-background-2'} transition duration-200`} |
| 202 | + > |
| 203 | + <div className="flex items-center justify-start gap-1"> |
| 204 | + <p |
| 205 | + className={`text-sm transition duration-200 ${selected === name ? 'text-human-2' : 'text-primary'}`} |
| 206 | + > |
| 207 | + {label} |
| 208 | + </p> |
| 209 | + <i |
| 210 | + className={`material-symbols-outlined text-base transition duration-200 ${selected === name ? 'text-human-2/80' : 'text-primary/80'}`} |
| 211 | + > |
| 212 | + keyboard_arrow_down |
| 213 | + </i> |
| 214 | + </div> |
| 215 | + {selected === name && ( |
| 216 | + <motion.div |
| 217 | + layoutId="underline" |
| 218 | + className="absolute -bottom-0.5 h-0.5 w-full bg-human-2/80" |
| 219 | + ></motion.div> |
| 220 | + )} |
| 221 | + </button> |
| 222 | + ) |
| 223 | +} |
0 commit comments