fix: collection card image preview

This commit is contained in:
Daniel Lautzenheiser 2023-05-25 07:12:08 -04:00
parent 708efb3d66
commit c8e8a37dea
5 changed files with 52 additions and 11 deletions

View File

@ -11,13 +11,14 @@ const PostPreview = ({ entry, widgetFor }) => {
); );
}; };
const PostPreviewCard = ({ entry, theme, hasLocalBackup, widgetFor }) => { const PostPreviewCard = ({ entry, theme, hasLocalBackup, collection }) => {
const date = new Date(entry.data.date); const date = new Date(entry.data.date);
const month = date.getMonth() + 1; const month = date.getMonth() + 1;
const day = date.getDate(); const day = date.getDate();
const image = entry.data.image; const imageField = useMemo(() => collection.fields.find((f) => f.name === 'image'), []);
const image = useMediaAsset(entry.data.image, collection, imageField, entry);
return h( return h(
'div', 'div',

View File

@ -130,12 +130,20 @@ const EntryCard: FC<TranslatedProps<EntryCardProps>> = ({
} }
return ( return (
<Card className="h-full"> <Card className="h-full" title={summary}>
<CardActionArea to={path}> <CardActionArea to={path}>
{image && imageField ? <CardMedia height="140" image={image} /> : null} {image && imageField ? (
<CardMedia
height="140"
image={image}
collection={collection}
field={imageField}
entry={entry}
/>
) : null}
<CardContent> <CardContent>
<div className="flex w-full items-center justify-between"> <div className="flex w-full items-center justify-between">
<div>{summary}</div> <div className="whitespace-nowrap overflow-hidden text-ellipsis">{summary}</div>
{hasLocalBackup ? ( {hasLocalBackup ? (
<InfoIcon <InfoIcon
className=" className="

View File

@ -7,9 +7,10 @@ import type { ReactNode } from 'react';
interface CardProps { interface CardProps {
children: ReactNode | ReactNode[]; children: ReactNode | ReactNode[];
className?: string; className?: string;
title?: string;
} }
const Card = ({ children, className }: CardProps) => { const Card = ({ children, className, title }: CardProps) => {
return ( return (
<div <div
className={classNames( className={classNames(
@ -28,6 +29,7 @@ const Card = ({ children, className }: CardProps) => {
`, `,
className, className,
)} )}
title={title}
> >
{children} {children}
</div> </div>

View File

@ -2,14 +2,33 @@ import React from 'react';
import Image from '../image/Image'; import Image from '../image/Image';
interface CardMediaProps { import type {
BaseField,
Collection,
Entry,
MediaField,
UnknownField,
} from '@staticcms/core/interface';
interface CardMediaProps<EF extends BaseField> {
image: string; image: string;
width?: string | number; width?: string | number;
height?: string | number; height?: string | number;
alt?: string; alt?: string;
collection?: Collection<EF>;
field?: MediaField;
entry?: Entry;
} }
const CardMedia = ({ image, width, height, alt = '' }: CardMediaProps) => { const CardMedia = <EF extends BaseField = UnknownField>({
image,
width,
height,
alt = '',
collection,
field,
entry,
}: CardMediaProps<EF>) => {
return ( return (
<Image <Image
className="rounded-t-lg bg-cover bg-no-repeat bg-center w-full object-cover" className="rounded-t-lg bg-cover bg-no-repeat bg-center w-full object-cover"
@ -19,6 +38,9 @@ const CardMedia = ({ image, width, height, alt = '' }: CardMediaProps) => {
}} }}
src={image} src={image}
alt={alt} alt={alt}
collection={collection}
field={field}
entry={entry}
/> />
); );
}; };

View File

@ -7,7 +7,13 @@ import { selectEditingDraft } from '@staticcms/core/reducers/selectors/entryDraf
import { useAppSelector } from '@staticcms/core/store/hooks'; import { useAppSelector } from '@staticcms/core/store/hooks';
import { isEmpty } from '@staticcms/core/lib/util/string.util'; import { isEmpty } from '@staticcms/core/lib/util/string.util';
import type { BaseField, Collection, MediaField, UnknownField } from '@staticcms/core/interface'; import type {
BaseField,
Collection,
Entry,
MediaField,
UnknownField,
} from '@staticcms/core/interface';
import type { CSSProperties } from 'react'; import type { CSSProperties } from 'react';
export interface ImageProps<EF extends BaseField> { export interface ImageProps<EF extends BaseField> {
@ -17,6 +23,7 @@ export interface ImageProps<EF extends BaseField> {
style?: CSSProperties; style?: CSSProperties;
collection?: Collection<EF>; collection?: Collection<EF>;
field?: MediaField; field?: MediaField;
entry?: Entry;
'data-testid'?: string; 'data-testid'?: string;
} }
@ -27,11 +34,12 @@ const Image = <EF extends BaseField = UnknownField>({
style, style,
collection, collection,
field, field,
entry,
'data-testid': dataTestId, 'data-testid': dataTestId,
}: ImageProps<EF>) => { }: ImageProps<EF>) => {
const entry = useAppSelector(selectEditingDraft); const editingDraft = useAppSelector(selectEditingDraft);
const assetSource = useMediaAsset(src, collection, field, entry); const assetSource = useMediaAsset(src, collection, field, entry ?? editingDraft);
if (isEmpty(src)) { if (isEmpty(src)) {
return ( return (