Skip to content

Commit 11bb454

Browse files
committed
fix: Fix select menu UI update (closes #49) and refactor
1 parent e3e2407 commit 11bb454

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

src/commands/activity/activity.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const chartJSNodeCanvas = new ChartJSNodeCanvas({
2626
},
2727
});
2828

29+
const ACTIVITY_PERIOD_ID = "activity-period";
30+
const ACTIVITY_ROLLING_ID = "activity-rolling";
31+
2932
const ActivityCommand: Command = {
3033
data: new SlashCommandBuilder()
3134
.setName("activity")
@@ -55,7 +58,7 @@ const ActivityCommand: Command = {
5558

5659
const fileBuffer = await plotTrend(guildId, userLocale, serverName, botName, channel);
5760

58-
const trendPeriodSelect = new StringSelectMenuBuilder().setCustomId("activity-period").setOptions([
61+
const trendPeriodSelect = new StringSelectMenuBuilder().setCustomId(ACTIVITY_PERIOD_ID).setOptions([
5962
{
6063
label: translations.responses?.sinceTheBeginning?.[userLocale] ?? "Since the beginning",
6164
value: "0",
@@ -81,7 +84,7 @@ const ActivityCommand: Command = {
8184
},
8285
]);
8386

84-
const rollingMeanSelect = new StringSelectMenuBuilder().setCustomId("activity-rolling").setOptions([
87+
const rollingMeanSelect = new StringSelectMenuBuilder().setCustomId(ACTIVITY_ROLLING_ID).setOptions([
8588
{
8689
label: translations.responses?.rolling14Days?.[userLocale] ?? "Average over 14 days",
8790
value: "14",
@@ -131,26 +134,29 @@ const ActivityCommand: Command = {
131134
let rolling = 14;
132135

133136
collector.on("collect", async (i) => {
134-
if (i.customId === "activity-period") {
137+
if (i.customId === ACTIVITY_PERIOD_ID) {
135138
period = parseInt(i.values[0] ?? "0");
136-
} else if (i.customId === "activity-rolling") {
139+
} else if (i.customId === ACTIVITY_ROLLING_ID) {
137140
rolling = parseInt(i.values[0] ?? "14");
138141
}
139142
logger.debug("Period: %d, Rolling: %d", period, rolling);
140143

141-
const selectedPeriodOption = trendPeriodSelect.options.find((option) => option.data.value === i.values[0]);
142-
const selectedRollingOption = rollingMeanSelect.options.find((option) => option.data.value === i.values[0]);
143-
if (selectedPeriodOption) {
144-
trendPeriodSelect.options.forEach((option) => {
145-
option.setDefault(false);
146-
});
147-
selectedPeriodOption.setDefault(true);
148-
}
149-
if (selectedRollingOption) {
150-
rollingMeanSelect.options.forEach((option) => {
151-
option.setDefault(false);
152-
});
153-
selectedRollingOption.setDefault(true);
144+
if (i.customId === ACTIVITY_PERIOD_ID) {
145+
const selectedPeriodOption = trendPeriodSelect.options.find((option) => option.data.value === i.values[0]);
146+
if (selectedPeriodOption) {
147+
trendPeriodSelect.options.forEach((option) => {
148+
option.setDefault(false);
149+
});
150+
selectedPeriodOption.setDefault(true);
151+
}
152+
} else if (i.customId === ACTIVITY_ROLLING_ID) {
153+
const selectedRollingOption = rollingMeanSelect.options.find((option) => option.data.value === i.values[0]);
154+
if (selectedRollingOption) {
155+
rollingMeanSelect.options.forEach((option) => {
156+
option.setDefault(false);
157+
});
158+
selectedRollingOption.setDefault(true);
159+
}
154160
}
155161

156162
const deferUpdatePromise = i.update({ content: "", components: components });

src/commands/activity/trend.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const chartJSNodeCanvas = new ChartJSNodeCanvas({
2626
},
2727
});
2828

29+
const TREND_PERIOD_ID = "trend-period";
30+
const TREND_ROLLING_ID = "trend-rolling";
31+
2932
const TrendCommand: Command = {
3033
data: new SlashCommandBuilder()
3134
.setName("trend")
@@ -54,7 +57,7 @@ const TrendCommand: Command = {
5457

5558
const fileBuffer = await plotTrend(guildId, term, userLocale, serverName, botName);
5659

57-
const trendPeriodSelect = new StringSelectMenuBuilder().setCustomId("trend-period").setOptions([
60+
const trendPeriodSelect = new StringSelectMenuBuilder().setCustomId(TREND_PERIOD_ID).setOptions([
5861
{
5962
label: translations.responses?.sinceTheBeginning?.[userLocale] ?? "Since the beginning",
6063
value: "0",
@@ -80,7 +83,7 @@ const TrendCommand: Command = {
8083
},
8184
]);
8285

83-
const rollingMeanSelect = new StringSelectMenuBuilder().setCustomId("trend-rolling").setOptions([
86+
const rollingMeanSelect = new StringSelectMenuBuilder().setCustomId(TREND_ROLLING_ID).setOptions([
8487
{
8588
label: translations.responses?.rolling14Days?.[userLocale] ?? "Average over 14 days",
8689
value: "14",
@@ -130,26 +133,29 @@ const TrendCommand: Command = {
130133
let rolling = 14;
131134

132135
collector.on("collect", async (i) => {
133-
if (i.customId === "trend-period") {
136+
if (i.customId === TREND_PERIOD_ID) {
134137
period = parseInt(i.values[0] ?? "0");
135-
} else if (i.customId === "trend-rolling") {
138+
} else if (i.customId === TREND_ROLLING_ID) {
136139
rolling = parseInt(i.values[0] ?? "14");
137140
}
138141
logger.debug("Period: %d, Rolling: %d", period, rolling);
139142

140-
const selectedPeriodOption = trendPeriodSelect.options.find((option) => option.data.value === i.values[0]);
141-
const selectedRollingOption = rollingMeanSelect.options.find((option) => option.data.value === i.values[0]);
142-
if (selectedPeriodOption) {
143-
trendPeriodSelect.options.forEach((option) => {
144-
option.setDefault(false);
145-
});
146-
selectedPeriodOption.setDefault(true);
147-
}
148-
if (selectedRollingOption) {
149-
rollingMeanSelect.options.forEach((option) => {
150-
option.setDefault(false);
151-
});
152-
selectedRollingOption.setDefault(true);
143+
if (i.customId === TREND_PERIOD_ID) {
144+
const selectedPeriodOption = trendPeriodSelect.options.find((option) => option.data.value === i.values[0]);
145+
if (selectedPeriodOption) {
146+
trendPeriodSelect.options.forEach((option) => {
147+
option.setDefault(false);
148+
});
149+
selectedPeriodOption.setDefault(true);
150+
}
151+
} else if (i.customId === TREND_ROLLING_ID) {
152+
const selectedRollingOption = rollingMeanSelect.options.find((option) => option.data.value === i.values[0]);
153+
if (selectedRollingOption) {
154+
rollingMeanSelect.options.forEach((option) => {
155+
option.setDefault(false);
156+
});
157+
selectedRollingOption.setDefault(true);
158+
}
153159
}
154160

155161
const deferUpdatePromise = i.update({ content: "", components: components });

src/locales/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"description": "Visualisez le nombre de messages postés par jour.",
3939
"options": {
4040
"channel": {
41-
"name": "channel",
41+
"name": "salon",
4242
"description": "Salon dans lequel compter."
4343
}
4444
},

0 commit comments

Comments
 (0)