feat: updating notifications system based on incoming request
This is making the frontend data logic a little complex
This commit is contained in:
@ -20,6 +20,10 @@ type SearchImageStore = {
|
|||||||
images: Accessor<ImageWithRawData[]>;
|
images: Accessor<ImageWithRawData[]>;
|
||||||
|
|
||||||
imagesWithProperties: Accessor<ReturnType<typeof groupPropertiesWithImage>>;
|
imagesWithProperties: Accessor<ReturnType<typeof groupPropertiesWithImage>>;
|
||||||
|
processingImages: Accessor<
|
||||||
|
Awaited<ReturnType<typeof getUserImages>>["ProcessingImages"] | undefined
|
||||||
|
>;
|
||||||
|
|
||||||
onRefetchImages: () => void;
|
onRefetchImages: () => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -70,6 +74,8 @@ export const SearchImageContextProvider: Component<ParentProps> = (props) => {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const processingImages = () => data()?.ProcessingImages ?? [];
|
||||||
|
|
||||||
const imagesWithProperties = createMemo<
|
const imagesWithProperties = createMemo<
|
||||||
ReturnType<typeof groupPropertiesWithImage>
|
ReturnType<typeof groupPropertiesWithImage>
|
||||||
>(() => {
|
>(() => {
|
||||||
@ -86,6 +92,7 @@ export const SearchImageContextProvider: Component<ParentProps> = (props) => {
|
|||||||
value={{
|
value={{
|
||||||
images: imageData,
|
images: imageData,
|
||||||
imagesWithProperties: imagesWithProperties,
|
imagesWithProperties: imagesWithProperties,
|
||||||
|
processingImages,
|
||||||
onRefetchImages: refetch,
|
onRefetchImages: refetch,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
@ -175,7 +175,11 @@ const userProcessingImageValidator = strictObject({
|
|||||||
ImageName: string(),
|
ImageName: string(),
|
||||||
Image: null_(),
|
Image: null_(),
|
||||||
}),
|
}),
|
||||||
Status: union([literal("not-started"), literal("in-progress")]),
|
Status: union([
|
||||||
|
literal("not-started"),
|
||||||
|
literal("in-progress"),
|
||||||
|
literal("complete"),
|
||||||
|
]),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type UserImage = InferOutput<typeof dataTypeValidator>;
|
export type UserImage = InferOutput<typeof dataTypeValidator>;
|
||||||
|
@ -10,11 +10,16 @@ import {
|
|||||||
uuid,
|
uuid,
|
||||||
} from "valibot";
|
} from "valibot";
|
||||||
import { base } from "../network";
|
import { base } from "../network";
|
||||||
import { createContext, onCleanup } from "solid-js";
|
import { createContext, createEffect, onCleanup } from "solid-js";
|
||||||
|
import { useSearchImageContext } from "../contexts/SearchImageContext";
|
||||||
|
|
||||||
const processingImagesValidator = strictObject({
|
const processingImagesValidator = strictObject({
|
||||||
ImageID: pipe(string(), uuid()),
|
ImageID: pipe(string(), uuid()),
|
||||||
Status: union([literal("in-progress"), literal("complete")]),
|
Status: union([
|
||||||
|
literal("not-started"),
|
||||||
|
literal("in-progress"),
|
||||||
|
literal("complete"),
|
||||||
|
]),
|
||||||
});
|
});
|
||||||
|
|
||||||
type NotificationState = {
|
type NotificationState = {
|
||||||
@ -29,6 +34,8 @@ export const Notifications = (onCompleteImage: () => void) => {
|
|||||||
ProcessingImages: {},
|
ProcessingImages: {},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const { processingImages } = useSearchImageContext();
|
||||||
|
|
||||||
const access = localStorage.getItem("access");
|
const access = localStorage.getItem("access");
|
||||||
if (access == null) {
|
if (access == null) {
|
||||||
throw new Error("Access token not defined");
|
throw new Error("Access token not defined");
|
||||||
@ -66,6 +73,26 @@ export const Notifications = (onCompleteImage: () => void) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const upsertImageProcessing = (
|
||||||
|
images: NotificationState["ProcessingImages"],
|
||||||
|
) => {
|
||||||
|
setState("ProcessingImages", (currentImages) => ({
|
||||||
|
...currentImages,
|
||||||
|
...images,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
createEffect(() => {
|
||||||
|
const images = processingImages();
|
||||||
|
if (images == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
upsertImageProcessing(
|
||||||
|
Object.fromEntries(images.map((i) => [i.ImageID, i.Status])),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
const events = new EventSource(`${base}/notifications?token=${access}`);
|
const events = new EventSource(`${base}/notifications?token=${access}`);
|
||||||
|
|
||||||
events.addEventListener("data", dataEventListener);
|
events.addEventListener("data", dataEventListener);
|
||||||
|
Reference in New Issue
Block a user