Skip to content

armgit / ente

Public Imported from GitHub
0

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

[web][photos] Reuse album collection rendering for sidebar search

Aswin Asok211309aa7478
diff --git a/web/apps/photos/src/components/gallery/BarImpl.tsx b/web/apps/photos/src/components/gallery/BarImpl.tsx
index b2d3f1dd15..3c64353531 100644
--- a/web/apps/photos/src/components/gallery/BarImpl.tsx
+++ b/web/apps/photos/src/components/gallery/BarImpl.tsx
@@ -1,12 +1,9 @@
import { CollectionsSortOptions } from "@/components/CollectionsSortOptions";
import { PeopleSortOptions } from "@/components/PeopleSortOptions";
import {
- albumFilters,
visibleAlbumFilters as getVisibleAlbumFilters,
matchesAlbumFilter,
- searchAlbumRows,
type AlbumFilter,
- type AlbumSearchRow,
} from "@/utils/album-filters";
import type { PeopleSortBy } from "@/utils/people-sort";
import { Link05Icon, PinIcon, StarIcon } from "@hugeicons/core-free-icons";
@@ -98,41 +95,54 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
const searchInputRef = useRef<HTMLInputElement>(null);
const [searchQuery, setSearchQuery] = useState("");
const isSearching = mode == "albums" && !!searchQuery.trim();
- const searchRows = useMemo(
- () =>
- isSearching
- ? searchAlbumRows(collectionSummaries, searchQuery)
- : [],
- [collectionSummaries, searchQuery, isSearching],
- );
+ const matchingCollectionSummaries = useMemo(() => {
+ if (!isSearching) return collectionSummaries;
+ const search = searchQuery.trim().toLowerCase();
+ return collectionSummaries.filter((cs) =>
+ cs.name.toLowerCase().includes(search),
+ );
+ }, [collectionSummaries, searchQuery, isSearching]);
const [viewportHeight, setViewportHeight] = useState(0);
const [albumFilter, setAlbumFilter] = useState<AlbumFilter | undefined>(
"ente",
);
const visibleAlbumFilters = useMemo(
- () => getVisibleAlbumFilters(collectionSummaries),
- [collectionSummaries],
+ () =>
+ getVisibleAlbumFilters(collectionSummaries).filter(
+ ({ value }) =>
+ !isSearching ||
+ matchingCollectionSummaries.some((cs) =>
+ matchesAlbumFilter(cs, value),
+ ),
+ ),
+ [collectionSummaries, isSearching, matchingCollectionSummaries],
);
const activeAlbumFilter =
albumFilter === undefined ||
visibleAlbumFilters.some(({ value }) => value == albumFilter)
? albumFilter
- : "ente";
+ : visibleAlbumFilters[0]?.value;
const visibleCollectionSummaries = useMemo(
() =>
mode != "albums"
? collectionSummaries
- : collectionSummaries.filter(
+ : matchingCollectionSummaries.filter(
(cs) =>
activeAlbumFilter !== undefined &&
matchesAlbumFilter(cs, activeAlbumFilter),
),
- [mode, collectionSummaries, activeAlbumFilter],
+ [
+ mode,
+ collectionSummaries,
+ matchingCollectionSummaries,
+ activeAlbumFilter,
+ ],
);
useEffect(() => {
- if (albumFilter != activeAlbumFilter) setAlbumFilter(activeAlbumFilter);
- }, [albumFilter, activeAlbumFilter]);
+ if (!isSearching && albumFilter != activeAlbumFilter)
+ setAlbumFilter(activeAlbumFilter);
+ }, [albumFilter, activeAlbumFilter, isSearching]);
useEffect(() => {
if (
@@ -178,33 +188,24 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
const itemData = useMemo<ItemData>(
() =>
- isSearching
+ mode == "albums" ||
+ mode == "hidden-albums" ||
+ mode == "archive-albums"
? {
- type: "album-search",
- rows: searchRows,
+ type: "collections",
+ collectionSummaries: visibleCollectionSummaries,
+ indented: mode == "albums",
activeCollectionID: activeCollectionID!,
onSelectCollectionID,
}
- : mode == "albums" ||
- mode == "hidden-albums" ||
- mode == "archive-albums"
- ? {
- type: "collections",
- collectionSummaries: visibleCollectionSummaries,
- indented: mode == "albums",
- activeCollectionID: activeCollectionID!,
- onSelectCollectionID,
- }
- : {
- type: "people" as const,
- people,
- activePerson,
- onSelectPerson,
- },
+ : {
+ type: "people" as const,
+ people,
+ activePerson,
+ onSelectPerson,
+ },
[
mode,
- isSearching,
- searchRows,
visibleCollectionSummaries,
activeCollectionID,
onSelectCollectionID,
@@ -228,9 +229,9 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
<Box
id={mode == "albums" ? albumListID : undefined}
sx={{
- flex: mode == "albums" && !isSearching ? "0 1 auto" : 1,
+ flex: mode == "albums" ? "0 1 auto" : 1,
height:
- mode == "albums" && !isSearching
+ mode == "albums"
? getItemCount(itemData) * (mobile ? 46 : 50)
: undefined,
minHeight: 0,
@@ -410,17 +411,13 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
)}
</Stack>
)}
- {isSearching ? (
- searchRows.length ? (
- items
- ) : (
- <Typography
- role="status"
- sx={{ px: "16px", color: "text.muted" }}
- >
- {t("no_results")}
- </Typography>
- )
+ {isSearching && !visibleAlbumFilters.length ? (
+ <Typography
+ role="status"
+ sx={{ px: "16px", color: "text.muted" }}
+ >
+ {t("no_results")}
+ </Typography>
) : mode == "albums" ? (
<Stack
role="group"
@@ -531,12 +528,6 @@ const BarScrollContainer = styled("div")({
});
type ItemData =
- | {
- type: "album-search";
- rows: AlbumSearchRow[];
- activeCollectionID: number;
- onSelectCollectionID: (id: number) => void;
- }
| {
type: "collections";
collectionSummaries: CollectionSummary[];
@@ -553,8 +544,6 @@ type ItemData =
const getItemCount = (data: ItemData) => {
switch (data.type) {
- case "album-search":
- return data.rows.length;
case "collections": {
return data.collectionSummaries.length;
}
@@ -566,12 +555,6 @@ const getItemCount = (data: ItemData) => {
const getItemKey = (index: number, data: ItemData) => {
switch (data.type) {
- case "album-search": {
- const row = data.rows[index]!;
- return row.type == "group"
- ? `group-${row.filter}`
- : `${row.filter}-${row.collectionSummary.id}-${row.collectionSummary.coverFile?.id}`;
- }
case "collections": {
const collectionSummary = data.collectionSummaries[index]!;
return `${data.type}-${collectionSummary.id}-${collectionSummary.coverFile?.id}`;
@@ -589,41 +572,6 @@ const ListItem = memo((props: ListChildComponentProps<ItemData>) => {
let card: React.ReactNode;
switch (data.type) {
- case "album-search": {
- const row = data.rows[index]!;
- card =
- row.type == "group" ? (
- <Stack
- direction="row"
- role="heading"
- aria-level={3}
- sx={{
- height: "100%",
- alignItems: "center",
- px: "16px",
- gap: "12px",
- }}
- >
- <CardText>
- {albumFilters
- .find(({ value }) => value == row.filter)!
- .label()}
- </CardText>
- <ExpandMoreIcon
- aria-hidden
- sx={{ fontSize: 16, color: "text.muted" }}
- />
- </Stack>
- ) : (
- <CollectionBarCard
- collectionSummary={row.collectionSummary}
- activeCollectionID={data.activeCollectionID}
- onSelectCollectionID={data.onSelectCollectionID}
- indented
- />
- );
- break;
- }
case "collections": {
const {
collectionSummaries,
diff --git a/web/apps/photos/src/utils/album-filters.ts b/web/apps/photos/src/utils/album-filters.ts
index 8e8ec7bc2a..df768a7e17 100644
--- a/web/apps/photos/src/utils/album-filters.ts
+++ b/web/apps/photos/src/utils/album-filters.ts
@@ -79,38 +79,3 @@ export const visibleAlbumFilters = (collectionSummaries: CollectionSummary[]) =>
}
return collectionSummaries.some((cs) => matchesAlbumFilter(cs, value));
});
-
-export type AlbumSearchRow =
- | { type: "group"; filter: AlbumFilter }
- | {
- type: "collection";
- filter: AlbumFilter;
- collectionSummary: CollectionSummary;
- };
-
-export const searchAlbumRows = (
- collectionSummaries: CollectionSummary[],
- query: string,
-): AlbumSearchRow[] => {
- const search = query.trim().toLowerCase();
- const matches = collectionSummaries.filter((cs) =>
- cs.name.toLowerCase().includes(search),
- );
- return visibleAlbumFilters(collectionSummaries).flatMap<AlbumSearchRow>(
- ({ value }) => {
- const albums = matches.filter((cs) =>
- matchesAlbumFilter(cs, value),
- );
- return albums.length
- ? [
- { type: "group", filter: value },
- ...albums.map((collectionSummary): AlbumSearchRow => ({
- type: "collection",
- filter: value,
- collectionSummary,
- })),
- ]
- : [];
- },
- );
-};
diff --git a/web/apps/photos/tests/album-filters.test.ts b/web/apps/photos/tests/album-filters.test.ts
index 014b1c4d8a..9952d7edde 100644
--- a/web/apps/photos/tests/album-filters.test.ts
+++ b/web/apps/photos/tests/album-filters.test.ts
@@ -5,7 +5,6 @@ import type {
import { describe, expect, test } from "vitest";
import {
matchesAlbumFilter,
- searchAlbumRows,
visibleAlbumFilters,
type AlbumFilter,
} from "../src/utils/album-filters";
@@ -27,96 +26,6 @@ const album = (
});
describe("album filters shared by the dialog and sidebar", () => {
- test("sidebar search trims its query and matches album names rather than group labels", () => {
- const albums = [
- { ...album(1), name: "Wedding favourites" },
- { ...album(2), name: "Music" },
- ];
- expect(searchAlbumRows(albums, " WEDDING ")).toEqual([
- { type: "group", filter: "ente" },
- {
- type: "collection",
- filter: "ente",
- collectionSummary: albums[0],
- },
- ]);
- expect(searchAlbumRows(albums, "Shared")).toEqual([]);
- expect(searchAlbumRows(albums, "missing")).toEqual([]);
- expect(searchAlbumRows(albums, " ")).toEqual(
- searchAlbumRows(albums, ""),
- );
- });
-
- test("sidebar search preserves group order, album order, and overlapping memberships", () => {
- const albums = [
- album(4, ["quickLink"]),
- album(3, ["sharedIncoming"]),
- album(2, ["sharedOutgoing"]),
- album(1),
- ];
- const rows = searchAlbumRows(albums, "Album");
- expect(
- rows.map((row) =>
- row.type == "group"
- ? row.filter
- : `${row.filter}:${row.collectionSummary.id}`,
- ),
- ).toEqual([
- "ente",
- "ente:3",
- "ente:2",
- "ente:1",
- "shared",
- "shared:2",
- "received",
- "received:3",
- "links",
- "links:4",
- ]);
- expect(albums.map(({ id }) => id)).toEqual([4, 3, 2, 1]);
- expect(searchAlbumRows(albums, "4")).toEqual([
- { type: "group", filter: "links" },
- {
- type: "collection",
- filter: "links",
- collectionSummary: albums[0],
- },
- ]);
- });
-
- test("sidebar search keeps full-list empty-album thresholds and collection eligibility", () => {
- const albums = [album(1, [], 0), album(2, [], 0)];
- expect(
- searchAlbumRows(albums, "1").filter((row) => row.type == "group"),
- ).toEqual([{ type: "group", filter: "ente" }]);
- albums.push(
- album(3, [], 0),
- album(4, ["archived"]),
- album(5, ["system"]),
- );
- expect(
- searchAlbumRows(albums, "1").filter((row) => row.type == "group"),
- ).toEqual([
- { type: "group", filter: "ente" },
- { type: "group", filter: "empty-albums" },
- ]);
- expect(searchAlbumRows(albums, "4")).toEqual([]);
- expect(searchAlbumRows(albums, "5")).toEqual([]);
- });
-
- test("sidebar search reflects updated summaries and upstream sorting", () => {
- const albums = [album(1), album(2)];
- const ids = (summaries: CollectionSummary[]) =>
- searchAlbumRows(summaries, "Album").flatMap((row) =>
- row.type == "collection" ? [row.collectionSummary.id] : [],
- );
- expect(ids(albums)).toEqual([1, 2]);
- expect(ids([...albums].reverse())).toEqual([2, 1]);
- expect(ids([{ ...albums[0]!, name: "Renamed" }, albums[1]!])).toEqual([
- 2,
- ]);
- });
-
test("Ente matches mobile membership, including shared, received, and empty albums", () => {
const albums = [
album(1),