1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
import { Hono } from "hono";
import { and, eq, asc } from "drizzle-orm";
import { db } from "../db";
import { savedReplies } from "../db/schema";
import type { AuthEnv } from "../middleware/auth";
import { requireAuth } from "../middleware/auth";
import { Layout } from "../views/layout";
const replies = new Hono<AuthEnv>();
replies.use("/settings/replies", requireAuth);
replies.use("/settings/replies/*", requireAuth);
replies.use("/api/user/replies", requireAuth);
function trimBounded(s: string, max: number): string {
const t = s.trim();
return t.length > max ? t.slice(0, max) : t;
}
async function listForUser(userId: string) {
try {
return await db
.select()
.from(savedReplies)
.where(eq(savedReplies.userId, userId))
.orderBy(asc(savedReplies.shortcut));
} catch (err) {
console.error("[saved-replies] list:", err);
return [];
}
}
replies.get("/settings/replies", async (c) => {
const user = c.get("user")!;
const rows = await listForUser(user.id);
const error = c.req.query("error");
const success = c.req.query("success");
return c.html(
<Layout title="Saved replies" user={user}>
<div class="settings-container" style="max-width: 720px">
<h2>Saved replies</h2>
<p style="color: var(--text-muted); font-size: 14px; margin-bottom: 16px">
Canned responses you can insert into any issue or PR comment. The
shortcut is a nickname only you see.
</p>
{error && <div class="auth-error">{decodeURIComponent(error)}</div>}
{success && (
<div class="auth-success">{decodeURIComponent(success)}</div>
)}
<form method="post" action="/settings/replies" style="margin-bottom: 24px">
<div class="form-group">
<label for="shortcut">Shortcut</label>
<input
type="text"
id="shortcut"
name="shortcut"
required
maxLength={64}
placeholder="lgtm"
/>
</div>
<div class="form-group">
<label for="body">Reply body</label>
<textarea
id="body"
name="body"
rows={4}
required
maxLength={4096}
placeholder="LGTM! Thanks for the PR."
style="font-family: var(--font-mono); font-size: 13px"
/>
</div>
<button type="submit" class="btn btn-primary">
Add saved reply
</button>
</form>
{rows.length > 0 && (
<div>
<h3 style="font-size: 16px; margin-bottom: 12px">
Your replies ({rows.length})
</h3>
<div class="saved-replies-list">
{rows.map((r) => (
<details class="saved-reply-item">
<summary>
<code>{r.shortcut}</code>
<span style="color: var(--text-muted); font-size: 12px; margin-left: 8px">
{r.body.slice(0, 80).replace(/\n/g, " ")}
{r.body.length > 80 ? "\u2026" : ""}
</span>
</summary>
<div style="padding: 12px 16px; background: var(--bg-secondary); border-top: 1px solid var(--border)">
<form method="post" action={`/settings/replies/${r.id}`}>
<div class="form-group">
<label>Shortcut</label>
<input
type="text"
name="shortcut"
required
value={r.shortcut}
maxLength={64}
/>
</div>
<div class="form-group">
<label>Body</label>
<textarea
name="body"
rows={4}
required
maxLength={4096}
style="font-family: var(--font-mono); font-size: 13px"
>
{r.body}
</textarea>
</div>
<div style="display: flex; gap: 8px">
<button type="submit" class="btn btn-primary">
Save
</button>
<button
type="submit"
formaction={`/settings/replies/${r.id}/delete`}
class="btn btn-danger"
onclick="return confirm('Delete this saved reply?')"
>
Delete
</button>
</div>
</form>
</div>
</details>
))}
</div>
</div>
)}
</div>
</Layout>
);
});
replies.post("/settings/replies", async (c) => {
const user = c.get("user")!;
const body = await c.req.parseBody();
const shortcut = trimBounded(String(body.shortcut || ""), 64);
const text = trimBounded(String(body.body || ""), 4096);
if (!shortcut || !text) {
return c.redirect(
"/settings/replies?error=" + encodeURIComponent("Shortcut and body are required")
);
}
try {
await db.insert(savedReplies).values({
userId: user.id,
shortcut,
body: text,
});
} catch (err: any) {
if (String(err?.message || err).includes("saved_replies_user_shortcut")) {
return c.redirect(
"/settings/replies?error=" +
encodeURIComponent("You already have a reply with that shortcut")
);
}
console.error("[saved-replies] create:", err);
return c.redirect(
"/settings/replies?error=" + encodeURIComponent("Failed to save")
);
}
return c.redirect(
"/settings/replies?success=" + encodeURIComponent("Reply saved")
);
});
replies.post("/settings/replies/:id", async (c) => {
const user = c.get("user")!;
const id = c.req.param("id");
const body = await c.req.parseBody();
const shortcut = trimBounded(String(body.shortcut || ""), 64);
const text = trimBounded(String(body.body || ""), 4096);
if (!shortcut || !text) {
return c.redirect(
"/settings/replies?error=" + encodeURIComponent("Shortcut and body are required")
);
}
try {
await db
.update(savedReplies)
.set({ shortcut, body: text, updatedAt: new Date() })
.where(and(eq(savedReplies.id, id), eq(savedReplies.userId, user.id)));
} catch (err) {
console.error("[saved-replies] update:", err);
}
return c.redirect(
"/settings/replies?success=" + encodeURIComponent("Reply updated")
);
});
replies.post("/settings/replies/:id/delete", async (c) => {
const user = c.get("user")!;
const id = c.req.param("id");
try {
await db
.delete(savedReplies)
.where(and(eq(savedReplies.id, id), eq(savedReplies.userId, user.id)));
} catch (err) {
console.error("[saved-replies] delete:", err);
}
return c.redirect(
"/settings/replies?success=" + encodeURIComponent("Reply deleted")
);
});
replies.get("/api/user/replies", async (c) => {
const user = c.get("user")!;
const rows = await listForUser(user.id);
return c.json({
ok: true,
replies: rows.map((r) => ({
id: r.id,
shortcut: r.shortcut,
body: r.body,
})),
});
});
export default replies;
|