Skip to content

armgit / ente

Public Imported from GitHub
0

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

Restore the original Space grid layout

Anand Baburajand8155b6089f8
diff --git a/web/apps/space/src/components/PostGrid.tsx b/web/apps/space/src/components/PostGrid.tsx
index 9506d26b5f..695e8ce535 100644
--- a/web/apps/space/src/components/PostGrid.tsx
+++ b/web/apps/space/src/components/PostGrid.tsx
@@ -13,12 +13,10 @@ interface PostGridItem {
export const SpacePostGrid = <Item extends PostGridItem>({
items,
postCount,
- fixedRows = false,
renderTile,
}: {
items: Item[];
postCount?: number;
- fixedRows?: boolean;
renderTile: (
item: Item,
index: number,
@@ -27,7 +25,7 @@ export const SpacePostGrid = <Item extends PostGridItem>({
}) => {
const [width, setWidth] = React.useState(0);
const [previousLayout, setLayout] = React.useState(() => ({
- ...postPhotoLayout([], 0, undefined, postCount, fixedRows),
+ ...postPhotoLayout([], 0),
items,
postCount,
}));
@@ -45,7 +43,6 @@ export const SpacePostGrid = <Item extends PostGridItem>({
const layout =
previousLayout.items == items &&
previousLayout.width == width &&
- previousLayout.fixedRows == fixedRows &&
previousLayout.postCount == postCount
? previousLayout
: {
@@ -59,7 +56,6 @@ export const SpacePostGrid = <Item extends PostGridItem>({
width,
previousLayout,
postCount,
- fixedRows,
),
items,
postCount,
diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsx
index dc796f0b83..5e5041af62 100644
--- a/web/apps/space/src/screens/HomeScreen.tsx
+++ b/web/apps/space/src/screens/HomeScreen.tsx
@@ -2311,7 +2311,6 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
{hasFeedItems ? (
<>
<SpacePostGrid
- fixedRows
postCount={
isFeedComplete
? latestFeedEntries.length +
diff --git a/web/apps/space/src/utils/profile-photo-layout.ts b/web/apps/space/src/utils/profile-photo-layout.ts
index 9fe5251121..85b647d9c2 100644
--- a/web/apps/space/src/utils/profile-photo-layout.ts
+++ b/web/apps/space/src/utils/profile-photo-layout.ts
@@ -2,33 +2,6 @@ import { minimumPostPhotoFrameAspectRatio } from "../styles/tiles";
export const profilePhotoGap = 6;
const profilePhotoMinRowHeight = 100;
-const fixedPhotoMinTileWidth = 100;
-const fixedRowPattern = [
- { heightRatio: 0.62, weights: [1.16, 0.84] },
- { heightRatio: 0.34, weights: [0.88, 1.18, 0.94] },
- { heightRatio: 0.49, weights: [0.78, 1.22] },
- { heightRatio: 0.72, weights: [1.28, 0.72] },
- { heightRatio: 0.66, weights: [1] },
- { heightRatio: 0.3, weights: [1.16, 0.8, 1.04] },
- { heightRatio: 0.58, weights: [0.9, 1.1] },
- { heightRatio: 0.9, weights: [1] },
- { heightRatio: 0.46, weights: [1.24, 0.76] },
- { heightRatio: 0.38, weights: [1.03, 1.08, 0.89] },
- { heightRatio: 0.32, weights: [0.82, 1.04, 1.14] },
- { heightRatio: 0.67, weights: [0.76, 1.24] },
- { heightRatio: 0.54, weights: [1] },
- { heightRatio: 0.55, weights: [1.08, 0.92] },
- { heightRatio: 0.36, weights: [1.12, 0.86, 1.02] },
- { heightRatio: 0.76, weights: [0.86, 1.14] },
- { heightRatio: 0.51, weights: [1.3, 0.7] },
- { heightRatio: 0.78, weights: [1] },
- { heightRatio: 0.29, weights: [0.94, 1.22, 0.84] },
- { heightRatio: 0.64, weights: [1.04, 0.96] },
- { heightRatio: 0.6, weights: [1] },
- { heightRatio: 0.48, weights: [0.82, 1.18] },
- { heightRatio: 0.35, weights: [1.2, 0.96, 0.84] },
- { heightRatio: 0.69, weights: [1.2, 0.8] },
-];
interface PhotoRow<Tile> {
aspectRatio: number;
@@ -44,7 +17,6 @@ interface PostPhotoTile {
interface PostPhotoLayout {
width: number;
- fixedRows: boolean;
fullWidth?: boolean;
rows: PhotoRow<PostPhotoTile>[];
}
@@ -54,7 +26,6 @@ export const postPhotoLayout = (
width: number,
previous?: PostPhotoLayout,
postCount?: number,
- fixedRows = false,
): PostPhotoLayout => {
const fullWidth =
previous?.fullWidth ??
@@ -63,7 +34,7 @@ export const postPhotoLayout = (
: undefined);
const rows: PhotoRow<PostPhotoTile>[] = [];
let retainedCount = 0;
- if (previous?.width == width && previous.fixedRows == fixedRows) {
+ if (previous?.width == width) {
for (const row of previous.rows) {
if (
!row.tiles.every(
@@ -80,66 +51,9 @@ export const postPhotoLayout = (
rows.push(
...(fullWidth
? remaining.flatMap((tile) => profilePhotoRows([tile], width))
- : fixedRows
- ? fixedPhotoRows(remaining, width, rows.length)
- : profilePhotoRows(remaining, width)),
+ : profilePhotoRows(remaining, width)),
);
- return { width, fixedRows, fullWidth, rows };
-};
-
-const fixedPhotoRows = (
- tiles: PostPhotoTile[],
- width: number,
- startRow: number,
-) => {
- const rows: PhotoRow<PostPhotoTile>[] = [];
- if (width <= 0) return rows;
-
- const minTileWidth = Math.min(fixedPhotoMinTileWidth, width);
- const maxRowSize = Math.floor(
- (width + profilePhotoGap) / (minTileWidth + profilePhotoGap),
- );
- for (let index = 0; index < tiles.length; ) {
- const pattern =
- fixedRowPattern[(startRow + rows.length) % fixedRowPattern.length]!;
- const rowSize = Math.min(
- pattern.weights.length,
- maxRowSize,
- tiles.length - index,
- );
- const weights = pattern.weights.slice(0, rowSize);
- const totalWeight = weights.reduce((sum, weight) => sum + weight, 0);
- const availableWidth = width - (rowSize - 1) * profilePhotoGap;
- const extraWidths = weights.map((weight) =>
- Math.max(0, (availableWidth * weight) / totalWeight - minTileWidth),
- );
- const totalExtraWidth = extraWidths.reduce(
- (sum, extra) => sum + extra,
- 0,
- );
- const remainingWidth = availableWidth - rowSize * minTileWidth;
- const height = width * pattern.heightRatio;
- const aspectRatio = availableWidth / height;
- rows.push({
- aspectRatio,
- height,
- width,
- tiles: tiles
- .slice(index, index + rowSize)
- .map((tile, tileIndex) => ({
- ...tile,
- aspectRatio:
- (minTileWidth +
- (totalExtraWidth > 0
- ? (remainingWidth * extraWidths[tileIndex]!) /
- totalExtraWidth
- : 0)) /
- height,
- })),
- });
- index += rowSize;
- }
- return rows;
+ return { width, fullWidth, rows };
};
const profilePhotoRows = <Tile extends { aspectRatio: number }>(
diff --git a/web/apps/space/tests/profile-photo-layout.test.ts b/web/apps/space/tests/profile-photo-layout.test.ts
index 5e8a6cbcaf..8ef3c89e0e 100644
--- a/web/apps/space/tests/profile-photo-layout.test.ts
+++ b/web/apps/space/tests/profile-photo-layout.test.ts
@@ -287,193 +287,3 @@ test("resizing recalculates rows to fit the new width", () => {
expect(next.rows).toEqual(postPhotoLayout(tiles, 600).rows);
expect(next.rows).not.toEqual(initial.rows);
});
-
-test.each([288, 328, 358])(
- "fixed rows fill a %ipx grid independently of photo dimensions",
- (width) => {
- const tiles = photos(
- Array.from({ length: 80 }, (_, index) =>
- index % 2 == 0 ? 8 : 0.1,
- ),
- );
- const layout = postPhotoLayout(
- tiles,
- width,
- undefined,
- undefined,
- true,
- );
- const squareLayout = postPhotoLayout(
- tiles.map((tile) => ({ ...tile, aspectRatio: 1 })),
- width,
- undefined,
- undefined,
- true,
- );
- expect(layout).toEqual(squareLayout);
- const rowSizes = new Set(layout.rows.map((row) => row.tiles.length));
- expect(rowSizes).toEqual(new Set(width < 312 ? [1, 2] : [1, 2, 3]));
- expect(
- layout.rows.flatMap((row) => row.tiles.map((tile) => tile.id)),
- ).toEqual(tiles.map((tile) => tile.id));
- for (const rowSize of rowSizes) {
- const heights = layout.rows
- .filter((row) => row.tiles.length == rowSize)
- .map((row) => row.height);
- expect(new Set(heights).size).toBeGreaterThan(3);
- expect(Math.max(...heights) / Math.min(...heights)).toBeGreaterThan(
- 1.25,
- );
- }
- for (const row of layout.rows) {
- expect(row.width).toBe(width);
- expect(row.height).toBeGreaterThan(0);
- expect(
- new Set(row.tiles.map((tile) => tile.aspectRatio)).size,
- ).toBeGreaterThanOrEqual(row.tiles.length > 1 ? 2 : 1);
- if (row.tiles.length == 3) {
- for (const tile of row.tiles) {
- expect(tile.aspectRatio).toBeGreaterThan(0.7);
- expect(tile.aspectRatio).toBeLessThan(1.5);
- }
- }
- expect(
- row.tiles.reduce(
- (sum, tile) => sum + tile.aspectRatio * row.height,
- (row.tiles.length - 1) * profilePhotoGap,
- ),
- ).toBeCloseTo(width);
- }
- },
-);
-
-test.each([99, 100, 205, 206, 288, 311, 312, 328, 358, 600])(
- "fixed rows respect the minimum tile width and fill a %ipx grid",
- (width) => {
- const tiles = photos(new Array<number>(80).fill(1));
- const layout = postPhotoLayout(
- tiles,
- width,
- undefined,
- undefined,
- true,
- );
- expect(Math.max(...layout.rows.map((row) => row.tiles.length))).toBe(
- width < 206 ? 1 : width < 312 ? 2 : 3,
- );
- expect(
- layout.rows.flatMap((row) => row.tiles.map((tile) => tile.id)),
- ).toEqual(tiles.map((tile) => tile.id));
- for (const row of layout.rows) {
- const tileWidths = row.tiles.map(
- (tile) => tile.aspectRatio * row.height,
- );
- for (const tileWidth of tileWidths) {
- expect(tileWidth).toBeGreaterThanOrEqual(
- Math.min(width, 100) - 0.000001,
- );
- }
- expect(
- tileWidths.reduce(
- (sum, tileWidth) => sum + tileWidth,
- (row.tiles.length - 1) * profilePhotoGap,
- ),
- ).toBeCloseTo(width);
- }
- },
-);
-
-test.each([
- { initialCount: 1, lastRowSize: 1, nextRowSize: 3 },
- { initialCount: 3, lastRowSize: 1, nextRowSize: 2 },
- { initialCount: 4, lastRowSize: 2, nextRowSize: 2 },
-])(
- "a partial row after $initialCount photos stays filled and frozen when more arrive",
- ({ initialCount, lastRowSize, nextRowSize }) => {
- const tiles = photos(new Array<number>(30).fill(1));
- const initial = postPhotoLayout(
- tiles.slice(0, initialCount),
- 358,
- undefined,
- undefined,
- true,
- );
- expect(initial.rows.at(-1)!.tiles).toHaveLength(lastRowSize);
- expect(initial.rows.at(-1)!.width).toBe(358);
- expect(initial.rows.flatMap((row) => row.tiles)).toHaveLength(
- initialCount,
- );
-
- const appended = postPhotoLayout(tiles, 358, initial, 30, true);
- initial.rows.forEach((row, index) => {
- expect(appended.rows[index]).toBe(row);
- });
- expect(appended.rows[initial.rows.length]!.tiles).toHaveLength(
- nextRowSize,
- );
- expect(
- appended.rows.flatMap((row) => row.tiles.map((tile) => tile.id)),
- ).toEqual(tiles.map((tile) => tile.id));
- },
-);
-
-test("fixed rows retain their dimensions and pattern position across feed pages", () => {
- const tiles = photos(new Array<number>(90).fill(3 / 2));
- let layout = postPhotoLayout([], 358, undefined, undefined, true);
- for (let count = 20; count <= tiles.length; count += 10) {
- const next = postPhotoLayout(
- tiles.slice(0, count),
- 358,
- layout,
- count == tiles.length ? count : undefined,
- true,
- );
- layout.rows.forEach((row, index) => {
- expect(next.rows[index]).toBe(row);
- });
- expect(
- next.rows.flatMap((row) => row.tiles.map((tile) => tile.id)),
- ).toEqual(tiles.slice(0, count).map((tile) => tile.id));
- expect(next.rows.every((row) => row.width == 358)).toBe(true);
- layout = next;
- }
-});
-
-test.each([1, 2, 3, 4, 5, 6, 7])(
- "fixed rows preserve the existing layout for a complete collection of %i posts",
- (count) => {
- const tiles = photos([16 / 9, 3 / 4, 1, 9 / 16, 2, 0.3, 1.5]).slice(
- 0,
- count,
- );
- const layout = postPhotoLayout(tiles, 358, undefined, count, true);
- expect(layout.fullWidth).toBe(true);
- expect(layout.rows).toEqual(
- postPhotoLayout(tiles, 358, undefined, count).rows,
- );
- },
-);
-
-test("a small collection does not switch to fixed rows during the same visit", () => {
- const tiles = photos(new Array<number>(8).fill(3 / 4));
- const initial = postPhotoLayout(tiles.slice(0, 7), 358, undefined, 7, true);
- const appended = postPhotoLayout(tiles, 358, initial, 8, true);
- expect(appended.fullWidth).toBe(true);
- initial.rows.forEach((row, index) => {
- expect(appended.rows[index]).toBe(row);
- });
- expect(appended.rows.every((row) => row.tiles.length == 1)).toBe(true);
- const reopened = postPhotoLayout(tiles, 358, undefined, 8, true);
- expect(reopened.fullWidth).toBe(false);
- expect(reopened.rows.some((row) => row.tiles.length > 1)).toBe(true);
-});
-
-test("late photo dimensions do not change fixed rows", () => {
- const tiles = photos(new Array<number>(20).fill(1));
- const initial = postPhotoLayout(tiles, 358, undefined, undefined, true);
- const changed = tiles.map((tile) => ({ ...tile, aspectRatio: 0.1 }));
- const next = postPhotoLayout(changed, 358, initial, undefined, true);
- initial.rows.forEach((row, index) => {
- expect(next.rows[index]).toBe(row);
- });
-});