Skip to content

Commit b4892c4

Browse files
committed
Better user responses when adding incidents
1 parent 1df12e2 commit b4892c4

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

app/server.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ const app = new App({
2020
}),
2121
});
2222

23+
const myStore = {};
24+
const TWENTYFOUR_HOURS_IN_MS = 24 * 60 * 60 * 1000;
25+
26+
// Function to store data with a TTL
27+
function storeWithTTL(key, value, ttlInMilliseconds) {
28+
myStore[key] = value;
29+
30+
// Set a timeout to delete the entry after the TTL
31+
setTimeout(() => {
32+
delete myStore[key];
33+
}, ttlInMilliseconds);
34+
}
35+
2336
app.command("/addcategory", async ({ command, ack, respond }) => {
2437
await ack(); // Acknowledge the command
2538

@@ -59,6 +72,9 @@ app.event("message", async ({ event, client }) => {
5972
// Ignore everything except new messages
6073
return;
6174
}
75+
const uuid = crypto.randomUUID();
76+
storeWithTTL(event.text, TWENTYFOUR_HOURS_IN_MS); // Store the text with the UUID as the key for a day
77+
6278
console.log("Posting an ephemeral message to user:", event.user, "in channel:", event.channel);
6379
try {
6480
// Respond with an ephemeral message containing a dropdown menu
@@ -75,7 +91,7 @@ app.event("message", async ({ event, client }) => {
7591
},
7692
accessory: {
7793
type: "external_select",
78-
action_id: `category_select-${event.ts}`,
94+
action_id: uuid,
7995
placeholder: {
8096
type: "plain_text",
8197
text: "Select a category",
@@ -130,22 +146,34 @@ app.options(/category_select-.*/, async ({ options, ack }) => {
130146
});
131147

132148
// Listen for the interaction from the dropdown menu
133-
app.action(/category_select-.*/, async ({ body, ack, say }) => {
149+
app.action(/category_select-.*/, async ({ body, ack, client }) => {
134150
// Acknowledge the action
135151
await ack();
136152
console.log("body", body);
137-
// const text = atob(body.actions[0].action_id.split("-")[1]); Currently not used since it caused bugs, so text is just an empty string
138-
const text = "";
139-
console.log("text", text);
153+
const uuid = body.actions[0].action_id; // Extract the UUID from the action ID
154+
const originalText = myStore[uuid] ?? ''; // Retrieve the original text
155+
console.log("text", originalText);
140156
// Respond to the user's selection
141157
const selectedCategory = body.actions[0].selected_option.text.text;
142158
const dropdown_id = body.actions[0].action_id;
143159
try {
144-
addOrUpdateInc(body.user.username, text, selectedCategory, dropdown_id);
145-
await say(`You selected: ${selectedCategory}`);
160+
// Add or update the incident
161+
await addOrUpdateInc(body.user.username, originalText, selectedCategory, dropdown_id);
162+
163+
// Send an ephemeral message with a checkmark emoji
164+
await client.chat.postEphemeral({
165+
channel: body.channel.id, // Send it in the same channel
166+
user: body.user.id, // Send it to the user who made the selection
167+
text: `✅ You selected: ${selectedCategory}`, // Message with checkmark
168+
});
146169
} catch (error) {
147-
say("There was an error adding the incident. Please try again later.");
148-
console.error("Error adding incident:", error);
170+
// Handle any errors
171+
await client.chat.postEphemeral({
172+
channel: body.channel.id,
173+
user: body.user.id,
174+
text: "❌ There was an error adding the incident. Please try again later.",
175+
});
176+
console.error("Error adding incident:", error);
149177
}
150178

151179
});

0 commit comments

Comments
 (0)