@@ -3,9 +3,9 @@ import React, { useEffect, useMemo, useRef, useState } from "react";
33import { useDraggable } from "@dnd-kit/core" ;
44import { useGameData } from "../providers/GameDataProvider" ;
55
6- const Tile = ( { sx, letter, id, dealable } ) => {
7- const { blanks, fixedTiles, scoringTiles, bagTiles, dealing, roundOver , turnOver } = useGameData ( ) ;
8- const { attributes, listeners, setNodeRef } = useDraggable ( { id, disabled : fixedTiles ?. includes ( id ) || scoringTiles . length , data : { type : 'tile' } } ) ;
6+ const Tile = ( { sx, letter, id, dealable, disabled , bagTile } ) => {
7+ const { blanks, fixedTiles, scoringTiles, bagTiles, dealing, turnOver , retrieving , swapTiles } = useGameData ( ) ;
8+ const { attributes, listeners, setNodeRef } = useDraggable ( { id, disabled : turnOver || dealing || retrieving . length || swapTiles . length || fixedTiles ?. includes ( id ) || scoringTiles . length , data : { type : 'tile' } } ) ;
99 const displayedLetter = useMemo ( ( ) => blanks ?. [ id ] ?? letter . letter , [ blanks , id , letter . letter ] ) ;
1010 const [ classNames , setClassNames ] = useState ( [ 'hidden' ] ) ;
1111 const delay = useRef ( null ) ;
@@ -33,7 +33,7 @@ const Tile = ({ sx, letter, id, dealable }) => {
3333 if ( ! turnOver && ! fixedTiles ?. includes ( id ) && ! scoringTiles . length && ! classNames . includes ( 'tile' ) ) {
3434 setClassNames ( ( old ) => [ ...old , 'tile' ] ) ;
3535 }
36- } , [ classNames , dealing , fixedTiles , id , scoringTiles . length , turnOver ] )
36+ } , [ classNames , fixedTiles , id , scoringTiles . length , turnOver ] )
3737
3838 useEffect ( ( ) => {
3939 if ( dealing && ! bagTiles . includes ( id ) && classNames . includes ( 'in-bag' ) ) {
@@ -42,22 +42,28 @@ const Tile = ({ sx, letter, id, dealable }) => {
4242 } , [ bagTiles , classNames , dealing , id ] ) ;
4343
4444 useEffect ( ( ) => {
45- if ( ! dealing && classNames . includes ( 'in-bag' ) ) {
45+ if ( ! dealing && ! retrieving . length && classNames . includes ( 'in-bag' ) ) {
4646 setClassNames ( ( old ) => old . filter ( ( c ) => c !== 'in-bag' ) ) ;
4747 }
48- } , [ bagTiles , classNames , dealing , id ] ) ;
48+ } , [ classNames , dealing , retrieving . length ] ) ;
49+
50+ useEffect ( ( ) => {
51+ if ( ! swapTiles . length && classNames . includes ( 'in-swap' ) ) {
52+ setClassNames ( ( old ) => old . filter ( ( c ) => c !== 'in-swap' ) ) ;
53+ }
54+ } , [ classNames , swapTiles . length ] ) ;
4955
5056 useEffect ( ( ) => {
5157 if ( ! dealing && classNames . includes ( 'hidden' ) ) {
5258 setClassNames ( ( old ) => old . filter ( ( c ) => c !== 'hidden' ) ) ;
5359 }
54- } , [ bagTiles , classNames , dealing , id ] ) ;
60+ } , [ classNames , dealing ] ) ;
5561
5662 useEffect ( ( ) => {
57- if ( ! dealing && classNames . includes ( 'tile-base' ) ) {
63+ if ( ! dealing && ! retrieving . length && ! swapTiles . length && classNames . includes ( 'tile-base' ) ) {
5864 setClassNames ( ( old ) => old . filter ( ( c ) => c !== 'tile-base' ) ) ;
5965 }
60- } , [ classNames , dealing ] ) ;
66+ } , [ classNames , dealing , retrieving . length , swapTiles . length ] ) ;
6167
6268 useEffect ( ( ) => {
6369 if ( dealing && dealable && bagTiles . includes ( id ) && ! classNames . includes ( 'in-bag' ) && ! classNames . includes ( 'tile-base' ) ) {
@@ -75,6 +81,28 @@ const Tile = ({ sx, letter, id, dealable }) => {
7581 }
7682 } , [ bagTiles , classNames , dealable , dealing , id ] ) ;
7783
84+ useEffect ( ( ) => {
85+ if ( retrieving . length && dealable && bagTiles . includes ( id ) && ! classNames . includes ( 'in-bag' ) ) {
86+ setClassNames ( ( old ) => {
87+ // setTimeout(() => {
88+ // setClassNames((older) => [...older, 'in-bag']);
89+ // }, 100);
90+ return [ ...old , 'in-bag' ]
91+ } ) ;
92+ }
93+ } , [ bagTiles , classNames , dealable , id , retrieving . length ] ) ;
94+
95+ useEffect ( ( ) => {
96+ if ( swapTiles . length && dealable && swapTiles . includes ( id ) && ! classNames . includes ( 'in-swap' ) ) {
97+ setClassNames ( ( old ) => {
98+ // setTimeout(() => {
99+ // setClassNames((older) => [...older, 'in-bag']);
100+ // }, 100);
101+ return [ ...old , 'tile-base' , 'in-swap' ]
102+ } ) ;
103+ }
104+ } , [ swapTiles , classNames , dealable , id , swapTiles . length ] ) ;
105+
78106 useEffect ( ( ) => {
79107 if ( dealing ) {
80108 let init = initialPosition ;
@@ -94,30 +122,31 @@ const Tile = ({ sx, letter, id, dealable }) => {
94122 } , [ dealing , id , initialPosition ] ) ;
95123
96124 useEffect ( ( ) => {
97- if ( roundOver && ! classNames . includes ( 'tile-base' ) ) {
125+ if ( retrieving . length && retrieving . includes ( id ) && ! classNames . includes ( 'tile-base' ) ) {
98126 const domEl = document . getElementById ( id ) ;
99127 if ( domEl ) {
100128 const rect = domEl . getBoundingClientRect ( ) ;
101129 const bagRect = document . querySelector ( '.bag' ) . getBoundingClientRect ( ) ;
102130 const xpos = ( bagRect . width / 2 ) - ( rect . width / 2 ) + bagRect . x ;
103131 const ypos = bagRect . y + 8 ;
104132 const init = { left : xpos - rect . x , top : ypos - rect . y } ;
105- setInitialPosition ( `#${ id } .in-bag { transform: translate(${ init ?. left ?? - 1000 } px, ${ init ?. top ?? - 1000 } px); }` ) ;
133+ setInitialPosition ( `#${ id } .in-bag { transform: translate(${ init ?. left ?? - 1000 } px, ${ init ?. top ?? - 1000 } px); z-index: -1 }` ) ;
106134 }
107135 setClassNames ( ( old ) => [ ...old , 'tile-base' ] ) ;
108136 }
109- } , [ classNames , id , roundOver ] ) ;
137+ } , [ classNames , id , retrieving ] ) ;
110138
111139 return (
112140 < Box
113141 id = { id }
114142 className = { classNames . join ( ' ' ) }
115143 ref = { setNodeRef }
116- sx = { { touchAction : 'none' , boxSizing : 'border-box' , m : '3px' , width : '44px' , height : '44px' , borderRadius : '4px' , fontSize : '24px' , ...style , backgroundColor : fixedTiles ?. includes ( id ) ? 'gainsboro' : style . backgroundColor , display : 'flex' , justifyContent : 'center' , alignItems : 'center' , position : 'relative' , zIndex : 3 , pt : letter . multiplier ? 0.5 : 0 , ...sx } }
144+ sx = { { opacity : disabled ? 0.6 : 1 , touchAction : 'none' , boxSizing : 'border-box' , m : '3px' , width : '44px' , height : '44px' , borderRadius : '4px' , fontSize : '24px' , ...style , backgroundColor : fixedTiles ?. includes ( id ) ? 'gainsboro' : style . backgroundColor , display : 'flex' , justifyContent : 'center' , alignItems : 'center' , position : 'relative' , zIndex : 3 , pt : letter . multiplier ? 0.5 : 0 , ...sx } }
117145 { ...listeners }
118146 { ...attributes }
119147 >
120148 < style dangerouslySetInnerHTML = { { __html : initialPosition } } />
149+ < style dangerouslySetInnerHTML = { { __html : `#${ id } .in-swap { transform: translateX(1000px) }` } } />
121150 {
122151 letter . multiplier ? (
123152 < Box sx = { { position : 'absolute' , top : 0 , left : 0 , fontSize : '10px' , color : 'white' , backgroundColor : letter . scope === 'word' ? '#b02bb5' : '#11adab' , px : '3px' , borderRadius : '4px 2px 2px 2px' } } >
@@ -126,16 +155,19 @@ const Tile = ({ sx, letter, id, dealable }) => {
126155 ) : null
127156 }
128157 {
129- letter . multiplier ? (
158+ letter . multiplier || bagTile ? (
130159 < Tooltip arrow placement = "top" title = { (
131160 < Box sx = { { fontSize : 12 , color : 'white' , borderRadius : '4px' , display : 'flex' , flexDirection : 'column' , alignItems : 'center' } } >
132161 < Typography variant = 'h6' > { letter . letter } </ Typography >
133- < Typography variant = 'body2' > { letter . scope === 'letter' ? `${ letter . multiplier } x ${ letter . value } points` : `${ letter . multiplier } x word score` } </ Typography >
162+ {
163+ letter . multiplier ? ( < Typography variant = 'body2' > { letter . scope === 'letter' ? `${ letter . multiplier } x ${ letter . value } ${ letter . value !== 1 ? 'points' : 'point' } ` : `${ letter . multiplier } x word score` } </ Typography > )
164+ : ( < Typography variant = 'body2' > { letter . value } { letter . value !== 1 ? 'points' : 'point' } </ Typography > )
165+ }
134166 </ Box >
135167 ) } >
136- < span >
168+ < Box sx = { { width : '100%' , display : 'flex' , justifyContent : 'center' , alignItems : 'center' } } >
137169 { displayedLetter }
138- </ span >
170+ </ Box >
139171 </ Tooltip >
140172 ) : ( < span >
141173 { displayedLetter }
0 commit comments