Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor search to use rtk #376

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33,372 changes: 14,273 additions & 19,099 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import { Statistics } from "./layouts/dataviz/Statistics";
import { SignupPage } from "./layouts/login/SignUpPage";
import { DeletedPhotos } from "./layouts/photos/DeletedPhotos";
import { FavoritePhotos } from "./layouts/photos/FavoritePhotos";
import { OnlyPhotos } from "./layouts/photos/OnlyPhotos";
import { OnlyVideos } from "./layouts/photos/OnlyVideos";
import { HiddenPhotos } from "./layouts/photos/HiddenPhotos";
import { NoTimestampPhotosView } from "./layouts/photos/NoTimestampPhotosView";
import { OnlyPhotos } from "./layouts/photos/OnlyPhotos";
import { OnlyVideos } from "./layouts/photos/OnlyVideos";
import { RecentlyAddedPhotos } from "./layouts/photos/RecentlyAddedPhotos";
import { TimestampPhotos } from "./layouts/photos/TimestampPhotos";
import { PublicUserList } from "./layouts/public/PublicUserList";
Expand Down Expand Up @@ -117,8 +117,8 @@ export function App() {
<Route path="things" element={<AlbumThing />} />
<Route path="recent" element={<RecentlyAddedPhotos />} />
<Route path="favorites" element={<FavoritePhotos />} />
<Route path="photos" element={<OnlyPhotos/>} />
<Route path="videos" element={<OnlyVideos/>} />
<Route path="photos" element={<OnlyPhotos />} />
<Route path="videos" element={<OnlyVideos />} />
<Route path="deleted" element={<DeletedPhotos />} />
<Route path="hidden" element={<HiddenPhotos />} />
<Route path="notimestamp" element={<NoTimestampPhotosView />} />
Expand All @@ -131,7 +131,7 @@ export function App() {
<Route path="profile" element={<Profile />} />
<Route path="library" element={<Library />} />
<Route path="faces" element={<FaceDashboard />} />
<Route path="search" element={<SearchView />} />
<Route path="search/:query" element={<SearchView />} />
<Route path="person/:albumID" element={<AlbumPersonGallery />} />
<Route path="place/:albumID" element={<AlbumPlaceGallery />} />
<Route path="thing/:albumID" element={<AlbumThingGallery />} />
Expand Down
101 changes: 0 additions & 101 deletions src/actions/searchActions.js

This file was deleted.

47 changes: 41 additions & 6 deletions src/api_client/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { z } from "zod";

import { PigPhotoSchema } from "../actions/photosActions.types";
import { getPhotosFlatFromGroupedByDate } from "../util/util";
import { api } from "./api";

const SearchExamplesSchema = z.array(z.string());
Expand All @@ -8,26 +10,59 @@ const SearchExamplesResponseSchema = z.object({
});
type SearchExamples = z.infer<typeof SearchExamplesSchema>;

const PhotosGroupedByDate = z.array(
z.object({
date: z.string(),
location: z.string(),
items: z.array(PigPhotoSchema),
})
);

const SearchPhotosSchema = z.object({
results: PhotosGroupedByDate,
});

const SearchPhotosResultScheme = z.object({
photosFlat: z.array(PigPhotoSchema),
photosGroupedByDate: PhotosGroupedByDate,
});

type SearchPhotosResult = z.infer<typeof SearchPhotosResultScheme>;

enum Endpoints {
fetchSearchExamples = "fetchSearchExamples",
searchExamples = "searchExamples",
searchPhotos = "searchPhotos",
}

const searchApi = api
.injectEndpoints({
endpoints: builder => ({
[Endpoints.fetchSearchExamples]: builder.query<SearchExamples, void>({
[Endpoints.searchExamples]: builder.query<SearchExamples, void>({
query: () => "searchtermexamples/",
transformResponse: response => SearchExamplesResponseSchema.parse(response).results,
}),
[Endpoints.searchPhotos]: builder.query<SearchPhotosResult, string>({
query: query => `photos/searchlist/?search=${query}`,
transformResponse: response => {
const photosGroupedByDate = SearchPhotosSchema.parse(response).results;
return {
photosFlat: getPhotosFlatFromGroupedByDate(photosGroupedByDate),
photosGroupedByDate: photosGroupedByDate,
};
},
}),
}),
})
.enhanceEndpoints<"SearchExamples">({
addTagTypes: ["SearchExamples"],
.enhanceEndpoints<"SearchExamples" | "SearchPhotos">({
addTagTypes: ["SearchExamples", "SearchPhotos"],
endpoints: {
[Endpoints.fetchSearchExamples]: {
[Endpoints.searchExamples]: {
providesTags: ["SearchExamples"],
},
[Endpoints.searchPhotos]: {
providesTags: ["SearchPhotos"],
},
},
});

export const { useFetchSearchExamplesQuery } = searchApi;
export const { useSearchExamplesQuery, useSearchPhotosQuery } = searchApi;
8 changes: 3 additions & 5 deletions src/components/CustomSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import { useTranslation } from "react-i18next";
import { push } from "redux-first-history";
import { Album, Map, Search, Tag, X } from "tabler-icons-react";

import { searchPhotos } from "../actions/searchActions";
import { useFetchPeopleAlbumsQuery } from "../api_client/albums/people";
import { useFetchPlacesAlbumsQuery } from "../api_client/albums/places";
import { useFetchThingsAlbumsQuery } from "../api_client/albums/things";
import { useFetchUserAlbumsQuery } from "../api_client/albums/user";
import { useFetchSearchExamplesQuery } from "../api_client/search";
import { useSearchExamplesQuery } from "../api_client/search";
import { useAppDispatch } from "../store/store";
import { fuzzyMatch } from "../util/util";

Expand Down Expand Up @@ -87,7 +86,7 @@ export function CustomSearch() {
const [searchSuggestions, setSearchSuggestions] = useState<Array<AutocompleteItem>>([]);
const [searchPlaceholder, setSearchPlaceholder] = useState("");
const searchBarWidth = width - width / 2.2;
const { data: searchExamples } = useFetchSearchExamplesQuery();
const { data: searchExamples } = useSearchExamplesQuery();
const { data: placeAlbums } = useFetchPlacesAlbumsQuery();
const { data: thingAlbums } = useFetchThingsAlbumsQuery();
const { data: userAlbums } = useFetchUserAlbumsQuery();
Expand Down Expand Up @@ -137,8 +136,7 @@ export function CustomSearch() {
switch (item.type) {
case undefined:
case SuggestionType.EXAMPLE:
dispatch(searchPhotos(item.value));
dispatch(push("/search"));
dispatch(push(`/search/${item.value}`));
break;
case SuggestionType.USER_ALBUM:
dispatch(push(`/useralbum/${item.id}`));
Expand Down
54 changes: 0 additions & 54 deletions src/components/SimpleSearch.tsx

This file was deleted.

14 changes: 4 additions & 10 deletions src/components/lightbox/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { Edit, Map2, Note, Photo, Tags, UserOff, Users, X } from "tabler-icons-r

import { generatePhotoIm2txtCaption, savePhotoCaption } from "../../actions/photosActions";
import type { Photo as PhotoType } from "../../actions/photosActions.types";
import { searchPhotos } from "../../actions/searchActions";
import { api } from "../../api_client/api";
import { serverAddress } from "../../api_client/apiClient";
import { photoDetailsApi } from "../../api_client/photos/photoDetail";
Expand Down Expand Up @@ -137,14 +136,11 @@ export function Sidebar(props: Props) {
<Title order={4}>{t("lightbox.sidebar.people")}</Title>
</Group>
{photoDetail.people.map(nc => (
<Group position="center" spacing="xs">
<Group position="center" spacing="xs" key={`${nc.name}`}>
<Button
variant="subtle"
leftIcon={<Avatar radius="xl" src={serverAddress + nc.face_url} />}
onClick={() => {
dispatch(searchPhotos(nc.name));
dispatch(push("/search"));
}}
onClick={() => dispatch(push(`/search/${nc.name}`))}
>
<Text align="center" size="sm">
{nc.name}
Expand Down Expand Up @@ -223,8 +219,7 @@ export function Sidebar(props: Props) {
key={`lightbox_attribute_label_${photoDetail.image_hash}_${nc}`}
color="blue"
onClick={() => {
dispatch(searchPhotos(nc));
dispatch(push("/search"));
dispatch(push(`/search/${nc}`));
}}
>
{nc}
Expand All @@ -239,8 +234,7 @@ export function Sidebar(props: Props) {
key={`lightbox_category_label_${photoDetail.image_hash}_${nc}`}
color="teal"
onClick={() => {
dispatch(searchPhotos(nc));
dispatch(push("/search"));
dispatch(push(`/search/${nc}`));
}}
>
{nc}
Expand Down
30 changes: 11 additions & 19 deletions src/layouts/SearchView.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import React from "react";
// only needs to be imported once
import { Navigate } from "react-router-dom";
import { useParams } from "react-router-dom";
import "react-virtualized/styles.css";
import { Search } from "tabler-icons-react";

import { useSearchPhotosQuery } from "../api_client/search";
import { PhotoListView } from "../components/photolist/PhotoListView";
import { PhotosetType } from "../reducers/photosReducer";
import { useAppSelector } from "../store/store";

const DEFAULTS = {
photosFlat: [],
photosGroupedByDate: [],
};

export function SearchView() {
const { query: searchQuery } = useParams();
const user = useAppSelector(state => state.user.userSelfDetails);
const photosGroupedByDate = useAppSelector(state => state.photos.photosGroupedByDate);
const photosFlat = useAppSelector(state => state.photos.photosFlat);
const fetchedPhotosetType = useAppSelector(state => state.photos.fetchedPhotosetType);

const searchQuery = useAppSelector(state => state.search.query);

if (!searchQuery) {
// User hasn't searched for anything. Redirect to root.
return <Navigate to="/" />;
}
// if semantic search is activated we get a flat array sorted by relevance
// thats why we have to change the parameters
const title = `Searching "${searchQuery}"...`;
const { data: { photosGroupedByDate, photosFlat } = DEFAULTS, isFetching } = useSearchPhotosQuery(searchQuery ?? "");

// To-Do: Semantic Search broken, Zod Error
return (
<PhotoListView
title={title}
loading={fetchedPhotosetType !== PhotosetType.SEARCH}
title={`Searching "${searchQuery}"...`}
loading={isFetching}
icon={<Search size={50} />}
photoset={photosGroupedByDate}
idx2hash={user.semantic_search_topk ? photosGroupedByDate : photosFlat}
Expand Down
Loading