a fun bot for the hc slack
1import type { AnyMessageBlock } from "slack-edge";
2import type { MessageResponse } from "../types";
3import { prettyPrintTime } from "../../../libs/time";
4import { db } from "../../../libs/db";
5import { takes as takesTable } from "../../../libs/schema";
6import { eq, and, desc } from "drizzle-orm";
7
8export async function handleHistory(userId: string): Promise<MessageResponse> {
9 const takes = await db
10 .select()
11 .from(takesTable)
12 .where(and(eq(takesTable.userId, userId)))
13 .orderBy(desc(takesTable.createdAt));
14
15 const takeTimeMs = takes.reduce(
16 (acc, take) => acc + take.elapsedTime * 1000 * Number(take.multiplier),
17 0,
18 );
19 const takeTime = prettyPrintTime(takeTimeMs);
20
21 // Create blocks for each completed take
22 const historyBlocks: AnyMessageBlock[] = [
23 {
24 type: "header",
25 text: {
26 type: "plain_text",
27 text: `📋 you have uploaded ${takes.length} notes for a total of ${takeTime}`,
28 emoji: true,
29 },
30 },
31 ];
32
33 for (const take of takes) {
34 const notes = take.notes ? `\n• Notes: ${take.notes}` : "";
35 const duration = prettyPrintTime(take.elapsedTime * 1000);
36
37 historyBlocks.push({
38 type: "section",
39 text: {
40 type: "mrkdwn",
41 text: `*Duration:* \`${duration}\`\n${notes ? `*Notes:* ${take.notes}\n` : ""}${take.multiplier !== "1.0" ? `\n*Multiplier:* ${take.multiplier}\n` : ""}`,
42 },
43 });
44
45 // Add a divider between entries
46 if (take !== takes[takes.length - 1]) {
47 historyBlocks.push({
48 type: "divider",
49 });
50 }
51 }
52
53 // Add actions block
54 historyBlocks.push({
55 type: "actions",
56 elements: [
57 {
58 type: "button",
59 text: {
60 type: "plain_text",
61 text: "🏡 Home",
62 emoji: true,
63 },
64 value: "status",
65 action_id: "takes_home",
66 },
67 {
68 type: "button",
69 text: {
70 type: "plain_text",
71 text: "⚙️ Settings",
72 emoji: true,
73 },
74 value: "settings",
75 action_id: "takes_settings",
76 },
77 {
78 type: "button",
79 text: {
80 type: "plain_text",
81 text: "🔄 Refresh",
82 emoji: true,
83 },
84 value: "status",
85 action_id: "takes_history",
86 },
87 ],
88 });
89
90 return {
91 text: `${takes.length} notes for a total of ${takeTime}`,
92 response_type: "ephemeral",
93 blocks: historyBlocks,
94 };
95}