# personal_site_snippets.patch -rw-r--r-- 15.2 KiB View raw
                                                                                
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
f5767ea switch to dynamic snippet pages
diff --git a/src/backends/snippets.ts b/src/backends/snippets.ts
index f64f65e..c29c670 100644
--- a/src/backends/snippets.ts
+++ b/src/backends/snippets.ts
@@ -11,6 +11,14 @@ export type Snippet = {
   publishedDate: Dayjs;
 };
 
+type RawSnippet = {
+  title: string;
+  slug: string;
+  markdown: string;
+  tags: string[];
+  "pub-date": string;
+};
+
 const host = process.env.SNIPPETS_HOST! ?? import.meta.env.SNIPPETS_HOST;
 
 export async function getTags(): Promise<{ tag: string; count: number }[]> {
@@ -23,16 +31,19 @@ export async function getTags(): Promise<{ tag: string; count: number }[]> {
   return data;
 }
 
-export async function getSnippetById(id: string): Promise<Snippet[]> {
+export async function getSnippetBySlug(slug: string): Promise<Snippet> {
   const params = new URLSearchParams();
-  params.append("id", id);
+  params.append("slug", slug);
 
-  const res = await fetch(`${host}/api/snippet?${params}`);
-  const data = await res.json();
+  const res = await fetch(`${host}/api/snippet-by-slug?${params}`);
+  const data = (await res.json()) as RawSnippet;
   if (res.status !== 200) {
     throw new Error(`Failed to fetch snippets: ${res.status}`);
   }
-  return data;
+  return {
+    ...data,
+    publishedDate: dayjs(data["pub-date"]),
+  };
 }
 
 export async function getSnippetsByTag(
diff --git a/src/pages/tech/snippets/[slug].astro b/src/pages/tech/snippets/[slug].astro
index 6101673..1872555 100644
--- a/src/pages/tech/snippets/[slug].astro
+++ b/src/pages/tech/snippets/[slug].astro
@@ -1,32 +1,13 @@
 ---
 import BasePage from '../../../layouts/BasePage.astro';
-import { GET_SNIPPETS_STEP, getSnippetById, getSnippets } from '../../../backends/snippets';
-import { marked } from "marked";
+import { getSnippetBySlug } from '../../../backends/snippets';
 import Snippet from '../../../components/Snippet.astro';
 
-export async function getStaticPaths() {
-  const snippets = [];
-  let page = 1;
-  while(true) {
-    const newSnippets = await getSnippets(page);
-    snippets.push(...newSnippets);
-    if (newSnippets.length < GET_SNIPPETS_STEP) break;
-    page++;
-  }
-  return snippets.map(snippet => ({
-    params: { slug: snippet.slug },
-    props: { snippet },
-  }));
-}
-
-export const prerender = true;
-const { snippet } = Astro.props;
-
-// // TODO: Get snippets by tag
-// const snippets = await getSnippetById(tag.tag);
-// snippets.sort((a, b) => b.publishedDate.diff(a.publishedDate));
+const { slug } = Astro.params;
+const snippet = await getSnippetBySlug(slug!);
 
 ---
 <BasePage>
     <Snippet snippet={snippet} />
+    <a href="/tech/snippets">all snippets</a>
 </BasePage>
diff --git a/src/pages/tech/snippets/index.astro b/src/pages/tech/snippets/index.astro
index 284574d..1ec4d28 100644
--- a/src/pages/tech/snippets/index.astro
+++ b/src/pages/tech/snippets/index.astro
@@ -1,13 +1,18 @@
 ---
 import BasePage from "../../../layouts/BasePage.astro";
-import { getSnippets, getTags } from "../../../backends/snippets";
+import { GET_SNIPPETS_STEP, getSnippets, getTags } from "../../../backends/snippets";
 import Snippet from "../../../components/Snippet.astro";
 import Accordian from "../../../components/solid/Accordion";
+import { logger } from "../../../backends/logger";
 
 
 const title = "Snippets";
 const description = "Collection of my code snippets";
-const snippets = await getSnippets();
+
+const page = Number(Astro.url.searchParams.get("page") ?? "1");
+logger.info({params: Astro.params}, 'params');
+const snippets = await getSnippets(page);
+
 let tags = await getTags();
 tags = tags.sort((a, b) => b.count - a.count);
 ---
@@ -29,6 +34,10 @@ tags = tags.sort((a, b) => b.count - a.count);
       <div>
           {snippets.map(snippet => (<Snippet snippet={snippet} /> ))}
       </div>
+
+      {snippets.length >= GET_SNIPPETS_STEP && (
+        <a href={`/tech/snippets?page=${page + 1}`}>next page</a>
+      )}
   </main>
 </BasePage>
 
diff --git a/src/pages/tech/snippets/tags/[tag].astro b/src/pages/tech/snippets/tags/[tag].astro
index adc129b..f668d58 100644
--- a/src/pages/tech/snippets/tags/[tag].astro
+++ b/src/pages/tech/snippets/tags/[tag].astro
@@ -1,26 +1,16 @@
 ---
-import { getSnippetsByTag, getTags } from '../../../../backends/snippets';
+import { getSnippetsByTag } from '../../../../backends/snippets';
 import BasePage from '../../../../layouts/BasePage.astro';
+const { tag } = Astro.params;
 
-export async function getStaticPaths() {
-  const tags = await getTags();
-  return tags.map(tag => ({
-    params: { tag: tag.tag },
-    props: { tag },
-  }));
-}
-
-export const prerender = true;
-const { tag } = Astro.props;
-
-// TODO: Get snippets by tag
-const snippets = await getSnippetsByTag(tag.tag);
+const snippets = await getSnippetsByTag(tag!);
 snippets.sort((a, b) => b.publishedDate.diff(a.publishedDate));
 
 ---
 <BasePage>
-    <p>Snippets with tag {tag.tag}</p>
+    <p>Snippets with tag {tag}</p>
     <ul>
         {snippets.map(snippet => (<li><a href=`/tech/snippets/${snippet.slug}`>{snippet.title} {snippet.publishedDate.format('DD-MM-YY')}</a></li>))}
     </ul>
+    <a href="/tech/snippets">all snippets</a>
 </BasePage>
b73b02d add snippet section under tech
diff --git a/package.json b/package.json
index 2ccd42f..9fd4f1f 100644
--- a/package.json
+++ b/package.json
@@ -24,6 +24,7 @@
     "htmx.org": "^1.9.12",
     "jose": "^5.6.2",
     "marked": "^13.0.1",
+    "pino": "^9.7.0",
     "pocketbase": "^0.21.3",
     "solid-js": "^1.8.17",
     "typescript": "^5.4.5",
diff --git a/src/backends/logger.ts b/src/backends/logger.ts
new file mode 100644
index 0000000..ed26420
--- /dev/null
+++ b/src/backends/logger.ts
@@ -0,0 +1,3 @@
+import pino from "pino";
+
+export const logger = pino();
diff --git a/src/backends/microBlog.ts b/src/backends/microBlog.ts
index 96c9b79..3c085e2 100644
--- a/src/backends/microBlog.ts
+++ b/src/backends/microBlog.ts
@@ -1,4 +1,6 @@
 import PocketBase from "pocketbase";
+import type { AstroIntegration } from "astro";
+import { logger } from "./logger";
 
 export type MicroBlogPostImage = {
   id: string;
@@ -37,6 +39,7 @@ class MicroBlogBackend {
   }
 
   private async login() {
+    logger.info("Logging in to PocketBase");
     const userName = "personal_site_astro";
     const pw =
       process.env.POCKET_BASE_PASSWORD! ?? import.meta.env.POCKET_BASE_PASSWORD;
diff --git a/src/backends/snippets.ts b/src/backends/snippets.ts
new file mode 100644
index 0000000..f64f65e
--- /dev/null
+++ b/src/backends/snippets.ts
@@ -0,0 +1,89 @@
+import { logger } from "./logger";
+import { dayjs } from "../utils/time";
+import type { Dayjs } from "dayjs";
+
+export type Snippet = {
+  // id: string;
+  title: string;
+  slug: string;
+  markdown: string;
+  tags: string[];
+  publishedDate: Dayjs;
+};
+
+const host = process.env.SNIPPETS_HOST! ?? import.meta.env.SNIPPETS_HOST;
+
+export async function getTags(): Promise<{ tag: string; count: number }[]> {
+  const res = await fetch(`${host}/api/tags`);
+  const data = await res.json();
+  if (res.status !== 200) {
+    throw new Error(`Failed to fetch tags: ${res.status}`);
+  }
+
+  return data;
+}
+
+export async function getSnippetById(id: string): Promise<Snippet[]> {
+  const params = new URLSearchParams();
+  params.append("id", id);
+
+  const res = await fetch(`${host}/api/snippet?${params}`);
+  const data = await res.json();
+  if (res.status !== 200) {
+    throw new Error(`Failed to fetch snippets: ${res.status}`);
+  }
+  return data;
+}
+
+export async function getSnippetsByTag(
+  tag: string
+): Promise<{ title: string; slug: string; publishedDate: Dayjs }[]> {
+  const params = new URLSearchParams();
+  params.append("tag", tag);
+
+  const res = await fetch(`${host}/api/tag?${params}`);
+  const data = (await res.json()) as {
+    title: string;
+    slug: string;
+    "pub-date": string;
+  }[];
+  if (res.status !== 200) {
+    throw new Error(`Failed to fetch snippets: ${res.status}`);
+  }
+
+  return data.map((snippet) => {
+    return {
+      title: snippet.title,
+      slug: snippet.slug,
+      publishedDate: dayjs(snippet["pub-date"]),
+    };
+  });
+}
+
+export const GET_SNIPPETS_STEP = 25;
+export async function getSnippets(page = 1): Promise<Snippet[]> {
+  logger.info({ page }, "Getting snippets");
+  const params = new URLSearchParams();
+  params.append("limit", "25");
+  params.append("skip", String((page - 1) * GET_SNIPPETS_STEP));
+
+  const res = await fetch(`${host}/api/snippets?${params}`);
+  const data = (await res.json()) as {
+    title: string;
+    "pub-date": string;
+    tags: string[];
+    slug: string;
+    markdown: string;
+    // id: string;
+  }[];
+  if (res.status !== 200) {
+    throw new Error(`Failed to fetch snippets: ${res.status}`);
+  }
+
+  return data.map((snippet) => {
+    return {
+      ...snippet,
+      publishedDate: dayjs(snippet["pub-date"]),
+    };
+  });
+}
diff --git a/src/components/Snippet.astro b/src/components/Snippet.astro
new file mode 100644
index 0000000..4e847b2
--- /dev/null
+++ b/src/components/Snippet.astro
@@ -0,0 +1,41 @@
+---
+import { marked } from "marked";
+import type { Snippet } from "../backends/snippets";
+interface Props {
+  snippet: Snippet;
+}
+
+const { snippet } = Astro.props;
+
+---
+<div class="snippet">
+    <h3 class="title">{snippet.title}</h3>
+    <div class="row">
+        <p class="title">{snippet.publishedDate.format("DD-MM-YY")}</p>
+        <a href={`/tech/snippets/${snippet.slug}`}>%</a>
+    </div>
+    <div set:html={marked.parse(snippet.markdown)} />
+    <p>- [ {snippet.tags.map(tag => (
+        <a class="tag" href={`/tech/snippets/tags/${tag}`}>{tag}</a>
+        ))}]
+    </p>
+</div>
+<style>
+.row {
+    display: flex;
+}
+.snippet {
+    .title {
+        margin: 0;
+        margin-right: 0.5rem;
+        display: inline-block;
+    }
+    .tag {
+        display: inline-block;
+        margin-right: 0.25rem;
+    }
+    padding: 1rem;
+    margin-bottom: 1rem;
+    border: 1px solid black;
+}
+</style>
diff --git a/src/components/solid/Accordion.tsx b/src/components/solid/Accordion.tsx
new file mode 100644
index 0000000..5cc5dc7
--- /dev/null
+++ b/src/components/solid/Accordion.tsx
@@ -0,0 +1,26 @@
+import { createSignal, type JSX } from "solid-js";
+
+function Accordion({ children }: { children: JSX.Element }) {
+  const [open, setOpen] = createSignal<boolean>(false);
+  return (
+    <>
+      <div
+        style={{
+          "max-height": open() ? "100%" : "3.75rem",
+          overflow: "hidden",
+          transition: "max-height 0.3s ease-in-out",
+        }}
+        classList={{ open: open() }}
+      >
+        {children}
+      </div>
+      {!open() && (
+        <button type="button" onClick={() => setOpen(true)}>
+          show more
+        </button>
+      )}
+    </>
+  );
+}
+
+export default Accordion;
diff --git a/src/pages/tech/index.astro b/src/pages/tech/index.astro
index 193b3a1..3b32097 100644
--- a/src/pages/tech/index.astro
+++ b/src/pages/tech/index.astro
@@ -8,7 +8,8 @@ import  MicroBlogPost  from "../../components/solid/MicroBlogPost";
 import BasePage from "../../layouts/BasePage.astro";
 import { authCookieName, verifyJWT } from "../../utils/auth";
 
-let { title, description  } = Astro.params;
+const title = "Tech";
+const description = "A hub for my adventures in software and hardware.";
 const posts = await getCollection('techBlog');
 
 dayjs.extend(relativeTime)
@@ -28,10 +29,13 @@ if (jwt) {
 <BasePage title={title} description={description}>
   <main>
     <div class="content">
-      <slot />
-
       <h1>Tech</h1>
       <p>A hub for my adventures in software and hardware.</p>
+      <ul>
+          <li>
+              <a href="/tech/snippets">Code Snippets</a>
+          </li>
+      </ul>
       <h2>Recent tech related micro-blog posts</h2>
       {microBlogPosts.map((post) => <MicroBlogPost post={post} />)}
       {loggedIn && (<a href="/micro-blog/edit-groups">Add more tech posts</a>)}
diff --git a/src/pages/tech/snippets/[slug].astro b/src/pages/tech/snippets/[slug].astro
new file mode 100644
index 0000000..6101673
--- /dev/null
+++ b/src/pages/tech/snippets/[slug].astro
@@ -0,0 +1,32 @@
+---
+import BasePage from '../../../layouts/BasePage.astro';
+import { GET_SNIPPETS_STEP, getSnippetById, getSnippets } from '../../../backends/snippets';
+import { marked } from "marked";
+import Snippet from '../../../components/Snippet.astro';
+
+export async function getStaticPaths() {
+  const snippets = [];
+  let page = 1;
+  while(true) {
+    const newSnippets = await getSnippets(page);
+    snippets.push(...newSnippets);
+    if (newSnippets.length < GET_SNIPPETS_STEP) break;
+    page++;
+  }
+  return snippets.map(snippet => ({
+    params: { slug: snippet.slug },
+    props: { snippet },
+  }));
+}
+
+export const prerender = true;
+const { snippet } = Astro.props;
+
+// // TODO: Get snippets by tag
+// const snippets = await getSnippetById(tag.tag);
+// snippets.sort((a, b) => b.publishedDate.diff(a.publishedDate));
+
+---
+<BasePage>
+    <Snippet snippet={snippet} />
+</BasePage>
diff --git a/src/pages/tech/snippets/index.astro b/src/pages/tech/snippets/index.astro
new file mode 100644
index 0000000..284574d
--- /dev/null
+++ b/src/pages/tech/snippets/index.astro
@@ -0,0 +1,45 @@
+---
+import BasePage from "../../../layouts/BasePage.astro";
+import { getSnippets, getTags } from "../../../backends/snippets";
+import Snippet from "../../../components/Snippet.astro";
+import Accordian from "../../../components/solid/Accordion";
+
+
+const title = "Snippets";
+const description = "Collection of my code snippets";
+const snippets = await getSnippets();
+let tags = await getTags();
+tags = tags.sort((a, b) => b.count - a.count);
+---
+
+<BasePage title={title} description={description}>
+  <main>
+    <div class="content">
+      <h1>Snippets</h1>
+      <p>Collection of my code snippets</p>
+      <p>Tags:</p>
+      <Accordian client:load>
+        <div class="tags">
+            {tags.map(tag => (
+                <a class="tag" href={`/tech/snippets/tags/${tag.tag}`}>{tag.tag} ({tag.count})</a>
+            ))}
+        </div>
+      </Accordian>
+      <hr />
+      <div>
+          {snippets.map(snippet => (<Snippet snippet={snippet} /> ))}
+      </div>
+  </main>
+</BasePage>
+
+<style>
+.tags {
+    display: grid;
+    grid-template-columns: repeat(3, 1fr);
+    gap: 1rem;
+
+    @media (max-width: 768px) {
+        grid-template-columns: repeat(2, 1fr);
+    }
+}
+</style>
diff --git a/src/pages/tech/snippets/tags/[tag].astro b/src/pages/tech/snippets/tags/[tag].astro
new file mode 100644
index 0000000..adc129b
--- /dev/null
+++ b/src/pages/tech/snippets/tags/[tag].astro
@@ -0,0 +1,26 @@
+---
+import { getSnippetsByTag, getTags } from '../../../../backends/snippets';
+import BasePage from '../../../../layouts/BasePage.astro';
+
+export async function getStaticPaths() {
+  const tags = await getTags();
+  return tags.map(tag => ({
+    params: { tag: tag.tag },
+    props: { tag },
+  }));
+}
+
+export const prerender = true;
+const { tag } = Astro.props;
+
+// TODO: Get snippets by tag
+const snippets = await getSnippetsByTag(tag.tag);
+snippets.sort((a, b) => b.publishedDate.diff(a.publishedDate));
+
+---
+<BasePage>
+    <p>Snippets with tag {tag.tag}</p>
+    <ul>
+        {snippets.map(snippet => (<li><a href=`/tech/snippets/${snippet.slug}`>{snippet.title} {snippet.publishedDate.format('DD-MM-YY')}</a></li>))}
+    </ul>
+</BasePage>