Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Commit

Permalink
frontend: add inline embed to publication content
Browse files Browse the repository at this point in the history
  • Loading branch information
horacioh committed Mar 4, 2024
1 parent 44d79d1 commit aed307a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
16 changes: 16 additions & 0 deletions frontend/apps/site/src/site-embeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EmbedContentGroup,
EntityComponentProps,
ErrorBlock,
InlineEmbedComponentProps,
PublicationCardView,
PublicationContentProvider,
UnpackedHypermediaId,
Expand Down Expand Up @@ -55,6 +56,7 @@ export function SitePublicationContentProvider({
PublicationContent: EmbedPublicationContent,
PublicationCard: EmbedPublicationCard,
CommentCard: EmbedComment,
InlineEmbed: SiteInlineEmbed,
}}
onLinkClick={(href, e) => {
e.stopPropagation()
Expand Down Expand Up @@ -309,3 +311,17 @@ export function EmbedAccount(props: EntityComponentProps) {
function stripHMLinkPrefix(link: string) {
return link.replace(/^hm:\//, '')
}

export function SiteInlineEmbed(props: InlineEmbedComponentProps) {
const accountId = props?.type == 'a' ? props.eid : undefined
const accountQuery = trpc.account.get.useQuery({accountId})
const account = accountQuery.data?.account
return (
<SizableText color="$blue9" fontWeight="600">
{(accountId &&
accountQuery.status == 'success' &&
`@${account?.profile?.alias}`) ||
`@${accountId?.slice(0, 5) + '...' + accountId?.slice(-5)}`}
</SizableText>
)
}
16 changes: 15 additions & 1 deletion frontend/packages/app/components/app-embeds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EmbedContentAccount,
EmbedContentGroup,
EntityComponentProps,
InlineEmbedComponentProps,
PublicationCardView,
blockStyles,
createHmId,
Expand Down Expand Up @@ -236,7 +237,20 @@ export function EmbedAccount(props: EntityComponentProps) {

return accountQuery.status == 'success' ? (
<EmbedWrapper hmRef={props.id}>
<EmbedContentAccount account={accountQuery.data} />
<EmbedContentAccount account={accountQuery.data!} />
</EmbedWrapper>
) : null
}

export function AppInlineEmbed(props: InlineEmbedComponentProps) {
const accountId = props?.type == 'a' ? props.eid : undefined
const accountQuery = useAccount(accountId)
return (
<SizableText color="$blue9" fontWeight="600">
{(accountId &&
accountQuery.status == 'success' &&
`@${accountQuery.data?.profile?.alias}`) ||
`@${accountId?.slice(0, 5) + '...' + accountId?.slice(-5)}`}
</SizableText>
)
}
2 changes: 2 additions & 0 deletions frontend/packages/app/pages/publication-content-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import 'allotment/dist/style.css'
import {useAppContext} from '../app-context'
import {
AppInlineEmbed,
EmbedAccount,
EmbedComment,
EmbedGroup,
Expand Down Expand Up @@ -41,6 +42,7 @@ export function AppPublicationContentProvider({
PublicationCard: EmbedPublicationCard,
PublicationContent: EmbedPublicationContent,
CommentCard: EmbedComment,
InlineEmbed: AppInlineEmbed,
}}
onLinkClick={(href, e) => {
e.preventDefault()
Expand Down
15 changes: 13 additions & 2 deletions frontend/packages/shared/src/publication-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export type EntityComponentsRecord = {
PublicationCard: React.FC<EntityComponentProps>
PublicationContent: React.FC<EntityComponentProps>
CommentCard: React.FC<EntityComponentProps>
InlineEmbed: React.FC<InlineEmbedComponentProps>
}

export type PublicationContentContextValue = {
Expand All @@ -100,6 +101,8 @@ export const publicationContentContext =
export type EntityComponentProps = BlockContentProps &
ReturnType<typeof unpackHmId>

export type InlineEmbedComponentProps = ReturnType<typeof unpackHmId>

export function PublicationContentProvider({
children,
debugTop = 0,
Expand Down Expand Up @@ -870,7 +873,10 @@ function InlineContentView({
linkType?: LinkType
fontSize?: number
}) {
const {onLinkClick, textUnit} = usePublicationContentContext()
const {onLinkClick, textUnit, entityComponents} =
usePublicationContentContext()

const InlineEmbed = entityComponents.InlineEmbed

const fSize = fontSize || textUnit
return (
Expand Down Expand Up @@ -976,7 +982,7 @@ function InlineContentView({

return (
<Text
key={index}
key={`${content.type}-${index}`}
color={hmTextColor(linkType)}
textDecorationColor={hmTextColor(linkType)}
style={{textDecorationLine}}
Expand Down Expand Up @@ -1010,6 +1016,11 @@ function InlineContentView({
</a>
)
}

if (content.type == 'inline-embed') {
const unpackedRef = unpackHmId(content.ref)
return <InlineEmbed key={content.ref} {...unpackedRef} />
}
return null
})}
</Text>
Expand Down

0 comments on commit aed307a

Please sign in to comment.