Skip to content

Commit f04fcc8

Browse files
committed
fix(game-client): allow empty party command payload
1 parent 8b31048 commit f04fcc8

4 files changed

Lines changed: 83 additions & 27 deletions

File tree

apps/game-client/src/features/command/command-state.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,39 @@ describe("getCommandState", () => {
3636
submissionMessage: "szukam tanca",
3737
});
3838
});
39+
40+
it("allows !grp without a description", () => {
41+
expect(getCommandState("!grp", { selectedGuildCount: 1 })).toMatchObject({
42+
mode: "party",
43+
isNotification: true,
44+
isPartyCommand: true,
45+
hasContent: true,
46+
canSubmit: true,
47+
submissionMessage: "",
48+
});
49+
});
50+
51+
it("allows !grp with only trailing whitespace", () => {
52+
expect(getCommandState("!grp ", { selectedGuildCount: 1 })).toMatchObject({
53+
mode: "party",
54+
isNotification: true,
55+
isPartyCommand: true,
56+
hasContent: true,
57+
canSubmit: true,
58+
submissionMessage: "",
59+
});
60+
});
61+
62+
it("treats !grpfoo as a notification, not a party command", () => {
63+
expect(getCommandState("!grpfoo", { selectedGuildCount: 1 })).toMatchObject(
64+
{
65+
mode: "notification",
66+
isNotification: true,
67+
isPartyCommand: false,
68+
hasContent: true,
69+
canSubmit: true,
70+
submissionMessage: "grpfoo",
71+
},
72+
);
73+
});
3974
});

apps/game-client/src/features/command/command-state.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ export const PARTY_COMMAND_PREFIX = "!grp";
22
export const NOTIFICATION_COMMAND_PREFIX = "!";
33
const MAX_MESSAGE_LENGTH = 120;
44

5+
const hasCommandBoundary = (inputValue: string, prefix: string) => {
6+
if (!inputValue.startsWith(prefix)) {
7+
return false;
8+
}
9+
10+
const nextCharacter = inputValue.charAt(prefix.length);
11+
return nextCharacter === "" || /\s/.test(nextCharacter);
12+
};
13+
514
export type CommandMode = "normal" | "notification" | "party";
615

716
type CommandStateOptions = {
@@ -28,29 +37,35 @@ export const getCommandState = (
2837
options?: CommandStateOptions,
2938
): CommandState => {
3039
const hasRecipients = (options?.selectedGuildCount ?? 0) > 0;
31-
const isPartyCommand = inputValue.startsWith(PARTY_COMMAND_PREFIX);
40+
const isPartyCommand = hasCommandBoundary(inputValue, PARTY_COMMAND_PREFIX);
3241
const isNotification = inputValue.startsWith(NOTIFICATION_COMMAND_PREFIX);
3342
const isCommand = isNotification;
34-
const mode: CommandMode = isPartyCommand
35-
? "party"
36-
: isNotification
37-
? "notification"
38-
: "normal";
43+
let mode: CommandMode = "normal";
44+
let commandQuery = "";
45+
let showSuggestions = false;
46+
let commandPayload = inputValue;
3947

40-
const commandQuery = isCommand ? (inputValue.split(/\s/, 1)[0] ?? "") : "";
41-
const showSuggestions =
42-
commandQuery.startsWith(NOTIFICATION_COMMAND_PREFIX) &&
43-
!/\s/.test(inputValue);
48+
if (isPartyCommand) {
49+
mode = "party";
50+
commandQuery = PARTY_COMMAND_PREFIX;
51+
commandPayload = inputValue.slice(PARTY_COMMAND_PREFIX.length).trim();
52+
showSuggestions = !/\s/.test(inputValue);
53+
} else if (isNotification) {
54+
mode = "notification";
55+
commandQuery = inputValue.split(/\s/, 1)[0] ?? "";
56+
commandPayload = inputValue
57+
.slice(NOTIFICATION_COMMAND_PREFIX.length)
58+
.trim();
59+
showSuggestions = !/\s/.test(inputValue);
60+
}
4461

45-
const commandPayload = isPartyCommand
46-
? inputValue.slice(PARTY_COMMAND_PREFIX.length).trim()
47-
: isNotification
48-
? inputValue.slice(NOTIFICATION_COMMAND_PREFIX.length).trim()
49-
: inputValue;
62+
let hasContent = inputValue.trim().length > 0;
63+
if (isPartyCommand) {
64+
hasContent = true;
65+
} else if (isNotification) {
66+
hasContent = commandPayload.length > 0;
67+
}
5068

51-
const hasContent = isCommand
52-
? commandPayload.length > 0
53-
: inputValue.trim().length > 0;
5469
const exceedsMaxLength = inputValue.length > MAX_MESSAGE_LENGTH;
5570

5671
return {

apps/game-client/src/features/command/command.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ export const CommandWindow = () => {
5656
selectedGuildCount: selectedInputGuildIds.length,
5757
});
5858

59+
const getHintKey = () => {
60+
if (!commandState.hasRecipients) {
61+
return "settings.command.hints.selectGuild";
62+
}
63+
64+
if (commandState.hasContent) {
65+
return "settings.command.hints.submitReady";
66+
}
67+
68+
return `settings.command.modes.${commandState.mode}.emptyHint`;
69+
};
70+
5971
const suggestions = useCommandSuggestions({
6072
inputValue: messageValue,
6173
onSelect: (prefix) => {
@@ -234,13 +246,7 @@ export const CommandWindow = () => {
234246
: "ll:text-red-200",
235247
)}
236248
>
237-
{t(
238-
commandState.hasRecipients
239-
? commandState.hasContent
240-
? "settings.command.hints.submitReady"
241-
: `settings.command.modes.${commandState.mode}.emptyHint`
242-
: "settings.command.hints.selectGuild",
243-
)}
249+
{t(getHintKey())}
244250
</span>
245251
<span className="ll:text-[10px] ll:text-gray-500">
246252
{t("settings.command.hints.keyboard")}

apps/game-client/src/i18n/translations/settings/command.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"label": "Szukaj grupy",
3030
"composerLabel": "Szukanie grupy z !grp",
3131
"description": "Komenda !grp uruchamia szukanie grupy i publikuje je na wybranych gildiach.",
32-
"placeholder": "Dodaj opis po !grp",
33-
"emptyHint": "Dodaj opis po !grp, aby uruchomić szukanie grupy."
32+
"placeholder": "Opcjonalnie dodaj opis po !grp",
33+
"emptyHint": "Naciśnij Enter, aby uruchomić szukanie grupy bez opisu."
3434
}
3535
},
3636
"suggestions": {

0 commit comments

Comments
 (0)