a fun bot for the hc slack
1import type { AnyMessageBlock } from "slack-edge";
2import TakesConfig from "../../../libs/config";
3import { getCompletedTakes } from "../services/database";
4import type { MessageResponse } from "../types";
5import { calculateElapsedTime } from "../../../libs/time-periods";
6import { prettyPrintTime } from "../../../libs/time";
7
8export async function handleHistory(userId: string): Promise<MessageResponse> {
9 // Get completed takes for the user
10 const completedTakes = (
11 await getCompletedTakes(userId, TakesConfig.MAX_HISTORY_ITEMS)
12 ).sort(
13 (a, b) =>
14 (b.completedAt?.getTime() ?? 0) - (a.completedAt?.getTime() ?? 0),
15 );
16
17 if (completedTakes.length === 0) {
18 return {
19 text: "You haven't completed any takes sessions yet.",
20 response_type: "ephemeral",
21 };
22 }
23
24 // Create blocks for each completed take
25 const historyBlocks: AnyMessageBlock[] = [
26 {
27 type: "header",
28 text: {
29 type: "plain_text",
30 text: `📋 Your most recent ${completedTakes.length} Takes sessions`,
31 emoji: true,
32 },
33 },
34 ];
35
36 for (const take of completedTakes) {
37 const elapsedTime = calculateElapsedTime(JSON.parse(take.periods));
38
39 const notes = take.notes ? `\n• Notes: ${take.notes}` : "";
40 const description = take.description
41 ? `\n• Description: ${take.description}\n`
42 : "";
43
44 historyBlocks.push({
45 type: "section",
46 text: {
47 type: "mrkdwn",
48 text: `*Duration:* \`${prettyPrintTime(elapsedTime)}\`\n*Status:* ${take.status}\n${notes ? `*Notes:* ${take.notes}\n` : ""}${description ? `*Description:* ${take.description}\n` : ""}`,
49 },
50 });
51
52 // Add a divider between entries
53 if (take !== completedTakes[completedTakes.length - 1]) {
54 historyBlocks.push({
55 type: "divider",
56 });
57 }
58 }
59
60 // Add actions block
61 historyBlocks.push({
62 type: "actions",
63 elements: [
64 {
65 type: "button",
66 text: {
67 type: "plain_text",
68 text: "🎬 Start New Session",
69 emoji: true,
70 },
71 value: "start",
72 action_id: "takes_start",
73 },
74 {
75 type: "button",
76 text: {
77 type: "plain_text",
78 text: "👁️ Status",
79 emoji: true,
80 },
81 value: "status",
82 action_id: "takes_status",
83 },
84 {
85 type: "button",
86 text: {
87 type: "plain_text",
88 text: "🔄 Refresh",
89 emoji: true,
90 },
91 value: "status",
92 action_id: "takes_history",
93 },
94 ],
95 });
96
97 return {
98 text: `Your recent takes history (${completedTakes.length} sessions)`,
99 response_type: "ephemeral",
100 blocks: historyBlocks,
101 };
102}