From eeb80d3c866ec2f96120c543041bfa3fd047abff Mon Sep 17 00:00:00 2001
From: Joel Therrien <joel@joeltherrien.ca>
Date: Sun, 2 Mar 2025 15:12:00 -0800
Subject: [PATCH] fix: UI prevents players from playing blank, blank tiles

---
 ui/src/Game.tsx | 115 ++++++++++++++++++++++++++----------------------
 1 file changed, 62 insertions(+), 53 deletions(-)

diff --git a/ui/src/Game.tsx b/ui/src/Game.tsx
index d09b505..4051689 100644
--- a/ui/src/Game.tsx
+++ b/ui/src/Game.tsx
@@ -456,68 +456,77 @@ export function Game(props: {
                     className="check"
                     disabled={isGameOver || !isPlayersTurn}
                     onClick={async () => {
-                        const playedTiles = playerLetters.map((i) => {
-                            if (i == null) {
-                                return null;
-                            }
-
-                            if (i.location === LocationType.GRID) {
-                                let result: PlayedTile = {
-                                    index: i.index,
-                                    character: null
-                                };
-                                if (i.is_blank) {
-                                    result.character = i.text;
+                        try {
+                            const playedTiles = playerLetters.map((i) => {
+                                if (i == null) {
+                                    return null;
                                 }
 
-                                return result;
-                            }
-
-                            return null;
-                        });
-
-                        const committing = confirmedScorePoints > -1;
-                        const result = props.api.play(playedTiles, committing);
-
-                        result
-                            .then(
-                                (api_state) => {
-                                    console.log("Testing45")
-                                    console.log({api_state});
-
-                                    const play_tiles: TurnAction = api_state.update.type;
-                                    if (play_tiles.type == "PlayTiles") {
-                                        setConfirmedScorePoints(play_tiles.result.total);
-
-                                        if (committing) {
-                                            setAPIState(api_state);
+                                if (i.location === LocationType.GRID) {
+                                    let result: PlayedTile = {
+                                        index: i.index,
+                                        character: null
+                                    };
+                                    if (i.is_blank) {
+                                        result.character = i.text;
+                                        if(result.character == null || result.character == " " || result.character == "") {
+                                            throw new Error("Blank tiles must contain a letter");
                                         }
-
-                                    } else {
-                                        console.error("Inaccessible branch!!!")
                                     }
-                                }
-                            )
-                            .catch((error) => {
-                                console.error({error});
 
-                                if (error.endsWith(" is not a valid word")) {
-                                    const word = error.split(' ')[0];
-                                    // For whatever reason I can't pass props.api.add_to_dictionary directly
-                                    logDispatch(<AddWordButton word={word}
-                                                               addWordFn={(word) => {
-                                                                   props.api.add_to_dictionary(word)
-                                                                       .then((api_state) => {
-                                                                           setAPIState(api_state);
-                                                                       })
-                                                               }}/>);
-                                } else {
-                                    logDispatch(<div>{error}</div>);
+                                    return result;
                                 }
 
-
+                                return null;
                             });
 
+
+                            const committing = confirmedScorePoints > -1;
+                            const result = props.api.play(playedTiles, committing);
+
+                            result
+                                .then(
+                                    (api_state) => {
+                                        console.log("Testing45")
+                                        console.log({api_state});
+
+                                        const play_tiles: TurnAction = api_state.update.type;
+                                        if (play_tiles.type == "PlayTiles") {
+                                            setConfirmedScorePoints(play_tiles.result.total);
+
+                                            if (committing) {
+                                                setAPIState(api_state);
+                                            }
+
+                                        } else {
+                                            console.error("Inaccessible branch!!!")
+                                        }
+                                    }
+                                )
+                                .catch((error) => {
+                                    console.error({error});
+
+                                    if (error.endsWith(" is not a valid word")) {
+                                        const word = error.split(' ')[0];
+                                        // For whatever reason I can't pass props.api.add_to_dictionary directly
+                                        logDispatch(<AddWordButton word={word}
+                                                                   addWordFn={(word) => {
+                                                                       props.api.add_to_dictionary(word)
+                                                                           .then((api_state) => {
+                                                                               setAPIState(api_state);
+                                                                           })
+                                                                   }}/>);
+                                    } else {
+                                        logDispatch(<div>{error}</div>);
+                                    }
+
+
+                                });
+                        }
+                        catch(e) {
+                            logDispatch(<div>{e.message}</div>)
+                        }
+
                     }}>{confirmedScorePoints > -1 ? `Score ${confirmedScorePoints} points` : "Check"}</button>
                 <button
                     className="return"