backsplash-app
Version:
An AI powered wallpaper app.
62 lines (54 loc) • 2.59 kB
text/typescript
import { useEffect } from "react";
import { WallpaperUpdates } from "@/ipc/channels/wallpaperChannels";
import { WallpaperMetadata, WallpaperData } from "@/types/wallpaper"; // Added WallpaperData
import { useWallpaperHistory } from "./useWallpaperHistory";
interface WallpaperUpdateEventPayload {
objectKey: string;
upscaledImageUrl: string | null; // Can be null if not yet upscaled
originalImageUrl: string;
style: string;
metadata?: WallpaperMetadata;
setAsBackground?: boolean; // Indicates if this update also set it as desktop background
}
/**
* This hook listens for general wallpaper updates pushed from the main process
* (e.g., when a wallpaper is set by WallpaperService) and updates relevant stores.
*/
export function useWallpaperUpdates() {
const { addToHistory } = useWallpaperHistory();
useEffect(() => {
const handleWallpaperUpdate = (_event: any, update: WallpaperUpdateEventPayload) => {
console.log("[useWallpaperUpdates] Wallpaper update event received:", update);
if (update.objectKey && update.originalImageUrl) {
// Construct WallpaperData to save
const wallpaperToSave: WallpaperData = {
objectKey: update.objectKey,
upscaledImageUrl: update.upscaledImageUrl,
originalImageUrl: update.originalImageUrl,
style: update.style || "Unknown",
generatedAt: update.metadata?.created_at || new Date().toISOString(), // Use created_at directly or current ISO string
metadata: update.metadata,
};
// If this update indicated it was set as the background, add to history.
if (update.setAsBackground) {
// addToHistory from useWallpaperHistory now directly calls IPC to add to store
addToHistory({
key: wallpaperToSave.objectKey,
upscaledImageUrl: wallpaperToSave.upscaledImageUrl,
originalImageUrl: wallpaperToSave.originalImageUrl,
setAt: new Date().toISOString(),
metadata: wallpaperToSave.metadata,
isUpscaled: !!wallpaperToSave.upscaledImageUrl, // Simple check if upscaled URL exists
});
// fetchHistory(); // History updates automatically via its own listener now
}
}
};
// Listen for wallpaper updates from the main process
window.electron.ipcRenderer.on(WallpaperUpdates.UPDATE, handleWallpaperUpdate);
// Cleanup listener on unmount
return () => {
window.electron.ipcRenderer.removeListener(WallpaperUpdates.UPDATE, handleWallpaperUpdate);
};
}, [addToHistory]);
}