mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-15 15:45:27 +03:00
revalidation and mutation
This commit is contained in:
parent
265d68752e
commit
b9d3ad126b
@ -45,6 +45,8 @@ import {
|
|||||||
import { ReviewSegment } from "@/types/review";
|
import { ReviewSegment } from "@/types/review";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import Chip from "@/components/indicators/Chip";
|
import Chip from "@/components/indicators/Chip";
|
||||||
|
import { capitalizeFirstLetter } from "@/utils/stringUtil";
|
||||||
|
import useGlobalMutation from "@/hooks/use-global-mutate";
|
||||||
|
|
||||||
const SEARCH_TABS = [
|
const SEARCH_TABS = [
|
||||||
"details",
|
"details",
|
||||||
@ -232,6 +234,10 @@ function ObjectDetailsTab({
|
|||||||
}: ObjectDetailsTabProps) {
|
}: ObjectDetailsTabProps) {
|
||||||
const apiHost = useApiHost();
|
const apiHost = useApiHost();
|
||||||
|
|
||||||
|
// mutation / revalidation
|
||||||
|
|
||||||
|
const mutate = useGlobalMutation();
|
||||||
|
|
||||||
// data
|
// data
|
||||||
|
|
||||||
const [desc, setDesc] = useState(search?.data.description);
|
const [desc, setDesc] = useState(search?.data.description);
|
||||||
@ -282,6 +288,13 @@ function ObjectDetailsTab({
|
|||||||
position: "top-center",
|
position: "top-center",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
mutate(
|
||||||
|
(key) =>
|
||||||
|
typeof key === "string" &&
|
||||||
|
(key.includes("events") ||
|
||||||
|
key.includes("events/search") ||
|
||||||
|
key.includes("explore")),
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
toast.error("Failed to update the description", {
|
toast.error("Failed to update the description", {
|
||||||
@ -289,7 +302,7 @@ function ObjectDetailsTab({
|
|||||||
});
|
});
|
||||||
setDesc(search.data.description);
|
setDesc(search.data.description);
|
||||||
});
|
});
|
||||||
}, [desc, search]);
|
}, [desc, search, mutate]);
|
||||||
|
|
||||||
const regenerateDescription = useCallback(() => {
|
const regenerateDescription = useCallback(() => {
|
||||||
if (!search) {
|
if (!search) {
|
||||||
@ -300,18 +313,24 @@ function ObjectDetailsTab({
|
|||||||
.put(`events/${search.id}/description/regenerate`)
|
.put(`events/${search.id}/description/regenerate`)
|
||||||
.then((resp) => {
|
.then((resp) => {
|
||||||
if (resp.status == 200) {
|
if (resp.status == 200) {
|
||||||
toast.success("Description regeneration requested.", {
|
toast.success(
|
||||||
position: "top-center",
|
`A new description has been requested from ${capitalizeFirstLetter(config?.genai.provider ?? "Generative AI")}. Depending on the speed of your provider, the new description may take some time to regenerate.`,
|
||||||
});
|
{
|
||||||
|
position: "top-center",
|
||||||
|
duration: 7000,
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
toast.error("Failed to call generative AI for a new description", {
|
toast.error(
|
||||||
position: "top-center",
|
`Failed to call ${capitalizeFirstLetter(config?.genai.provider ?? "Generative AI")} for a new description`,
|
||||||
});
|
{
|
||||||
setDesc(search.data.description);
|
position: "top-center",
|
||||||
|
},
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}, [search]);
|
}, [search, config]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-5">
|
<div className="flex flex-col gap-5">
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import { useEventUpdate } from "@/api/ws";
|
||||||
import { useApiFilterArgs } from "@/hooks/use-api-filter";
|
import { useApiFilterArgs } from "@/hooks/use-api-filter";
|
||||||
import { SearchFilter, SearchQuery, SearchResult } from "@/types/search";
|
import { SearchFilter, SearchQuery, SearchResult } from "@/types/search";
|
||||||
import SearchView from "@/views/search/SearchView";
|
import SearchView from "@/views/search/SearchView";
|
||||||
@ -123,19 +124,19 @@ export default function Explore() {
|
|||||||
return [url, { ...params, limit: API_LIMIT }];
|
return [url, { ...params, limit: API_LIMIT }];
|
||||||
};
|
};
|
||||||
|
|
||||||
const { data, size, setSize, isValidating } = useSWRInfinite<SearchResult[]>(
|
const { data, size, setSize, isValidating, mutate } = useSWRInfinite<
|
||||||
getKey,
|
SearchResult[]
|
||||||
{
|
>(getKey, {
|
||||||
revalidateFirstPage: true,
|
revalidateFirstPage: true,
|
||||||
revalidateAll: false,
|
revalidateOnFocus: true,
|
||||||
onLoadingSlow: () => {
|
revalidateAll: false,
|
||||||
if (!similaritySearch) {
|
onLoadingSlow: () => {
|
||||||
setIsSlowLoading(true);
|
if (!similaritySearch) {
|
||||||
}
|
setIsSlowLoading(true);
|
||||||
},
|
}
|
||||||
loadingTimeout: 10000,
|
|
||||||
},
|
},
|
||||||
);
|
loadingTimeout: 10000,
|
||||||
|
});
|
||||||
|
|
||||||
const searchResults = useMemo(
|
const searchResults = useMemo(
|
||||||
() => (data ? ([] as SearchResult[]).concat(...data) : []),
|
() => (data ? ([] as SearchResult[]).concat(...data) : []),
|
||||||
@ -164,6 +165,16 @@ export default function Explore() {
|
|||||||
}
|
}
|
||||||
}, [isReachingEnd, isLoadingMore, setSize, size, searchResults, searchQuery]);
|
}, [isReachingEnd, isLoadingMore, setSize, size, searchResults, searchQuery]);
|
||||||
|
|
||||||
|
// mutation and revalidation
|
||||||
|
|
||||||
|
const eventUpdate = useEventUpdate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
mutate();
|
||||||
|
// mutate / revalidate when event description updates come in
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [eventUpdate]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{isSlowLoading && !similaritySearch ? (
|
{isSlowLoading && !similaritySearch ? (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user