Skip to content

armgit / ente

Public Imported from GitHub
0

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

fix(photos): show album groups matching sidebar search

AswinAsok777ec770e6f8
diff --git a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
index d1e9121fd3..eccae53100 100644
--- a/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
+++ b/web/apps/photos/src/components/Collections/GalleryBarAndListHeader.tsx
@@ -185,8 +185,22 @@ export const GalleryBarAndListHeader: React.FC<
[sortedCollectionSummaries, uncategorizedCollectionSummaryID, mode],
);
const visibleAlbumFilters = useMemo(
- () => getVisibleAlbumFilters(eligibleCollectionSummaries),
- [eligibleCollectionSummaries],
+ () =>
+ getVisibleAlbumFilters(
+ eligibleCollectionSummaries,
+ albumSearchQuery,
+ ),
+ [eligibleCollectionSummaries, albumSearchQuery],
+ );
+ // Sidebar searches span groups; the overview still follows its selected group.
+ const sidebarCollectionSummaries = useMemo(
+ () =>
+ eligibleCollectionSummaries.filter((album) =>
+ album.name
+ .toLowerCase()
+ .includes(albumSearchQuery.trim().toLowerCase()),
+ ),
+ [eligibleCollectionSummaries, albumSearchQuery],
);
const visibleCollectionSummaries = useMemo(
() =>
@@ -356,7 +370,11 @@ export const GalleryBarAndListHeader: React.FC<
onCreateAlbum={openAlbumCreation}
onShowAllAlbums={showAllAlbums}
onShowAllPeople={showAllPeople}
- collectionSummaries={visibleCollectionSummaries}
+ collectionSummaries={
+ mode == "albums"
+ ? sidebarCollectionSummaries
+ : visibleCollectionSummaries
+ }
visibleAlbumFilters={visibleAlbumFilters}
/>
diff --git a/web/apps/photos/src/components/gallery/BarImpl.tsx b/web/apps/photos/src/components/gallery/BarImpl.tsx
index 5596a744ed..9ec7c544e3 100644
--- a/web/apps/photos/src/components/gallery/BarImpl.tsx
+++ b/web/apps/photos/src/components/gallery/BarImpl.tsx
@@ -1,6 +1,10 @@
import { CollectionsSortOptions } from "@/components/CollectionsSortOptions";
import { PeopleSortOptions } from "@/components/PeopleSortOptions";
-import type { AlbumFilter, albumFilters } from "@/utils/album-filters";
+import {
+ matchesAlbumFilter,
+ type AlbumFilter,
+ type albumFilters,
+} from "@/utils/album-filters";
import type { PeopleSortBy } from "@/utils/people-sort";
import {
Delete02Icon,
@@ -145,6 +149,23 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
const [expandedAlbumFilter, setExpandedAlbumFilter] = useState<
AlbumFilter | undefined
>(albumFilter);
+ const displayedAlbumFilter =
+ isSearching &&
+ expandedAlbumFilter !== undefined &&
+ !visibleAlbumFilters.some(({ value }) => value == expandedAlbumFilter)
+ ? visibleAlbumFilters[0]?.value
+ : expandedAlbumFilter;
+ const displayedCollectionSummaries = useMemo(
+ () =>
+ mode == "albums"
+ ? collectionSummaries.filter(
+ (cs) =>
+ displayedAlbumFilter !== undefined &&
+ matchesAlbumFilter(cs, displayedAlbumFilter),
+ )
+ : collectionSummaries,
+ [mode, collectionSummaries, displayedAlbumFilter],
+ );
useEffect(() => {
setExpandedAlbumFilter(albumFilter);
@@ -173,7 +194,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
case "albums":
case "hidden-albums":
case "archive-albums":
- i = collectionSummaries.findIndex(
+ i = displayedCollectionSummaries.findIndex(
({ id }) => id == activeCollectionID,
);
break;
@@ -186,7 +207,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
if (i != -1) listRef.current.scrollToItem(i, "smart");
}, [
mode,
- collectionSummaries,
+ displayedCollectionSummaries,
activeCollectionID,
matchingPeople,
activePerson,
@@ -204,7 +225,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
mode == "archive-albums"
? {
type: "collections",
- collectionSummaries: collectionSummaries,
+ collectionSummaries: displayedCollectionSummaries,
indented: mode == "albums",
activeCollectionID: activeCollectionID!,
onSelectCollectionID,
@@ -217,7 +238,7 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
},
[
mode,
- collectionSummaries,
+ displayedCollectionSummaries,
activeCollectionID,
onSelectCollectionID,
matchingPeople,
@@ -512,7 +533,10 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
</IconButton>
)}
</Stack>
- {isSearching && !getItemCount(itemData) ? (
+ {isSearching &&
+ (mode == "albums"
+ ? !visibleAlbumFilters.length
+ : !getItemCount(itemData)) ? (
searchEmptyState
) : mode == "albums" ? (
<Stack
@@ -537,16 +561,16 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
: undefined
}
aria-expanded={
- expandedAlbumFilter == filter.value
+ displayedAlbumFilter == filter.value
}
aria-controls={
- expandedAlbumFilter == filter.value
+ displayedAlbumFilter == filter.value
? albumListID
: undefined
}
onClick={() => {
- setExpandedAlbumFilter((current) =>
- current == filter.value
+ setExpandedAlbumFilter(
+ displayedAlbumFilter == filter.value
? undefined
: filter.value,
);
@@ -561,13 +585,13 @@ export const GalleryBarImpl: React.FC<GalleryBarImplProps> = ({
fontSize: 16,
color: "text.muted",
transform:
- expandedAlbumFilter == filter.value
+ displayedAlbumFilter == filter.value
? undefined
: "rotate(-90deg)",
}}
/>
</BarCard>
- {expandedAlbumFilter == filter.value && items}
+ {displayedAlbumFilter == filter.value && items}
</React.Fragment>
))}
</Stack>
diff --git a/web/apps/photos/src/utils/album-filters.ts b/web/apps/photos/src/utils/album-filters.ts
index 96065bf754..17a9d3c9c6 100644
--- a/web/apps/photos/src/utils/album-filters.ts
+++ b/web/apps/photos/src/utils/album-filters.ts
@@ -68,8 +68,21 @@ export const matchesAlbumFilter = (
}
};
-export const visibleAlbumFilters = (collectionSummaries: CollectionSummary[]) =>
- albumFilters.filter(({ value }) => {
+export const visibleAlbumFilters = (
+ collectionSummaries: CollectionSummary[],
+ query = "",
+) => {
+ const normalizedQuery = query.trim().toLowerCase();
+ return albumFilters.filter(({ value }) => {
+ if (
+ normalizedQuery &&
+ !collectionSummaries.some(
+ (cs) =>
+ matchesAlbumFilter(cs, value) &&
+ cs.name.toLowerCase().includes(normalizedQuery),
+ )
+ )
+ return false;
if (value == "ente") return true;
if (value == "empty-albums") {
return (
@@ -79,6 +92,7 @@ export const visibleAlbumFilters = (collectionSummaries: CollectionSummary[]) =>
}
return collectionSummaries.some((cs) => matchesAlbumFilter(cs, value));
});
+};
export const filterAlbumSummaries = (
summaries: CollectionSummary[],
diff --git a/web/apps/photos/tests/album-filters.test.ts b/web/apps/photos/tests/album-filters.test.ts
index 570160f515..bf216f406b 100644
--- a/web/apps/photos/tests/album-filters.test.ts
+++ b/web/apps/photos/tests/album-filters.test.ts
@@ -137,6 +137,34 @@ describe("album filters shared by the dialog and sidebar", () => {
"ente",
]);
});
+
+ test("search shows only groups containing a matching album", () => {
+ const albums = [
+ { ...album(1), name: "Summer" },
+ { ...album(2, ["sharedOutgoing"]), name: "Winter" },
+ { ...album(3, ["sharedIncoming"]), name: "Winter" },
+ { ...album(4, ["quickLink"]), name: "Holiday link" },
+ ];
+ const groups = (query: string) =>
+ visibleAlbumFilters(albums, query).map(({ value }) => value);
+ expect(groups(" HOLIDAY ")).toEqual(["links"]);
+ expect(groups("winter")).toEqual(["ente", "shared", "received"]);
+ expect(groups("summer")).toEqual(["ente"]);
+ expect(groups("missing")).toEqual([]);
+ expect(groups(" ")).toEqual(["ente", "shared", "received", "links"]);
+ });
+
+ test("search preserves empty-group availability when only one empty album matches", () => {
+ const albums = [album(1, [], 0), album(2, [], 0), album(3, [], 0)];
+ expect(
+ visibleAlbumFilters(albums, "Album 1").map(({ value }) => value),
+ ).toEqual(["ente", "empty-albums"]);
+ expect(
+ visibleAlbumFilters(albums.slice(0, 2), "Album 1").map(
+ ({ value }) => value,
+ ),
+ ).toEqual(["ente"]);
+ });
});
describe("album overview search", () => {
diff --git a/web/apps/photos/tests/sidebar-album-search.test.tsx b/web/apps/photos/tests/sidebar-album-search.test.tsx
new file mode 100644
index 0000000000..4f13fe9cf6
--- /dev/null
+++ b/web/apps/photos/tests/sidebar-album-search.test.tsx
@@ -0,0 +1,120 @@
+import { ThemeProvider } from "@mui/material";
+import { photosTheme } from "ente-base/components/utils/theme";
+import type { CollectionSummary } from "ente-new/photos/services/collection-summary";
+import { renderToStaticMarkup } from "react-dom/server";
+import { describe, expect, test, vi } from "vitest";
+import {
+ GalleryBarImpl,
+ type GalleryBarImplProps,
+} from "../src/components/gallery/BarImpl";
+import { visibleAlbumFilters } from "../src/utils/album-filters";
+
+vi.mock("@/components/CollectionsSortOptions", () => ({
+ CollectionsSortOptions: () => null,
+}));
+vi.mock("@/components/PeopleSortOptions", () => ({
+ PeopleSortOptions: () => null,
+}));
+vi.mock("@/utils/album-filters", () => import("../src/utils/album-filters"));
+vi.mock("i18next", () => ({ t: (key: string) => key }));
+vi.mock("ente-new/photos/components/Tiles", () => ({ ItemCard: () => null }));
+vi.mock("react-virtualized-auto-sizer", () => ({
+ default: ({
+ children,
+ }: {
+ children: (size: { width: number; height: number }) => React.ReactNode;
+ }) => children({ width: 300, height: 400 }),
+}));
+
+const owned: CollectionSummary = {
+ id: 1,
+ name: "Family",
+ type: "album",
+ attributes: new Set(),
+ fileCount: 1,
+ latestFile: undefined,
+ coverFile: undefined,
+ updationTime: undefined,
+ sortPriority: 0,
+};
+const link: CollectionSummary = {
+ ...owned,
+ id: 2,
+ name: "Holiday link",
+ attributes: new Set(["quickLink"]),
+};
+const albums = [owned, link];
+
+const renderSearch = (query: string, albumFilter = "ente") => {
+ const normalizedQuery = query.trim().toLowerCase();
+ const props: GalleryBarImplProps = {
+ mode: "albums",
+ albumFilter: albumFilter as GalleryBarImplProps["albumFilter"],
+ albumSearchQuery: query,
+ collectionSummaries: albums.filter((album) =>
+ album.name.toLowerCase().includes(normalizedQuery),
+ ),
+ visibleAlbumFilters: visibleAlbumFilters(albums, query),
+ normalCollectionSummaries: new Map(),
+ uncategorizedCollectionSummaryID: 99,
+ activeCollectionID: undefined,
+ collectionsSortBy: "updation-time-desc",
+ peopleSortBy: "name-asc",
+ people: [],
+ activePerson: undefined,
+ collapsed: false,
+ mobileDrawerProps: { open: false, onClose: vi.fn() },
+ onAlbumSearchQueryChange: vi.fn(),
+ onShowAlbumOverview: vi.fn(),
+ onShowCollectionSummary: vi.fn(),
+ onSelectCollectionID: vi.fn(),
+ onShowAllAlbums: vi.fn(),
+ onCreateAlbum: vi.fn(),
+ onShowAllPeople: vi.fn(),
+ onChangeCollectionsSortBy: vi.fn(),
+ onChangePeopleSortBy: vi.fn(),
+ onSelectPerson: vi.fn(),
+ };
+ return renderToStaticMarkup(
+ <ThemeProvider theme={photosTheme}>
+ <GalleryBarImpl {...props} />
+ </ThemeProvider>,
+ );
+};
+
+describe("sidebar album search across groups", () => {
+ test("reveals matching Links albums while Ente is selected", () => {
+ const html = renderSearch("holiday");
+ expect(html).toContain('aria-label="links_albums_title"');
+ expect(html).toContain('aria-expanded="true"');
+ expect(html).toContain("Holiday link");
+ expect(html).not.toContain('aria-label="Ente"');
+ expect(html).not.toContain("no-matches.svg");
+ });
+
+ test("reveals matching Ente albums while Links is selected", () => {
+ const html = renderSearch("family", "links");
+ expect(html).toContain('aria-label="Ente"');
+ expect(html).toContain("Family");
+ expect(html).not.toContain('aria-label="links_albums_title"');
+ expect(html).not.toContain("no-matches.svg");
+ });
+
+ test("shows the empty state only when no groups match", () => {
+ const html = renderSearch("missing");
+ expect(html).toContain("no-matches.svg");
+ expect(html).not.toContain('aria-label="filter_albums"');
+ });
+
+ test.each(["", " "])(
+ "restores all group headings for query %j",
+ (query) => {
+ const html = renderSearch(query);
+ expect(html).toContain('aria-label="Ente"');
+ expect(html).toContain('aria-label="links_albums_title"');
+ expect(html).toContain("Family");
+ expect(html).not.toContain("Holiday link");
+ expect(html).not.toContain("no-matches.svg");
+ },
+ );
+});