-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathSharedPostPreview.tsx
More file actions
111 lines (108 loc) · 3.64 KB
/
Copy pathSharedPostPreview.tsx
File metadata and controls
111 lines (108 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import type { ReactElement } from 'react';
import React from 'react';
import classNames from 'classnames';
import type { Post } from '../../../graphql/posts';
import type { Source } from '../../../graphql/sources';
import { apiUrl } from '../../../lib/config';
import { useCardCover } from '../../../hooks/feed/useCardCover';
import { ProfileImageSize } from '../../ProfilePicture';
import { Image, ImageType } from '../../image/Image';
import type { ImageProps } from '../../image/Image';
import { SourceAvatar } from '../../profile/source/SourceAvatar';
import {
Typography,
TypographyColor,
TypographyTag,
TypographyType,
} from '../../typography/Typography';
interface SharedPostPreviewProps {
post: Post;
source?: Pick<Source, 'image' | 'handle' | 'name'>;
title?: string;
image?: string;
className?: string;
onShare?: (post: Post) => unknown;
imageProps?: Pick<ImageProps, 'fetchPriority' | 'loading'>;
}
export function SharedPostPreview({
post,
source,
title,
image,
className,
onShare,
imageProps,
}: SharedPostPreviewProps): ReactElement {
const { overlay } = useCardCover({ post, onShare });
const isUnknownSource =
(post.sharedPost?.source?.id ?? 'unknown') === 'unknown';
const unknownSource = post.sharedPost?.domain ?? source?.handle ?? 'unknown';
const pixelRatio = globalThis?.window?.devicePixelRatio ?? 1;
const iconSize = Math.round(16 * pixelRatio);
const sourceImage =
isUnknownSource && post.sharedPost?.domain
? `${apiUrl}/icon?url=${encodeURIComponent(
post.sharedPost?.domain,
)}&size=${iconSize}`
: source?.image;
return (
<div
data-testid="shared-post-preview"
className={classNames(
'relative mb-1 mt-2 h-40 overflow-hidden rounded-12 border border-border-subtlest-tertiary bg-background-default',
className,
)}
>
{overlay}
<div
className={classNames(
'flex h-full flex-col bg-background-default',
!!overlay && 'opacity-16',
)}
>
<div className="relative min-h-0 flex-1 overflow-hidden">
<Image
{...imageProps}
type={ImageType.Post}
src={image}
alt={title || 'Shared post cover image'}
className="size-full overflow-hidden rounded-12 object-cover"
/>
<div className="absolute inset-x-0 top-0 ">
<div className="flex flex-col gap-2 rounded-t-8 bg-background-subtle p-2">
{sourceImage && (source?.handle || isUnknownSource) && (
<div className="flex min-w-0 items-center gap-1">
<SourceAvatar
source={{
image: sourceImage,
handle: isUnknownSource ? unknownSource : source!.handle,
}}
size={ProfileImageSize.Size16}
className="shrink-0"
/>
<Typography
tag={TypographyTag.Span}
type={TypographyType.Caption1}
color={TypographyColor.Primary}
className="truncate font-bold"
>
{isUnknownSource ? unknownSource : source!.name}
</Typography>
</div>
)}
{!!title && (
<Typography
type={TypographyType.Footnote}
color={TypographyColor.Primary}
className="line-clamp-2 break-words"
>
{title}
</Typography>
)}
</div>
</div>
</div>
</div>
</div>
);
}