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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 626 - (hide annotations)
Sat Sep 7 09:38:45 2024 UTC (6 months, 3 weeks ago) by rakinar2
File MIME type: application/typescript
File size: 1724 byte(s)
chore: sync with git

1 rakinar2 575 "use server";
2    
3 rakinar2 626 import index from "@/index.json";
4 rakinar2 575 import { branch } from "@/utils/links";
5    
6 rakinar2 626 const map = {} as Record<string, any>;
7    
8 rakinar2 575 export async function getPageInfo(pathname: string) {
9     "use server";
10    
11 rakinar2 626 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 === "/" ? "/" : "")}page.${pageExtension}`;
28    
29 rakinar2 575 const githubURL = pathname
30 rakinar2 626 ? `https://api.github.com/repos/onesoft-sudo/sudobot/commits?path=${encodeURIComponent(urlEncodedPath)}&sha=${encodeURIComponent(branch)}`
31 rakinar2 575 : null;
32 rakinar2 626
33 rakinar2 575 let lastModifiedDate = new Date();
34     let avatarURL = null;
35 rakinar2 626 let username = null;
36 rakinar2 575
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 rakinar2 626 username = json?.[0]?.author?.name ?? json?.[0]?.author?.login;
49 rakinar2 575
50     if (timestamp) {
51     lastModifiedDate = new Date(timestamp);
52     }
53     } catch (error) {
54     console.error(error);
55     }
56     }
57    
58 rakinar2 626 return { lastModifiedDate, avatarURL, urlEncodedPath, username };
59 rakinar2 575 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26