From b4c5361f28ab5e598660202cbcce79c42df89b0d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 4 Jan 2025 01:56:22 +0800 Subject: [PATCH] :mag: Post descriptions --- src/pages/posts/[...id].tsx | 71 +++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/src/pages/posts/[...id].tsx b/src/pages/posts/[...id].tsx index 18da2c1..f50a9d4 100644 --- a/src/pages/posts/[...id].tsx +++ b/src/pages/posts/[...id].tsx @@ -14,9 +14,11 @@ import { Typography, Divider, } from '@mui/material' +import { AttachmentItem } from '@/components/attachments/AttachmentItem' import { GetServerSideProps, InferGetServerSidePropsType } from 'next' import { useEffect, useMemo, useState } from 'react' import { unified } from 'unified' +import Head from 'next/head' import Image from 'next/image' import rehypeSanitize from 'rehype-sanitize' import rehypeStringify from 'rehype-stringify' @@ -24,13 +26,15 @@ import remarkParse from 'remark-parse' import remarkRehype from 'remark-rehype' import CloseIcon from '@mui/icons-material/Close' -import { AttachmentItem } from '@/components/attachments/AttachmentItem' export const getServerSideProps = (async (context) => { const id = context.params!.id as string[] try { const { data: post } = await sni.get('/cgi/co/posts/' + id.join(':')) if (post.body.content) { + if (!post.body.description) { + post.body.description = post.body.content.replaceAll('\n', ' ').substring(0, 200) + } const out = await unified() .use(remarkParse) .use(remarkRehype) @@ -52,7 +56,48 @@ export const getServerSideProps = (async (context) => { }) satisfies GetServerSideProps<{ post: SnPost; attachments: SnAttachment[] }> export default function Post({ post, attachments }: InferGetServerSidePropsType) { - const link = useMemo(() => `https://sn.solsynth.dev/posts/${post.id}`, [post]) + const appLink = useMemo(() => `https://sn.solsynth.dev/posts/${post.id}`, [post]) + const link = useMemo( + () => + post.alias && post.aliasPrefix + ? `https://solsynth.dev/posts/${post.aliasPrefix}/${post.alias}` + : `https://solsynth.dev/posts/${post.id}`, + [post], + ) + + const title = useMemo( + () => + post.body.title + ? `${post.body.title} / @${post.publisher.name} / Solar Network` + : `Post #${post.id} / @${post.publisher.name} / Solar Network`, + [post], + ) + const description = useMemo(() => post.body.description, [post]) + + const image = useMemo(() => { + if (post.body.thumbnail) { + return getAttachmentUrl(post.body.thumbnail) + } + if (attachments) { + const images = attachments.filter((a) => a.mimetype.startsWith('image')) + if (images) return getAttachmentUrl(images[0].rid) + } + return null + }, [post]) + const video = useMemo(() => { + if (attachments) { + const videos = attachments.filter((a) => a.mimetype.startsWith('video')) + if (videos) return getAttachmentUrl(videos[0].rid) + } + return null + }, [post]) + const audio = useMemo(() => { + if (attachments) { + const audios = attachments.filter((a) => a.mimetype.startsWith('audio')) + if (audios) return getAttachmentUrl(audios[0].rid) + } + return null + }, [post]) const [openAppHint, setOpenAppHint] = useState() @@ -70,6 +115,26 @@ export default function Post({ post, attachments }: InferGetServerSidePropsType< return ( <> + + {title} + + + + + + + + + + + + + + {image && } + {video && } + {audio && } + + Open in Solian All feature supported, cross-platform, the official app of Solar Network.{' '} - + Launch