Skip to content

Commit 5f838b4

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

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

app/server.js

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

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

@@ -59,6 +71,9 @@ app.event("message", async ({ event, client }) => {
5971
// Ignore everything except new messages
6072
return;
6173
}
74+
const uuid = crypto.randomUUID();
75+
storeWithTTL(event.text); // Store the text with the UUID as the key
76+
6277
console.log("Posting an ephemeral message to user:", event.user, "in channel:", event.channel);
6378
try {
6479
// Respond with an ephemeral message containing a dropdown menu
@@ -75,7 +90,7 @@ app.event("message", async ({ event, client }) => {
7590
},
7691
accessory: {
7792
type: "external_select",
78-
action_id: `category_select-${event.ts}`,
93+
action_id: `category_select-${uuid}`,
7994
placeholder: {
8095
type: "plain_text",
8196
text: "Select a category",
@@ -130,22 +145,34 @@ app.options(/category_select-.*/, async ({ options, ack }) => {
130145
});
131146

132147
// Listen for the interaction from the dropdown menu
133-
app.action(/category_select-.*/, async ({ body, ack, say }) => {
148+
app.action(/category_select-.*/, async ({ body, ack, client }) => {
134149
// Acknowledge the action
135150
await ack();
136151
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);
152+
const uuid = body.actions[0].action_id.split('-')[1]; // Extract the UUID from the action ID
153+
const originalText = myStore[uuid]; // Retrieve the original text
154+
console.log("text", originalText);
140155
// Respond to the user's selection
141156
const selectedCategory = body.actions[0].selected_option.text.text;
142157
const dropdown_id = body.actions[0].action_id;
143158
try {
144-
addOrUpdateInc(body.user.username, text, selectedCategory, dropdown_id);
145-
await say(`You selected: ${selectedCategory}`);
159+
// Add or update the incident
160+
await addOrUpdateInc(body.user.username, originalText, selectedCategory, dropdown_id);
161+
162+
// Send an ephemeral message with a checkmark emoji
163+
await client.chat.postEphemeral({
164+
channel: body.channel.id, // Send it in the same channel
165+
user: body.user.id, // Send it to the user who made the selection
166+
text: `✅ You selected: ${selectedCategory}`, // Message with checkmark
167+
});
146168
} catch (error) {
147-
say("There was an error adding the incident. Please try again later.");
148-
console.error("Error adding incident:", error);
169+
// Handle any errors
170+
await client.chat.postEphemeral({
171+
channel: body.channel.id,
172+
user: body.user.id,
173+
text: "❌ There was an error adding the incident. Please try again later.",
174+
});
175+
console.error("Error adding incident:", error);
149176
}
150177

151178
});

0 commit comments

Comments
 (0)