Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Show Space profile posts as photo feed

Anand Baburajane182d1ea8965
diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsx
index 5ff6112f99..63ae21a818 100644
--- a/web/apps/space/src/screens/HomeScreen.tsx
+++ b/web/apps/space/src/screens/HomeScreen.tsx
@@ -51,6 +51,7 @@ import {
spaceText,
spaceTextMuted,
} from "styles/colors";
+import { minimumPostPhotoFrameAspectRatio } from "styles/tiles";
import { spaceTouchTargetSize } from "styles/touch-targets";
import { firstNameFrom, formatSpaceDate } from "utils/display";
import { spacePostPhotos, viewerPhotosFromPost } from "utils/post-photos";
@@ -72,7 +73,6 @@ const feedAvatarSize = 38;
const feedLikeActionSize = spaceTouchTargetSize;
const feedActionIconSize = 20;
const feedHorizontalPadding = "16px";
-const minimumFeedPhotoFrameAspectRatio = 3 / 4;
const feedMediaLoadRootMargin = "640px 0px";
const feedLoadMoreRootMargin = "0px 0px 160px 0px";
const feedRowEnterDurationMs = 460;
@@ -1016,7 +1016,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
dimensionsFromAspectRatio(aspectRatio).width,
};
const photoFrameAspectRatio = Math.max(
- minimumFeedPhotoFrameAspectRatio,
+ minimumPostPhotoFrameAspectRatio,
photoDimensions.width / photoDimensions.height,
);
const [frameAspectRatio, setFrameAspectRatio] = useState(
diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsx
index 9afa356d5a..4798f46e7f 100644
--- a/web/apps/space/src/screens/ProfileScreen.tsx
+++ b/web/apps/space/src/screens/ProfileScreen.tsx
@@ -19,6 +19,7 @@ import {
} from "components/ActionToast";
import { SpaceAvatarImage } from "components/AvatarImage";
import { SpaceButtonSpinner } from "components/ButtonSpinner";
+import { SpaceCaptionText } from "components/CaptionText";
import { ConfirmationActionSheet } from "components/ConfirmationActionSheet";
import { SpaceFeedPostButton } from "components/FeedPostButton";
import {
@@ -52,15 +53,17 @@ import {
spaceText,
spaceTextMuted,
} from "styles/colors";
-import { spaceProfilePostRadius } from "styles/tiles";
+import {
+ minimumPostPhotoFrameAspectRatio,
+ spaceProfilePostRadius,
+} from "styles/tiles";
import { spaceTouchTargetSize } from "styles/touch-targets";
-import { firstNameFrom } from "utils/display";
+import { firstNameFrom, formatSpaceDate } from "utils/display";
import {
spaceDefaultCoverImagePath,
type SpaceDraftPostImage,
} from "utils/post-image";
import { viewerPhotosFromPost } from "utils/post-photos";
-import { profilePhotoGap, profilePhotoRows } from "utils/profile-photo-layout";
import { thumbHashDataURLFromBase64 } from "utils/thumbhash";
const green = "#08C225";
@@ -80,12 +83,10 @@ const profileAvatarTopOffset = 54;
const profileAvatarSize = 132;
const profileCoverHeight =
profileHeaderHeight + profileAvatarTopOffset + profileAvatarSize / 2;
-const photoGridGap = `${profilePhotoGap}px`;
-const photoGridPlaceholderBackground = spaceSurface;
-const photoGridRadius = `${spaceProfilePostRadius}px`;
+const photoPlaceholderBackground = spaceSurface;
const profileCoverRadius = "12px";
-const photoGridLoadRootMargin = "800px 0px";
-const publicPhotoGridLoadRootMargin = "400px 0px";
+const profilePostLoadRootMargin = "800px 0px";
+const publicProfilePostLoadRootMargin = "400px 0px";
interface ProfilePhotoDimensions {
height: number;
width: number;
@@ -116,12 +117,6 @@ interface SelectedProfilePost {
photoIndex: number;
}
-interface ProfilePhotoTile {
- aspectRatio: number;
- index: number;
- item: ProfilePostItem;
-}
-
const profilePostImageCacheKey = (item: ProfilePostItem) =>
[item.id, item.imageAsset?.objectKey ?? item.imageUrl ?? ""].join(":");
@@ -142,7 +137,7 @@ const ProfileStatsSkeleton: React.FC = () => (
<Skeleton
variant="rectangular"
sx={{
- bgcolor: photoGridPlaceholderBackground,
+ bgcolor: photoPlaceholderBackground,
borderRadius: "999px",
height: 18,
transform: "none",
@@ -162,7 +157,7 @@ const ProfileStatsSkeleton: React.FC = () => (
<Skeleton
variant="rectangular"
sx={{
- bgcolor: photoGridPlaceholderBackground,
+ bgcolor: photoPlaceholderBackground,
borderRadius: "999px",
height: 18,
transform: "none",
@@ -237,9 +232,9 @@ const ProfilePostLoadingIndicator: React.FC = () => (
</Box>
);
-interface ProfilePostTileProps {
+interface ProfilePostProps {
+ dimensions: ProfilePhotoDimensions;
displayName: string;
- flexGrow: number;
imageUrl?: string;
index: number;
isUnavailable: boolean;
@@ -251,9 +246,9 @@ interface ProfilePostTileProps {
onRememberDimensions: (itemID: string, image: HTMLImageElement) => void;
}
-const ProfilePostTile: React.FC<ProfilePostTileProps> = ({
+const ProfilePost: React.FC<ProfilePostProps> = ({
+ dimensions,
displayName,
- flexGrow,
imageUrl,
index,
isUnavailable: isPostUnavailable,
@@ -267,13 +262,23 @@ const ProfilePostTile: React.FC<ProfilePostTileProps> = ({
const [shouldLoad, setShouldLoad] = React.useState(Boolean(imageUrl));
const [readyImageUrl, setReadyImageUrl] = React.useState<string>();
const [imageDecodeFailed, setImageDecodeFailed] = React.useState(false);
- const tileRef = React.useRef<HTMLButtonElement | null>(null);
+ const postRef = React.useRef<HTMLElement | null>(null);
const thumbHashDataURL = React.useMemo(
() => thumbHashDataURLFromBase64(item.thumbHash),
[item.thumbHash],
);
const isCurrentImageReady = Boolean(imageUrl && readyImageUrl == imageUrl);
const isUnavailable = isPostUnavailable || imageDecodeFailed;
+ const aspectRatio =
+ dimensions.width > 0 && dimensions.height > 0
+ ? dimensions.width / dimensions.height
+ : 1;
+ const frameAspectRatio = Math.max(
+ minimumPostPhotoFrameAspectRatio,
+ aspectRatio,
+ );
+ const caption = item.caption?.trim();
+ const photoCount = item.photos?.length ?? 1;
React.useEffect(() => {
if (imageUrl) setShouldLoad(true);
@@ -282,7 +287,7 @@ const ProfilePostTile: React.FC<ProfilePostTileProps> = ({
React.useEffect(() => {
if (isUnavailable) return;
if (shouldLoad || imageUrl) return;
- const element = tileRef.current;
+ const element = postRef.current;
if (!element) return;
if (
typeof window == "undefined" ||
@@ -315,114 +320,185 @@ const ProfilePostTile: React.FC<ProfilePostTileProps> = ({
return (
<Box
- ref={tileRef}
- component="button"
- type="button"
- aria-label={
- isUnavailable
- ? "Post unavailable"
- : `Open ${displayName} post ${index + 1}`
- }
- disabled={!imageUrl || isUnavailable}
- onClick={() => {
- if (imageUrl && !isUnavailable) onOpen(imageUrl);
- }}
+ ref={postRef}
+ component="article"
sx={{
- appearance: "none",
- bgcolor: photoGridPlaceholderBackground,
- border: 0,
- borderRadius: photoGridRadius,
- cursor: imageUrl && !isUnavailable ? "pointer" : "default",
- display: "block",
- flex: `${flexGrow} 1 0px`,
- height: "100%",
+ aspectRatio: frameAspectRatio,
+ bgcolor: photoPlaceholderBackground,
+ borderRadius: `${spaceProfilePostRadius}px`,
minWidth: 0,
- opacity: 1,
overflow: "hidden",
- p: 0,
position: "relative",
- "&:focus-visible": {
- outline: `2px solid ${green}`,
- outlineOffset: -2,
+ transition: "aspect-ratio 220ms ease",
+ width: "100%",
+ "@media (prefers-reduced-motion: reduce)": {
+ transition: "none",
},
}}
>
- {!isUnavailable && thumbHashDataURL ? (
- <Box
- component="img"
- alt=""
- aria-hidden
- src={thumbHashDataURL}
- sx={{
- display: "block",
- filter: "blur(14px)",
- height: "100%",
- inset: 0,
- objectFit: "cover",
- objectPosition: "center",
- position: "absolute",
- transform: "scale(1.08)",
- width: "100%",
- }}
- />
- ) : null}
- {!isUnavailable && imageUrl ? (
- <Box
- component="img"
- alt={`${displayName} post ${index + 1}`}
- onLoad={(event) => {
- setReadyImageUrl(imageUrl);
- onRememberDimensions(item.id, event.currentTarget);
- }}
- onError={() => {
- log.warn(
- `Post ${item.postId} is unavailable because the browser could not decode its image`,
- );
- setImageDecodeFailed(true);
- onImageDecodeError();
- }}
- src={imageUrl}
- sx={{
- display: "block",
- height: "100%",
- inset: 0,
- objectFit: "contain",
- objectPosition: "center",
- opacity:
- isCurrentImageReady || !thumbHashDataURL ? 1 : 0,
- position: "absolute",
- transition: thumbHashDataURL
- ? "opacity 220ms ease"
- : "none",
- width: "100%",
- "@media (prefers-reduced-motion: reduce)": {
- opacity: 1,
- transition: "none",
- },
- }}
- />
- ) : null}
+ <Box
+ component="button"
+ type="button"
+ aria-label={
+ isUnavailable
+ ? "Post unavailable"
+ : `Open ${displayName} post ${index + 1}${
+ photoCount > 1 ? `, ${photoCount} photos` : ""
+ }`
+ }
+ disabled={!imageUrl || isUnavailable}
+ onClick={() => {
+ if (imageUrl && !isUnavailable) onOpen(imageUrl);
+ }}
+ sx={{
+ appearance: "none",
+ bgcolor: "transparent",
+ border: 0,
+ cursor: imageUrl && !isUnavailable ? "pointer" : "default",
+ display: "block",
+ height: "100%",
+ p: 0,
+ position: "relative",
+ width: "100%",
+ "&:focus-visible": {
+ outline: `2px solid ${green}`,
+ outlineOffset: -2,
+ },
+ }}
+ >
+ {!isUnavailable && thumbHashDataURL ? (
+ <Box
+ component="img"
+ alt=""
+ aria-hidden
+ src={thumbHashDataURL}
+ sx={{
+ display: "block",
+ filter: "blur(14px)",
+ height: "100%",
+ inset: 0,
+ objectFit: "cover",
+ objectPosition: "center",
+ position: "absolute",
+ transform: "scale(1.08)",
+ width: "100%",
+ }}
+ />
+ ) : null}
+ {!isUnavailable && imageUrl ? (
+ <Box
+ component="img"
+ alt={`${displayName} post ${index + 1}`}
+ onLoad={(event) => {
+ setReadyImageUrl(imageUrl);
+ onRememberDimensions(item.id, event.currentTarget);
+ }}
+ onError={() => {
+ log.warn(
+ `Post ${item.postId} is unavailable because the browser could not decode its image`,
+ );
+ setImageDecodeFailed(true);
+ onImageDecodeError();
+ }}
+ src={imageUrl}
+ sx={{
+ display: "block",
+ height: "100%",
+ inset: 0,
+ objectFit: "cover",
+ objectPosition: "center",
+ opacity:
+ isCurrentImageReady || !thumbHashDataURL
+ ? 1
+ : 0,
+ position: "absolute",
+ transition: thumbHashDataURL
+ ? "opacity 220ms ease"
+ : "none",
+ width: "100%",
+ "@media (prefers-reduced-motion: reduce)": {
+ opacity: 1,
+ transition: "none",
+ },
+ }}
+ />
+ ) : null}
+ {isUnavailable && (
+ <Box
+ sx={{
+ alignItems: "center",
+ color: textSoft,
+ display: "flex",
+ fontSize: 12,
+ fontWeight: 600,
+ height: "100%",
+ justifyContent: "center",
+ width: "100%",
+ }}
+ >
+ Post unavailable
+ </Box>
+ )}
+ </Box>
{!isUnavailable && (
- <SpacePostPhotosBadge
- count={item.photos?.length ?? 1}
- inset={8}
- />
- )}
- {isUnavailable && (
- <Box
- sx={{
- alignItems: "center",
- color: textSoft,
- display: "flex",
- fontSize: 12,
- fontWeight: 600,
- height: "100%",
- justifyContent: "center",
- width: "100%",
- }}
- >
- Post unavailable
- </Box>
+ <>
+ <Box
+ aria-hidden
+ sx={{
+ background:
+ "linear-gradient(180deg, rgba(0, 0, 0, 0.78), rgba(0, 0, 0, 0))",
+ filter: "blur(12px)",
+ height: 100,
+ left: -12,
+ pointerEvents: "none",
+ position: "absolute",
+ right: -12,
+ top: -12,
+ }}
+ />
+ <Box
+ component="time"
+ dateTime={new Date(item.timestampMs).toISOString()}
+ sx={{
+ color: "#C8C8C8",
+ fontFamily: '"Inter Variable", Inter, sans-serif',
+ fontSize: 12,
+ fontWeight: 500,
+ left: 16,
+ lineHeight: "16px",
+ pointerEvents: "none",
+ position: "absolute",
+ top: 16,
+ }}
+ >
+ {formatSpaceDate(item.timestampMs)}
+ </Box>
+ <SpacePostPhotosBadge count={photoCount} inset={12} />
+ {caption && (
+ <Box
+ title={caption}
+ sx={{
+ bottom: 20,
+ color: "#E6E6E6",
+ fontFamily:
+ '"Inter Variable", Inter, sans-serif',
+ fontSize: 13,
+ fontWeight: 600,
+ left: "50%",
+ lineHeight: "21px",
+ maxWidth: "78%",
+ pointerEvents: "none",
+ position: "absolute",
+ textAlign: "center",
+ textWrap: "balance",
+ transform: "translateX(-50%)",
+ width: "max-content",
+ }}
+ >
+ <SpaceCaptionText caption={caption} lineClamp={2} />
+ </Box>
+ )}
+ </>
)}
</Box>
);
@@ -531,8 +607,6 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
Record<string, true>
>({});
const [loadedCoverUrl, setLoadedCoverUrl] = useState<string | null>(null);
- const [postGridWidth, setPostGridWidth] = useState(0);
- const postGridRef = React.useRef<HTMLDivElement | null>(null);
const postInputRef = React.useRef<HTMLInputElement | null>(null);
const postImageLoadsInFlightRef = React.useRef<
Map<string, Promise<string | undefined>>
@@ -573,17 +647,6 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
const canOpenProfileCover = Boolean(onOpenProfileCover);
const canOpenProfilePhoto = Boolean(onOpenProfilePhoto);
const hasProfilePosts = postsSharedCount > 0;
- React.useLayoutEffect(() => {
- const grid = postGridRef.current;
- if (!grid) return;
-
- const observer = new ResizeObserver(([entry]) =>
- setPostGridWidth(entry!.contentRect.width),
- );
- setPostGridWidth(grid.getBoundingClientRect().width);
- observer.observe(grid);
- return () => observer.disconnect();
- }, [hasProfilePosts]);
const shouldShowPostLoadingIndicator =
isPostsLoading && (showPostLoadingIndicator ?? true);
const isCoverImageLoading = Boolean(
@@ -596,8 +659,8 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
? "hidden"
: "like-only";
const postImageLoadRootMargin = isAnonymousPublicProfile
- ? publicPhotoGridLoadRootMargin
- : photoGridLoadRootMargin;
+ ? publicProfilePostLoadRootMargin
+ : profilePostLoadRootMargin;
const closeFriendActions = () => setFriendActionsAnchor(null);
const requestUnfriend = () => {
@@ -734,18 +797,6 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
[loadedPhotoDimensionsByID],
);
- const photoRows = profilePhotoRows(
- visiblePostItems.map((item, index) => {
- const { width, height } = dimensionsForPost(item);
- return {
- aspectRatio: width > 0 && height > 0 ? width / height : 1,
- index,
- item,
- };
- }),
- postGridWidth,
- );
-
const profileViewerPhotos = viewerPostItems.flatMap((item) =>
viewerPhotosFromPost({
...item,
@@ -796,17 +847,14 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
});
};
- const renderPostTile = (
- { aspectRatio, index, item }: ProfilePhotoTile,
- rowAspectRatio: number,
- ) => {
+ const renderPost = (item: ProfilePostItem, index: number) => {
const imageUrl = loadedPostImageURLFor(item);
const isUnavailable = !viewerPostIndexByID.has(item.id);
return (
- <ProfilePostTile
+ <ProfilePost
key={item.id}
+ dimensions={dimensionsForPost(item)}
displayName={displayName}
- flexGrow={aspectRatio / rowAspectRatio}
imageUrl={imageUrl}
index={index}
isUnavailable={isUnavailable}
@@ -1280,8 +1328,7 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
<Skeleton
variant="rounded"
sx={{
- bgcolor:
- photoGridPlaceholderBackground,
+ bgcolor: photoPlaceholderBackground,
height: 26,
transform: "none",
width: 112,
@@ -1583,32 +1630,16 @@ export const ProfileScreen: React.FC<ProfileScreenProps> = ({
>
{hasProfilePosts ? (
<Box
- ref={postGridRef}
sx={{
display: "flex",
flexDirection: "column",
- gap: photoGridGap,
+ gap: "24px",
mt: "6px",
mx: "16px",
width: "calc(100% - 32px)",
}}
>
- {photoRows.map((row) => (
- <Box
- key={row.tiles[0]!.item.id}
- sx={{
- display: "flex",
- flexShrink: 0,
- gap: photoGridGap,
- height: row.height,
- width: "100%",
- }}
- >
- {row.tiles.map((tile) =>
- renderPostTile(tile, row.aspectRatio),
- )}
- </Box>
- ))}
+ {visiblePostItems.map(renderPost)}
</Box>
) : shouldShowPostLoadingIndicator ? (
<ProfilePostLoadingIndicator />
diff --git a/web/apps/space/src/styles/tiles.ts b/web/apps/space/src/styles/tiles.ts
index 4a2574f888..522484526c 100644
--- a/web/apps/space/src/styles/tiles.ts
+++ b/web/apps/space/src/styles/tiles.ts
@@ -1,3 +1,4 @@
+export const minimumPostPhotoFrameAspectRatio = 3 / 4;
export const spaceProfilePostRadius = 16;
export const spaceTileCornerStyles = (radius: number) => ({
diff --git a/web/apps/space/src/utils/profile-photo-layout.ts b/web/apps/space/src/utils/profile-photo-layout.ts
deleted file mode 100644
index 5011be6a46..0000000000
--- a/web/apps/space/src/utils/profile-photo-layout.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-export const profilePhotoGap = 6;
-export const profilePhotoMinRowHeight = 100;
-
-export const profilePhotoRows = <Tile extends { aspectRatio: number }>(
- tiles: Tile[],
- width: number,
-) => {
- const rows: { aspectRatio: number; height: number; tiles: Tile[] }[] = [];
- if (width <= 0) return rows;
-
- const targetRowHeight = width / 2;
- const costs = new Array<number>(tiles.length + 1).fill(Infinity);
- const rowSizes = new Array<number>(tiles.length);
- costs[tiles.length] = 0;
-
- for (let index = tiles.length - 1; index >= 0; index--) {
- let aspectRatio = 0;
- for (
- let rowSize = 1;
- rowSize <= 3 && index + rowSize <= tiles.length;
- rowSize++
- ) {
- aspectRatio += tiles[index + rowSize - 1]!.aspectRatio;
- const height =
- (width - (rowSize - 1) * profilePhotoGap) / aspectRatio;
- if (rowSize > 1 && height < profilePhotoMinRowHeight) continue;
-
- const cost =
- (height / targetRowHeight - 1) ** 2 + costs[index + rowSize]!;
- if (cost <= costs[index]!) {
- costs[index] = cost;
- rowSizes[index] = rowSize;
- }
- }
- }
-
- for (let index = 0; index < tiles.length; ) {
- const rowSize = rowSizes[index]!;
- const rowTiles = tiles.slice(index, index + rowSize);
- const aspectRatio = rowTiles.reduce(
- (sum, tile) => sum + tile.aspectRatio,
- 0,
- );
- rows.push({
- aspectRatio,
- height: (width - (rowSize - 1) * profilePhotoGap) / aspectRatio,
- tiles: rowTiles,
- });
- index += rowSize;
- }
-
- return rows;
-};
diff --git a/web/apps/space/tests/profile-photo-layout.test.ts b/web/apps/space/tests/profile-photo-layout.test.ts
deleted file mode 100644
index 63cfd2686b..0000000000
--- a/web/apps/space/tests/profile-photo-layout.test.ts
+++ /dev/null
@@ -1,132 +0,0 @@
-import { expect, test } from "vitest";
-import {
- profilePhotoGap,
- profilePhotoMinRowHeight,
- profilePhotoRows,
-} from "../src/utils/profile-photo-layout";
-
-const photos = (ratios: number[]) =>
- ratios.map((aspectRatio, id) => ({ id, aspectRatio }));
-
-test.each([
- { count: 2, rowSizes: [2] },
- { count: 3, rowSizes: [3] },
- { count: 4, rowSizes: [2, 2] },
-])(
- "a profile with $count photos uses the adaptive grid",
- ({ count, rowSizes }) => {
- const tiles = photos(new Array<number>(count).fill(2 / 3));
- const rows = profilePhotoRows(tiles, 352);
- expect(rows.map(({ tiles }) => tiles.length)).toEqual(rowSizes);
- expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);
- },
-);
-
-test("a profile with exactly five photos uses the adaptive grid throughout", () => {
- const tiles = photos([3 / 2, 3 / 2, 3 / 2, 3 / 2, 3 / 2]);
- const rows = profilePhotoRows(tiles, 352);
- expect(rows.map(({ tiles }) => tiles.map(({ id }) => id))).toEqual([
- [0, 1],
- [2, 3],
- [4],
- ]);
-});
-
-test("three portraits share a compact row", () => {
- expect(
- profilePhotoRows(
- photos([2 / 3, 3 / 4, 2 / 3, 2 / 3, 3 / 4, 2 / 3]),
- 328,
- ).map(({ tiles }) => tiles.length),
- ).toEqual([3, 3]);
-});
-
-test("a landscape can share a row with two portraits to avoid a lone portrait", () => {
- const tiles = photos([3 / 4, 3 / 4, 16 / 9, 3 / 4, 9 / 16]);
- expect(
- profilePhotoRows(tiles, 363).map(({ tiles }) =>
- tiles.map(({ id }) => id),
- ),
- ).toEqual([
- [0, 1],
- [2, 3, 4],
- ]);
-});
-
-test("a standalone portrait keeps its original proportions", () => {
- const tiles = photos([9 / 16]);
- expect(profilePhotoRows(tiles, 328)).toEqual([
- { aspectRatio: 9 / 16, height: 328 / (9 / 16), tiles },
- ]);
-});
-
-test("similar landscapes are not forced into full-width rows", () => {
- const tiles = photos(new Array<number>(10).fill(3 / 2));
- const rows = profilePhotoRows(tiles, 358);
- expect(rows.map(({ tiles }) => tiles.length)).toEqual([2, 2, 2, 2, 2]);
- expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);
-});
-
-test("wider photos stand alone when that fits better than a short paired row", () => {
- const tiles = photos([3 / 2, 3 / 2, 5 / 3, 5 / 3, 3 / 2, 3 / 2]);
- const rows = profilePhotoRows(tiles, 358);
- expect(rows.map(({ tiles }) => tiles.length)).toEqual([2, 1, 1, 2]);
- expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);
-});
-
-test("row grouping adapts to the available width and accounts for gaps", () => {
- const tiles = photos([1.5, 1.5, 1.5, 1.5, 1.5, 1.5]);
- expect(
- profilePhotoRows(tiles, 299 + profilePhotoGap).map(
- ({ tiles }) => tiles.length,
- ),
- ).toEqual([1, 1, 1, 1, 1, 1]);
- expect(
- profilePhotoRows(tiles, 300 + profilePhotoGap).map(
- ({ tiles }) => tiles.length,
- ),
- ).toEqual([2, 2, 2]);
- expect(
- profilePhotoRows(tiles, 456).map(({ tiles }) => tiles.length),
- ).toEqual([2, 2, 2]);
-});
-
-test("a panorama keeps its aspect ratio even when it cannot reach the minimum", () => {
- const tiles = photos([8, 1, 1, 1, 1]);
- const rows = profilePhotoRows(tiles, 328);
- expect(rows.map(({ tiles }) => tiles.length)).toEqual([1, 2, 2]);
- expect(rows[0]!.aspectRatio).toBe(8);
-});
-
-test.each([288, 328, 358, 568])(
- "rows preserve order, fill a %ipx grid, and meet the minimum when possible",
- (width) => {
- const tiles = photos([1.5, 1.2, 1.8, 0.75, 0.6, 8, 0.05, 1, 2, 1.5]);
- const rows = profilePhotoRows(tiles, width);
- expect(rows.flatMap(({ tiles }) => tiles)).toEqual(tiles);
- for (const row of rows) {
- expect(row.tiles.length).toBeLessThanOrEqual(3);
- const gaps = (row.tiles.length - 1) * profilePhotoGap;
- expect(
- row.tiles.reduce(
- (sum, tile) => sum + tile.aspectRatio * row.height,
- gaps,
- ),
- ).toBeCloseTo(width);
- expect(row.height).toBeGreaterThanOrEqual(
- Math.min(
- profilePhotoMinRowHeight,
- width / row.tiles[0]!.aspectRatio,
- ),
- );
- }
- },
-);
-
-test("an empty grid has no rows", () => {
- expect(profilePhotoRows([], 328)).toEqual([]);
-});
-
-test("the grid waits for its width to be measured", () => {
- expect(profilePhotoRows(photos([3 / 4, 3 / 2]), 0)).toEqual([]);
-});