Skip to content

Commit 3ed0ad6

Browse files
committed
fix: show human-readable label instead of value in walkthrough
1 parent 2583096 commit 3ed0ad6

File tree

3 files changed

+51
-26
lines changed

3 files changed

+51
-26
lines changed

src/commands/util/walkthrough.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
FetchMessageOptions,
1717
} from "discord.js";
1818

19-
export function generateMessage(
19+
export function generateQuestion(
2020
question: string,
2121
component: StringSelectMenuBuilder,
2222
embeds: (EmbedBuilder | Embed)[] = [],
@@ -45,7 +45,7 @@ export async function doWalkthrough(
4545
}
4646

4747
// Generate the message with the action row
48-
const message = generateMessage(
48+
const message = generateQuestion(
4949
"What are you creating this issue for?",
5050
issueCategorySelector,
5151
);

src/events/walkthrough.ts

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { doWalkthrough, generateMessage } from "@commands/util/walkthrough.js";
1+
import { doWalkthrough, generateQuestion } from "@commands/util/walkthrough.js";
22

33
import issueCategorySelector from "@components/issueCategorySelector.js";
44
import productSelector from "@components/productSelector.js";
@@ -11,6 +11,19 @@ import {
1111
type InteractionUpdateOptions,
1212
} from "discord.js";
1313

14+
// This has to follow the order of the walkthrough steps
15+
const selectors = [
16+
issueCategorySelector,
17+
productSelector,
18+
operatingSystemFamilySelector,
19+
];
20+
21+
function getLabelFromValue(value, selector: (typeof selectors)[number]) {
22+
return selector.options.filter((option) => option.data.value === value)[0]
23+
.data.label;
24+
}
25+
26+
// TODO: make this readable
1427
export default function registerEvents(client: Client) {
1528
// Do walkthrough whenever a thread is opened
1629
client.on(Events.ThreadCreate, async (channel) => doWalkthrough(channel));
@@ -20,12 +33,22 @@ export default function registerEvents(client: Client) {
2033
if (interaction.isStringSelectMenu()) {
2134
let message: InteractionUpdateOptions;
2235

23-
// TODO : make this code more generic
24-
if (interaction.customId === issueCategorySelector.data.custom_id) {
36+
const selector = selectors.filter(
37+
(element) => element.data.custom_id === interaction.customId,
38+
)[0];
39+
const index = selectors.indexOf(selector);
40+
41+
const nextSelector = selectors[index + 1];
42+
43+
if (index === 0) {
2544
const dataEmbed = new EmbedBuilder()
2645
.setTitle(`<#${interaction.channelId}>`)
2746
.addFields([
28-
{ name: "Category", value: interaction.values[0], inline: true },
47+
{
48+
name: "Category",
49+
value: getLabelFromValue(interaction.values[0], selector),
50+
inline: true,
51+
},
2952
{ name: "Product", value: "N/A", inline: true },
3053
{ name: "Platform", value: "N/A", inline: true },
3154
{
@@ -34,33 +57,35 @@ export default function registerEvents(client: Client) {
3457
},
3558
]);
3659

37-
message = generateMessage(
60+
message = generateQuestion(
3861
"What product are you using?",
3962
productSelector,
4063
[dataEmbed],
4164
);
42-
} else if (interaction.customId === productSelector.data.custom_id) {
43-
// Grab the embed from the last message and edit the "Product" field
65+
} else {
66+
// Grab the embed from the last message and edit the corresponding field with the human-readable field (instead of the ID)
4467
const dataEmbed = interaction.message.embeds[0];
45-
dataEmbed.fields[1].value = interaction.values[0];
46-
47-
// TODO: replace "the product" by the name of the product that was chosen in the previous step (productSelector)
48-
message = generateMessage(
49-
"What operating system are you running the product on?",
50-
operatingSystemFamilySelector,
51-
[dataEmbed],
68+
dataEmbed.fields[index].value = getLabelFromValue(
69+
interaction.values[0],
70+
selector,
5271
);
53-
} else if (
54-
interaction.customId === operatingSystemFamilySelector.data.custom_id
55-
) {
56-
// Grab the embed from the last message and edit the "Product" field
57-
const dataEmbed = interaction.message.embeds[0];
58-
dataEmbed.fields[2].value = interaction.values[0];
5972

60-
// Generate an empty message with just the data embed
61-
message = { components: [], embeds: [dataEmbed] };
73+
// TODO : make this part more generic once we have more questions
74+
if (selector === productSelector) {
75+
message = generateQuestion(
76+
`What operating system are you running ${dataEmbed.fields[index].value} on?`,
77+
nextSelector,
78+
[dataEmbed],
79+
);
80+
} else if (index + 1 === selectors.length) {
81+
// <- means this is the last step of the walkthrough
82+
// Generate an empty message with just the data embed and pin it
83+
message = { components: [], embeds: [dataEmbed] };
6284

63-
// TODO: pin
85+
await interaction.message.pin();
86+
} else {
87+
throw new Error("No case matches this walkthrough step");
88+
}
6489
}
6590

6691
await interaction.update(message);

src/ui/components/productSelector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
const options = [
99
new StringSelectMenuOptionBuilder()
10-
.setLabel("Coder OSS (v2)")
10+
.setLabel("Coder (v2)")
1111
.setValue("coder")
1212
.setEmoji(config.emojis.coder),
1313

0 commit comments

Comments
 (0)