fix: implement author page

pull/5494/head
Arik Chakma 8 months ago
parent fa483763f1
commit 7f26ce5e2f
  1. 3
      src/components/NavigationDropdown.tsx
  2. 20
      src/lib/video.ts
  3. 19
      src/pages/authors/[authorId].astro
  4. 27
      src/pages/authors/[authorId].json.ts

@ -68,12 +68,13 @@ export function NavigationDropdown() {
})} })}
onClick={() => setIsOpen(true)} onClick={() => setIsOpen(true)}
onMouseOver={() => setIsOpen(true)} onMouseOver={() => setIsOpen(true)}
aria-label="Open Navigation Dropdown"
> >
<Menu className="h-5 w-5" /> <Menu className="h-5 w-5" />
</button> </button>
<div <div
className={cn( className={cn(
'absolute pointer-events-none invisible left-0 top-full z-[999] mt-2 w-48 min-w-[320px] -translate-y-1 rounded-lg bg-slate-800 py-2 opacity-0 shadow-xl transition-all duration-100', 'pointer-events-none invisible absolute left-0 top-full z-[999] mt-2 w-48 min-w-[320px] -translate-y-1 rounded-lg bg-slate-800 py-2 opacity-0 shadow-xl transition-all duration-100',
{ {
'pointer-events-auto visible translate-y-2.5 opacity-100': isOpen, 'pointer-events-auto visible translate-y-2.5 opacity-100': isOpen,
}, },

@ -1,8 +1,8 @@
import type { MarkdownFileType } from './file'; import type { MarkdownFileType } from './file';
import type { AuthorFileType } from './author.ts'; import type { AuthorFileType } from './author.ts';
import { getAllAuthors } from './author.ts'; import { getAllAuthors } from './author.ts';
import type {GuideFileType} from "./guide.ts"; import type { GuideFileType } from './guide.ts';
import {getAllGuides} from "./guide.ts"; import { getAllGuides } from './guide.ts';
export interface VideoFrontmatter { export interface VideoFrontmatter {
title: string; title: string;
@ -75,10 +75,20 @@ export async function getAllVideos(): Promise<VideoFileType[]> {
} }
export async function getVideoById(id: string): Promise<VideoFileType> { export async function getVideoById(id: string): Promise<VideoFileType> {
const video = await import(`../data/videos/${id}.md`); const videoFilesMap: Record<string, VideoFileType> =
import.meta.glob<VideoFileType>('../data/videos/*.md', {
eager: true,
});
const videoFile = Object.values(videoFilesMap).find((videoFile) => {
return videoPathToId(videoFile.file) === id;
});
if (!videoFile) {
throw new Error(`Video with ID ${id} not found`);
}
return { return {
...video, ...videoFile,
id: videoPathToId(video.file), id: videoPathToId(videoFile.file),
}; };
} }

@ -2,24 +2,23 @@
import BaseLayout from '../../layouts/BaseLayout.astro'; import BaseLayout from '../../layouts/BaseLayout.astro';
import AstroIcon from '../../components/AstroIcon.astro'; import AstroIcon from '../../components/AstroIcon.astro';
import { getGuidesByAuthor } from '../../lib/guide'; import { getGuidesByAuthor } from '../../lib/guide';
import { getAllVideos, getVideosByAuthor } from '../../lib/video'; import { getVideosByAuthor } from '../../lib/video';
import GuideListItem from '../../components/GuideListItem.astro'; import GuideListItem from '../../components/GuideListItem.astro';
import { getAuthorById, getAuthorIds } from '../../lib/author'; import { getAuthorById } from '../../lib/author';
import VideoListItem from '../../components/VideoListItem.astro'; import VideoListItem from '../../components/VideoListItem.astro';
interface Params extends Record<string, string | undefined> {} interface Params extends Record<string, string | undefined> {}
export async function getStaticPaths() {
const authorIds = await getAuthorIds();
return authorIds.map((authorId) => ({
params: { authorId },
}));
}
const { authorId } = Astro.params; const { authorId } = Astro.params;
if (!authorId) {
return Astro.redirect('/404');
}
const author = await getAuthorById(authorId); const author = await getAuthorById(authorId);
if (!author) {
return Astro.redirect('/404');
}
const authorFrontmatter = author.frontmatter; const authorFrontmatter = author.frontmatter;
const guides = await getGuidesByAuthor(authorId); const guides = await getGuidesByAuthor(authorId);

@ -1,25 +1,20 @@
import type { APIRoute } from 'astro'; import type { APIRoute } from 'astro';
import { getAuthorById, getAuthorIds } from '../../lib/author'; import { getAuthorById } from '../../lib/author';
export async function getStaticPaths() { export const GET: APIRoute = async function ({ params, request, props }) {
const authorIds = await getAuthorIds(); const { authorId } = params as { authorId: string };
return await Promise.all(
authorIds.map(async (authorId) => {
const authorDetails = await getAuthorById(authorId); const authorDetails = await getAuthorById(authorId);
if (!authorDetails) {
return { return new Response(JSON.stringify({ error: 'Not found' }), {
params: { authorId }, status: 404,
props: { headers: {
authorDetails: authorDetails?.frontmatter || {}, 'Content-Type': 'application/json',
}, },
}; });
}), }
);
}
export const GET: APIRoute = async function ({ params, request, props }) { return new Response(JSON.stringify(authorDetails?.frontmatter), {
return new Response(JSON.stringify(props.authorDetails), {
status: 200, status: 200,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

Loading…
Cancel
Save