|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import Button from 'components/input/button'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { |
| 6 | + CharacterId, |
| 7 | + CHARACTERS, |
| 8 | + getDefaultAlignment, |
| 9 | + getType, |
| 10 | +} from './characters'; |
| 11 | +import CharacterTokenSelector from './character-token-selector'; |
| 12 | +import { cn } from 'utils/class-names'; |
| 13 | +import CharacterToken from './character-token'; |
| 14 | + |
3 | 15 | interface InfoTokenProps { |
4 | 16 | text: string; |
| 17 | + className?: string; |
| 18 | + characters: CharacterId[]; |
| 19 | + allCharacters: CharacterId[]; |
| 20 | + demonBluffs?: CharacterId[]; |
| 21 | + setDemonBluffs?: (arg0: CharacterId[]) => void; |
5 | 22 | } |
6 | 23 |
|
7 | | -const InfoToken = ({ text }: InfoTokenProps) => { |
| 24 | +const InfoToken = ({ |
| 25 | + text, |
| 26 | + className = '', |
| 27 | + characters, |
| 28 | + allCharacters, |
| 29 | + demonBluffs, |
| 30 | + setDemonBluffs, |
| 31 | +}: InfoTokenProps) => { |
| 32 | + const [isSelectingCharacterToken, setIsSelectingCharacterToken] = |
| 33 | + useState(false); |
| 34 | + const [characterTokens, _setCharacterTokens] = useState<CharacterId[]>( |
| 35 | + demonBluffs ?? [], |
| 36 | + ); |
| 37 | + |
| 38 | + const setCharacterTokens = (v: CharacterId[]) => { |
| 39 | + _setCharacterTokens(v); |
| 40 | + if (demonBluffs !== undefined && setDemonBluffs !== undefined) { |
| 41 | + setDemonBluffs(v); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + const charactersSet = new Set( |
| 46 | + demonBluffs ? characterTokens.concat(characters) : characterTokens, |
| 47 | + ); |
| 48 | + const filterFunc = (id: CharacterId) => |
| 49 | + !charactersSet.has(id) && |
| 50 | + (demonBluffs === undefined || |
| 51 | + (getDefaultAlignment(id) === 'good' && getType(id) !== 'travellers')); |
| 52 | + |
| 53 | + const firstCharacterToken = characterTokens[0]; |
8 | 54 | return ( |
9 | | - <div className='rounded border p-12 shadow-md'> |
10 | | - <h3 className='text-wrap text-center'>{text}</h3> |
| 55 | + <div |
| 56 | + className={cn( |
| 57 | + 'flex flex-col items-center rounded border p-12 text-white shadow-md', |
| 58 | + className, |
| 59 | + )} |
| 60 | + > |
| 61 | + {!isSelectingCharacterToken && ( |
| 62 | + <> |
| 63 | + <h3 className='text-wrap text-center'>{text}</h3> |
| 64 | + <div className='h-4' /> |
| 65 | + <div className='flex w-1/2 flex-wrap justify-center gap-4'> |
| 66 | + {characterTokens.length > 0 && |
| 67 | + characterTokens.map((id, i) => ( |
| 68 | + <div key={text + id} className='w-1/3'> |
| 69 | + <CharacterToken |
| 70 | + characterId={id} |
| 71 | + onClick={() => { |
| 72 | + setCharacterTokens(characterTokens.toSpliced(i, 1)); |
| 73 | + }} |
| 74 | + /> |
| 75 | + </div> |
| 76 | + ))} |
| 77 | + {characterTokens.length === 1 && |
| 78 | + firstCharacterToken && |
| 79 | + CHARACTERS[firstCharacterToken].description} |
| 80 | + </div> |
| 81 | + <div className='h-4' /> |
| 82 | + <div className='flex gap-2'> |
| 83 | + <Button |
| 84 | + compact |
| 85 | + onClick={() => { |
| 86 | + setIsSelectingCharacterToken(true); |
| 87 | + }} |
| 88 | + > |
| 89 | + Add character token |
| 90 | + </Button> |
| 91 | + </div> |
| 92 | + </> |
| 93 | + )} |
| 94 | + {isSelectingCharacterToken && ( |
| 95 | + <> |
| 96 | + <h3> |
| 97 | + Add character token |
| 98 | + {demonBluffs && ' (Showing good characters not in play)'} |
| 99 | + </h3> |
| 100 | + <div className='h-2' /> |
| 101 | + <CharacterTokenSelector |
| 102 | + characters={characters.filter(filterFunc)} |
| 103 | + allCharacters={allCharacters.filter(filterFunc)} |
| 104 | + onChange={(id) => { |
| 105 | + setCharacterTokens([...characterTokens, id]); |
| 106 | + setIsSelectingCharacterToken(false); |
| 107 | + }} |
| 108 | + defaultShowAll={!!demonBluffs} |
| 109 | + /> |
| 110 | + <div className='h-2' /> |
| 111 | + <Button |
| 112 | + compact |
| 113 | + onClick={() => { |
| 114 | + setIsSelectingCharacterToken(false); |
| 115 | + }} |
| 116 | + > |
| 117 | + Cancel |
| 118 | + </Button> |
| 119 | + </> |
| 120 | + )} |
11 | 121 | </div> |
12 | 122 | ); |
13 | 123 | }; |
|
0 commit comments