Fix gift letter empty message and item search UX.
Omit the custom paragraph when blank, keep the item picker visible, and let Enter add the first match.
@@ -54,48 +54,34 @@ export function initGiftsModal ({ socket, MessageType })
54
54
itemPickContext = null;
55
55
}
56
56
57
function characterLabel (row)
58
{
59
const name = `${row.firstName ?? ""} ${row.lastName ?? ""}`.trim();
60
const account = row.accountUsername ? ` · @${row.accountUsername}` : "";
61
return `${name}${account}`;
62
}
63
64
function renderResults ()
57
function addGiftItem (item)
65
58
{
66
resultsEl.replaceChildren();
67
if (!characters.length)
59
if (!item?.itemId)
60
{ return; }
61
const existing = giftItems.find((row) => row.itemId === item.itemId);
62
if (existing)
68
63
{
69
resultsEl.textContent = "No characters found.";
70
return;
64
existing.quantity = Math.min(100000, (existing.quantity | 0) + 1);
71
65
}
72
for (const row of characters)
66
else
73
67
{
74
const btn = document.createElement("button");
75
btn.type = "button";
76
btn.className = selected?.characterId === row.characterId ? "active" : "";
77
btn.textContent = characterLabel(row);
78
btn.addEventListener("click", () =>
79
{
80
selected = row;
81
giftItems = [];
82
messageText = "";
83
closeInlineItemSearch();
84
render();
68
giftItems.push({
69
itemId: item.itemId,
70
quantity: 1,
71
displayName: item.displayName
72
|| itemDefinition(item.itemId)?.displayName
73
|| `Item ${item.itemId}`
85
74
});
86
resultsEl.append(btn);
87
75
}
76
closeInlineItemSearch();
77
renderDetail();
88
78
}
89
79
90
function renderInlineItemSearch (query)
80
function filterItemMatches (query)
91
81
{
92
if (!itemPickContext)
93
{ return; }
94
const { resultsEl: pickResults } = itemPickContext;
95
pickResults.replaceChildren();
96
82
const q = String(query ?? "").trim().toLowerCase();
97
83
const taken = new Set(giftItems.map((row) => row.itemId));
98
const matches = listAllItemPickerEntries().filter((item) =>
84
return listAllItemPickerEntries().filter((item) =>
99
85
{
100
86
if (taken.has(item.itemId))
101
87
{ return false; }
@@ -105,6 +91,15 @@ export function initGiftsModal ({ socket, MessageType })
105
91
|| item.key.toLowerCase().includes(q)
106
92
|| String(item.itemId).includes(q);
107
93
}).slice(0, 40);
94
}
95
96
function renderInlineItemSearch (query)
97
{
98
if (!itemPickContext)
99
{ return; }
100
const { resultsEl: pickResults } = itemPickContext;
101
pickResults.replaceChildren();
102
const matches = filterItemMatches(query);
108
103
if (!matches.length)
109
104
{
110
105
pickResults.textContent = "No matching items.";
@@ -122,17 +117,41 @@ export function initGiftsModal ({ socket, MessageType })
122
117
key.className = "shop-item-search-row-key";
123
118
key.textContent = `${item.key} (#${item.itemId})`;
124
119
row.append(name, key);
125
row.addEventListener("click", () =>
120
row.addEventListener("click", () => addGiftItem(item));
121
pickResults.append(row);
122
}
123
}
124
125
function characterLabel (row)
126
{
127
const name = `${row.firstName ?? ""} ${row.lastName ?? ""}`.trim();
128
const account = row.accountUsername ? ` · @${row.accountUsername}` : "";
129
return `${name}${account}`;
130
}
131
132
function renderResults ()
133
{
134
resultsEl.replaceChildren();
135
if (!characters.length)
136
{
137
resultsEl.textContent = "No characters found.";
138
return;
139
}
140
for (const row of characters)
141
{
142
const btn = document.createElement("button");
143
btn.type = "button";
144
btn.className = selected?.characterId === row.characterId ? "active" : "";
145
btn.textContent = characterLabel(row);
146
btn.addEventListener("click", () =>
126
147
{
127
giftItems.push({
128
itemId: item.itemId,
129
quantity: 1,
130
displayName: item.displayName
131
});
148
selected = row;
149
giftItems = [];
150
messageText = "";
132
151
closeInlineItemSearch();
133
renderDetail();
152
render();
134
153
});
135
pickResults.append(row);
154
resultsEl.append(btn);
136
155
}
137
156
}
138
157
@@ -190,30 +209,31 @@ export function initGiftsModal ({ socket, MessageType })
190
209
itemsHeader.className = "gifts-items-header";
191
210
const itemsTitle = document.createElement("strong");
192
211
itemsTitle.textContent = "Items";
193
const addBtn = document.createElement("button");
194
addBtn.type = "button";
195
addBtn.textContent = "Add item";
196
itemsHeader.append(itemsTitle, addBtn);
212
itemsHeader.append(itemsTitle);
197
213
detailEl.append(itemsHeader);
198
214
199
215
const itemSearch = document.createElement("input");
200
216
itemSearch.type = "search";
201
itemSearch.placeholder = "Search items…";
202
itemSearch.hidden = true;
217
itemSearch.placeholder = "Search items to add…";
203
218
itemSearch.autocomplete = "off";
204
219
const itemResults = document.createElement("div");
205
220
itemResults.className = "gifts-item-search-results";
206
221
detailEl.append(itemSearch, itemResults);
207
222
208
addBtn.addEventListener("click", () =>
223
itemPickContext = { inputEl: itemSearch, resultsEl: itemResults };
224
itemSearch.addEventListener("input", () => renderInlineItemSearch(itemSearch.value));
225
itemSearch.addEventListener("search", () => renderInlineItemSearch(itemSearch.value));
226
itemSearch.addEventListener("keydown", (event) =>
209
227
{
210
itemPickContext = { inputEl: itemSearch, resultsEl: itemResults };
211
itemSearch.hidden = false;
212
itemSearch.value = "";
213
renderInlineItemSearch("");
214
itemSearch.focus();
228
if (event.key !== "Enter")
229
{ return; }
230
event.preventDefault();
231
event.stopPropagation();
232
const matches = filterItemMatches(itemSearch.value);
233
if (matches[0])
234
{ addGiftItem(matches[0]); }
215
235
});
216
itemSearch.addEventListener("input", () => renderInlineItemSearch(itemSearch.value));
236
renderInlineItemSearch(itemSearch.value);
217
237
218
238
const list = document.createElement("div");
219
239
list.className = "gifts-item-list";
@@ -221,7 +241,7 @@ export function initGiftsModal ({ socket, MessageType })
221
241
{
222
242
const empty = document.createElement("p");
223
243
empty.className = "hint";
224
empty.textContent = "No items yet.";
244
empty.textContent = "No items yet — search above and click a result (or press Enter).";
225
245
list.append(empty);
226
246
}
227
247
else
@@ -54,48 +54,34 @@ export function initGiftsModal ({ socket, MessageType })54
itemPickContext = null;54
itemPickContext = null;55
}55
}56
56
57
function characterLabel (row)57
function addGiftItem (item)58
{59
const name = `${row.firstName ?? ""} ${row.lastName ?? ""}`.trim();60
const account = row.accountUsername ? ` · @${row.accountUsername}` : "";61
return `${name}${account}`;62
}63
64
function renderResults ()65
{58
{66
resultsEl.replaceChildren();59
if (!item?.itemId)67
if (!characters.length)60
{ return; }61
const existing = giftItems.find((row) => row.itemId === item.itemId);62
if (existing)68
{63
{69
resultsEl.textContent = "No characters found.";64
existing.quantity = Math.min(100000, (existing.quantity | 0) + 1);70
return;71
}65
}72
for (const row of characters)66
else73
{67
{74
const btn = document.createElement("button");68
giftItems.push({75
btn.type = "button";69
itemId: item.itemId,76
btn.className = selected?.characterId === row.characterId ? "active" : "";70
quantity: 1,77
btn.textContent = characterLabel(row);71
displayName: item.displayName78
btn.addEventListener("click", () =>72
|| itemDefinition(item.itemId)?.displayName79
{73
|| `Item ${item.itemId}`80
selected = row;81
giftItems = [];82
messageText = "";83
closeInlineItemSearch();84
render();85
});74
});86
resultsEl.append(btn);87
}75
}76
closeInlineItemSearch();77
renderDetail();88
}78
}89
79
90
function renderInlineItemSearch (query)80
function filterItemMatches (query)91
{81
{92
if (!itemPickContext)93
{ return; }94
const { resultsEl: pickResults } = itemPickContext;95
pickResults.replaceChildren();96
const q = String(query ?? "").trim().toLowerCase();82
const q = String(query ?? "").trim().toLowerCase();97
const taken = new Set(giftItems.map((row) => row.itemId));83
const taken = new Set(giftItems.map((row) => row.itemId));98
const matches = listAllItemPickerEntries().filter((item) =>84
return listAllItemPickerEntries().filter((item) =>99
{85
{100
if (taken.has(item.itemId))86
if (taken.has(item.itemId))101
{ return false; }87
{ return false; }@@ -105,6 +91,15 @@ export function initGiftsModal ({ socket, MessageType })105
|| item.key.toLowerCase().includes(q)91
|| item.key.toLowerCase().includes(q)106
|| String(item.itemId).includes(q);92
|| String(item.itemId).includes(q);107
}).slice(0, 40);93
}).slice(0, 40);94
}95
96
function renderInlineItemSearch (query)97
{98
if (!itemPickContext)99
{ return; }100
const { resultsEl: pickResults } = itemPickContext;101
pickResults.replaceChildren();102
const matches = filterItemMatches(query);108
if (!matches.length)103
if (!matches.length)109
{104
{110
pickResults.textContent = "No matching items.";105
pickResults.textContent = "No matching items.";@@ -122,17 +117,41 @@ export function initGiftsModal ({ socket, MessageType })122
key.className = "shop-item-search-row-key";117
key.className = "shop-item-search-row-key";123
key.textContent = `${item.key} (#${item.itemId})`;118
key.textContent = `${item.key} (#${item.itemId})`;124
row.append(name, key);119
row.append(name, key);125
row.addEventListener("click", () =>120
row.addEventListener("click", () => addGiftItem(item));121
pickResults.append(row);122
}123
}124
125
function characterLabel (row)126
{127
const name = `${row.firstName ?? ""} ${row.lastName ?? ""}`.trim();128
const account = row.accountUsername ? ` · @${row.accountUsername}` : "";129
return `${name}${account}`;130
}131
132
function renderResults ()133
{134
resultsEl.replaceChildren();135
if (!characters.length)136
{137
resultsEl.textContent = "No characters found.";138
return;139
}140
for (const row of characters)141
{142
const btn = document.createElement("button");143
btn.type = "button";144
btn.className = selected?.characterId === row.characterId ? "active" : "";145
btn.textContent = characterLabel(row);146
btn.addEventListener("click", () =>126
{147
{127
giftItems.push({148
selected = row;128
itemId: item.itemId,149
giftItems = [];129
quantity: 1,150
messageText = "";130
displayName: item.displayName131
});132
closeInlineItemSearch();151
closeInlineItemSearch();133
renderDetail();152
render();134
});153
});135
pickResults.append(row);154
resultsEl.append(btn);136
}155
}137
}156
}138
157
@@ -190,30 +209,31 @@ export function initGiftsModal ({ socket, MessageType })190
itemsHeader.className = "gifts-items-header";209
itemsHeader.className = "gifts-items-header";191
const itemsTitle = document.createElement("strong");210
const itemsTitle = document.createElement("strong");192
itemsTitle.textContent = "Items";211
itemsTitle.textContent = "Items";193
const addBtn = document.createElement("button");212
itemsHeader.append(itemsTitle);194
addBtn.type = "button";195
addBtn.textContent = "Add item";196
itemsHeader.append(itemsTitle, addBtn);197
detailEl.append(itemsHeader);213
detailEl.append(itemsHeader);198
214
199
const itemSearch = document.createElement("input");215
const itemSearch = document.createElement("input");200
itemSearch.type = "search";216
itemSearch.type = "search";201
itemSearch.placeholder = "Search items…";217
itemSearch.placeholder = "Search items to add…";202
itemSearch.hidden = true;203
itemSearch.autocomplete = "off";218
itemSearch.autocomplete = "off";204
const itemResults = document.createElement("div");219
const itemResults = document.createElement("div");205
itemResults.className = "gifts-item-search-results";220
itemResults.className = "gifts-item-search-results";206
detailEl.append(itemSearch, itemResults);221
detailEl.append(itemSearch, itemResults);207
222
208
addBtn.addEventListener("click", () =>223
itemPickContext = { inputEl: itemSearch, resultsEl: itemResults };224
itemSearch.addEventListener("input", () => renderInlineItemSearch(itemSearch.value));225
itemSearch.addEventListener("search", () => renderInlineItemSearch(itemSearch.value));226
itemSearch.addEventListener("keydown", (event) =>209
{227
{210
itemPickContext = { inputEl: itemSearch, resultsEl: itemResults };228
if (event.key !== "Enter")211
itemSearch.hidden = false;229
{ return; }212
itemSearch.value = "";230
event.preventDefault();213
renderInlineItemSearch("");231
event.stopPropagation();214
itemSearch.focus();232
const matches = filterItemMatches(itemSearch.value);233
if (matches[0])234
{ addGiftItem(matches[0]); }215
});235
});216
itemSearch.addEventListener("input", () => renderInlineItemSearch(itemSearch.value));236
renderInlineItemSearch(itemSearch.value);217
237
218
const list = document.createElement("div");238
const list = document.createElement("div");219
list.className = "gifts-item-list";239
list.className = "gifts-item-list";@@ -221,7 +241,7 @@ export function initGiftsModal ({ socket, MessageType })221
{241
{222
const empty = document.createElement("p");242
const empty = document.createElement("p");223
empty.className = "hint";243
empty.className = "hint";224
empty.textContent = "No items yet.";244
empty.textContent = "No items yet — search above and click a result (or press Enter).";225
list.append(empty);245
list.append(empty);226
}246
}227
else247
else@@ -1384,8 +1384,16 @@ label {
1384
1384
display: flex;
1385
1385
flex-direction: column;
1386
1386
gap: 0.2rem;
1387
max-height: 10rem;
1387
max-height: 14rem;
1388
min-height: 4rem;
1388
1389
overflow: auto;
1390
border: 1px solid #2c3340;
1391
background: #0f1218;
1392
padding: 0.35rem;
1393
}
1394
1395
.gifts-item-search-results .shop-item-search-row {
1396
width: 100%;
1389
1397
}
1390
1398
1391
1399
.gifts-preview-wrap {
@@ -1384,8 +1384,16 @@ label {1384
display: flex;1384
display: flex;1385
flex-direction: column;1385
flex-direction: column;1386
gap: 0.2rem;1386
gap: 0.2rem;1387
max-height: 10rem;1387
max-height: 14rem;1388
min-height: 4rem;1388
overflow: auto;1389
overflow: auto;1390
border: 1px solid #2c3340;1391
background: #0f1218;1392
padding: 0.35rem;1393
}1394
1395
.gifts-item-search-results .shop-item-search-row {1396
width: 100%;1389
}1397
}1390
1398
1391
.gifts-preview-wrap {1399
.gifts-preview-wrap {@@ -53,8 +53,6 @@ export class GiftService
53
53
{ throw userError("Pick at least one item to gift."); }
54
54
55
55
const message = String(payload?.message ?? "").replace(/\r\n/g, "\n").trim();
56
if (!message)
57
{ throw userError("Write a gift message."); }
58
56
if (message.length > LETTER_MAX_BODY_CHARS)
59
57
{ throw userError("Gift message is too long."); }
60
58
@@ -53,8 +53,6 @@ export class GiftService53
{ throw userError("Pick at least one item to gift."); }53
{ throw userError("Pick at least one item to gift."); }54
54
55
const message = String(payload?.message ?? "").replace(/\r\n/g, "\n").trim();55
const message = String(payload?.message ?? "").replace(/\r\n/g, "\n").trim();56
if (!message)57
{ throw userError("Write a gift message."); }58
if (message.length > LETTER_MAX_BODY_CHARS)56
if (message.length > LETTER_MAX_BODY_CHARS)59
{ throw userError("Gift message is too long."); }57
{ throw userError("Gift message is too long."); }60
58
@@ -78,6 +78,23 @@ test("gift letter body and signature match Questward CEO format", () =>
78
78
}),
79
79
"Signed:\nJohn Questward\nCEO of Questward"
80
80
);
81
82
const noMessage = formatGiftLetterBody({
83
characterDisplayName: "Tim Falken",
84
message: " ",
85
items: [{ displayName: "Stick", quantity: 1 }]
86
});
87
assert.equal(
88
noMessage,
89
[
90
"Dear Tim Falken",
91
"",
92
"The following items were added to your bank:",
93
"• 1× Stick",
94
"",
95
"Thank you for playing Questward."
96
].join("\n")
97
);
81
98
});
82
99
83
100
test("admin gift credits bank and queues a system letter", () =>
@@ -78,6 +78,23 @@ test("gift letter body and signature match Questward CEO format", () =>78
}),78
}),79
"Signed:\nJohn Questward\nCEO of Questward"79
"Signed:\nJohn Questward\nCEO of Questward"80
);80
);81
82
const noMessage = formatGiftLetterBody({83
characterDisplayName: "Tim Falken",84
message: " ",85
items: [{ displayName: "Stick", quantity: 1 }]86
});87
assert.equal(88
noMessage,89
[90
"Dear Tim Falken",91
"",92
"The following items were added to your bank:",93
"• 1× Stick",94
"",95
"Thank you for playing Questward."96
].join("\n")97
);81
});98
});82
99
83
test("admin gift credits bank and queues a system letter", () =>100
test("admin gift credits bank and queues a system letter", () =>