From 737d86f738fd921d568383a38ed1e62b5b799d1c Mon Sep 17 00:00:00 2001 From: Horacio Herrera Date: Wed, 18 Oct 2023 01:24:31 +0200 Subject: [PATCH] frontend(site): add static renderer --- .../site/components/site-static-embeds.tsx | 163 ++++++++++++++++++ frontend/apps/site/publication-page.tsx | 26 ++- .../packages/app/components/static-embeds.tsx | 4 +- frontend/packages/app/pages/publication.tsx | 3 +- .../shared/src/static/static-renderer.tsx | 36 ++-- frontend/packages/ui/src/page-components.tsx | 4 +- 6 files changed, 213 insertions(+), 23 deletions(-) create mode 100644 frontend/apps/site/components/site-static-embeds.tsx diff --git a/frontend/apps/site/components/site-static-embeds.tsx b/frontend/apps/site/components/site-static-embeds.tsx new file mode 100644 index 0000000000..dbdd559062 --- /dev/null +++ b/frontend/apps/site/components/site-static-embeds.tsx @@ -0,0 +1,163 @@ +import {trpc} from '../trpc' +import { + Account, + DefaultStaticBlockUnknown, + Group, + StaticBlockNode, + StaticEmbedProps, + StaticGroup, + blockStyles, + createHmId, + getBlockNodeById, +} from '@mintter/shared' +import {SizableText, UIAvatar, XStack, YStack} from '@mintter/ui' +import {Book} from '@tamagui/lucide-icons' +import {cidURL} from 'ipfs' +import {NextLink} from 'next-link' +import {useRouter} from 'next/router' +import {useMemo, PropsWithChildren} from 'react' + +function EmbedWrapper(props: PropsWithChildren<{hmRef: string}>) { + return ( + + + {props.children} + + + ) +} + +export function StaticBlockPublication(props: StaticEmbedProps) { + const docId = props.type == 'd' ? createHmId('d', props.eid) : undefined + const pub = trpc.publication.get.useQuery( + { + documentId: docId, + versionId: props.version || undefined, + }, + { + enabled: !!docId, + }, + ) + + let embedData = useMemo(() => { + const {data} = pub + + const selectedBlock = + props.blockRef && data?.publication?.document?.children + ? getBlockNodeById(data.publication?.document.children, props.blockRef) + : null + + const embedBlocks = selectedBlock + ? [selectedBlock] + : data?.publication?.document?.children + + return { + ...pub, + data: { + publication: pub.data, + embedBlocks, + }, + } + }, [props.blockRef, pub]) + + return ( + + {embedData.data.embedBlocks?.length ? ( + + {embedData.data.embedBlocks.map((bn, idx) => ( + + ))} + + ) : ( + + )} + + ) +} + +export function StaticBlockGroup(props: StaticEmbedProps) { + const groupId = props.type == 'g' ? createHmId('g', props.eid) : undefined + const groupQuery = trpc.group.get.useQuery({groupId, version: ''}) + + return groupQuery.status == 'success' ? ( + + + + + + + + Group + + + + {groupQuery.data?.group?.title} + + + {groupQuery.data.group?.description} + + + + + + ) : null +} + +export function StaticBlockAccount(props: StaticEmbedProps) { + const accountId = props.type == 'a' ? props.eid : undefined + const accountQuery = trpc.account.get.useQuery({accountId}) + + return accountQuery.status == 'success' ? ( + + + + + + + + Account + + + + {accountQuery.data?.account?.profile?.alias} + + + {accountQuery.data.account?.profile?.bio} + + + + + + ) : null +} + +function stripHMLinkPrefix(link: string) { + return link.replace(/^hm:\//, '') +} diff --git a/frontend/apps/site/publication-page.tsx b/frontend/apps/site/publication-page.tsx index 4cc89ccbf9..283ba9be54 100644 --- a/frontend/apps/site/publication-page.tsx +++ b/frontend/apps/site/publication-page.tsx @@ -6,6 +6,8 @@ import { HMPublication, Publication, StaticBlockNode, + StaticPublication, + StaticPublicationProvider, UnpackedHypermediaId, createHmDocLink, formatBytes, @@ -39,6 +41,12 @@ import Footer from './footer' import {PublicationMetadata} from './publication-metadata' import {SiteHead} from './site-head' import {trpc} from './trpc' +import { + StaticBlockAccount, + StaticBlockGroup, + StaticBlockPublication, +} from 'components/site-static-embeds' +import {useRouter} from 'next/router' export type PublicationPageProps = { // documentId: string @@ -86,6 +94,7 @@ export default function PublicationPage({ contextGroup?: HMGroup | null }) { const media = useMedia() + const router = useRouter() const publication = trpc.publication.get.useQuery({ documentId: documentId, @@ -119,7 +128,22 @@ export default function PublicationPage({ {pub ? ( - + + { + e.preventDefault() + e.stopPropagation() + router.push(href) + }} + > + + + ) : publication.isLoading ? ( ) : ( diff --git a/frontend/packages/app/components/static-embeds.tsx b/frontend/packages/app/components/static-embeds.tsx index b29943395d..b6f5b0fe28 100644 --- a/frontend/packages/app/components/static-embeds.tsx +++ b/frontend/packages/app/components/static-embeds.tsx @@ -118,9 +118,7 @@ export function StaticBlockGroup(props: StaticEmbedProps) { {groupQuery.data?.title} - - Some random group description... - + {groupQuery.data?.description} diff --git a/frontend/packages/app/pages/publication.tsx b/frontend/packages/app/pages/publication.tsx index 1a6434f4ab..a494a976c0 100644 --- a/frontend/packages/app/pages/publication.tsx +++ b/frontend/packages/app/pages/publication.tsx @@ -33,6 +33,7 @@ import {DocumentPlaceholder} from './document-placeholder' export default function PublicationPage() { const route = useNavRoute() + const openUrl = useOpenUrl() if (route.key !== 'publication') throw new Error('Publication page expects publication actor') @@ -57,8 +58,6 @@ export default function PublicationPage() { publication.status == 'success' ? docId : undefined, ) - const openUrl = useOpenUrl() - if (publication.data) { return ( - {publication.document?.children.map((bn, idx) => ( - - ))} + {publication.document?.children?.length && + publication.document?.children?.map((bn, idx) => ( + + ))} ) } @@ -172,7 +178,7 @@ export function StaticBlockNode({ childrenType = 'group', ...props }: { - blockNode: BlockNode + blockNode: BlockNode | HMBlockNode index: number copyBlock?: { docId: string @@ -193,7 +199,7 @@ export function StaticBlockNode({ depth={depth + 1} blockNode={bn} childrenType={blockNode.block!.attributes?.childrenType} - start={blockNode.block?.attributes.start} + start={blockNode.block?.attributes?.start} index={index} embedDepth={ props.embedDepth ? props.embedDepth + 1 : props.embedDepth @@ -282,7 +288,7 @@ let inlineContentProps: SizableTextProps = { } export type StaticBlockProps = { - block: Block + block: Block | HMBlock depth: number } diff --git a/frontend/packages/ui/src/page-components.tsx b/frontend/packages/ui/src/page-components.tsx index aa22751ca0..88d9ede625 100644 --- a/frontend/packages/ui/src/page-components.tsx +++ b/frontend/packages/ui/src/page-components.tsx @@ -92,7 +92,7 @@ const PageSectionContent = styled(YStack, { paddingBottom: '$4', width: '100%', flex: 1, - maxWidth: 680, + maxWidth: 720, $gtLg: { flex: 3, flexGrow: 1, @@ -108,7 +108,7 @@ const PageSectionSide = styled(YStack, { }, }, }, - maxWidth: 680, + maxWidth: 720, alignSelf: 'center', width: '100%', $gtLg: {