/[sudobot]/trunk/docs/actions/pageinfo.ts
ViewVC logotype

Contents of /trunk/docs/actions/pageinfo.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 632 - (show annotations)
Thu Oct 10 17:53:11 2024 UTC (5 months, 2 weeks ago) by rakinar2
File MIME type: application/typescript
File size: 1765 byte(s)
chore: synchronize

1 "use server";
2
3 import index from "@/index.json";
4 import { branch } from "@/utils/links";
5
6 const map = {} as Record<string, any>;
7
8 export async function getPageInfo(pathname: string) {
9 "use server";
10
11 if (Object.keys(map).length === 0) {
12 for (const page of index) {
13 map[page.url] = page;
14 }
15 }
16
17 const trimmedPathname = pathname.replace(/\/$/, "");
18 const path = (map[trimmedPathname] ?? map[trimmedPathname + "/"])?.path;
19 const pageExtension = path?.endsWith(".mdx")
20 ? "mdx"
21 : path?.endsWith(".md")
22 ? "md"
23 : "tsx";
24
25 const urlEncodedPath = `docs/app${
26 pathname === "/" ? "" : "/(docs)"
27 }${pathname.trim() + (pathname[pathname.length - 1] !== "/" || pathname === "/" ? "/" : "")}page.${pageExtension}`;
28
29 const githubURL = pathname
30 ? `https://api.github.com/repos/onesoft-sudo/sudobot/commits?path=${encodeURIComponent(urlEncodedPath)}&sha=${encodeURIComponent(branch)}`
31 : null;
32
33 let lastModifiedDate = new Date();
34 let avatarURL = null;
35 let username = null;
36
37 if (githubURL) {
38 try {
39 const response = await fetch(githubURL, {
40 next: {
41 revalidate: 3600,
42 },
43 });
44
45 const json = await response.json();
46 const timestamp = json?.[0]?.commit?.author?.date;
47 avatarURL = json?.[0]?.author?.avatar_url;
48 username = json?.[0]?.author?.name ?? json?.[0]?.author?.login;
49
50 if (timestamp) {
51 lastModifiedDate = new Date(timestamp);
52 }
53 } catch (error) {
54 console.error(error);
55 }
56 }
57
58 return { lastModifiedDate, avatarURL, urlEncodedPath, username };
59 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26