|
"use client" |
|
|
|
import { useEffect, useRef, useState, useTransition } from "react" |
|
|
|
|
|
import { RenderedScene } from "@/types" |
|
|
|
import { getRender, newRender } from "@/app/engine/render" |
|
import { useStore } from "@/app/store" |
|
|
|
import { cn } from "@/lib/utils" |
|
import { getInitialRenderedScene } from "@/lib/getInitialRenderedScene" |
|
import { Progress } from "@/app/interface/progress" |
|
import { see } from "@/app/engine/caption" |
|
import { writeIntoBubble } from "@/lib/writeIntoBubble" |
|
|
|
|
|
export function Panel({ |
|
panel, |
|
className = "", |
|
width = 1, |
|
height = 1, |
|
}: { |
|
panel: number |
|
className?: string |
|
width?: number |
|
height?: number |
|
}) { |
|
const font = useStore(state => state.font) |
|
const preset = useStore(state => state.preset) |
|
const setGeneratingImages = useStore(state => state.setGeneratingImages) |
|
|
|
const panels = useStore(state => state.panels) |
|
const prompt = panels[panel] || "" |
|
|
|
const zoomLevel = useStore(state => state.zoomLevel) |
|
|
|
|
|
|
|
|
|
|
|
const [_isPending, startTransition] = useTransition() |
|
const [rendered, setRendered] = useState<RenderedScene>(getInitialRenderedScene()) |
|
const renderedRef = useRef<RenderedScene>() |
|
|
|
const timeoutRef = useRef<any>(null) |
|
|
|
const delay = 3000 + (1000 * panel) |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
if (!prompt?.length) { return } |
|
|
|
|
|
setGeneratingImages(panel, true) |
|
setRendered(getInitialRenderedScene()) |
|
|
|
setTimeout(() => { |
|
startTransition(async () => { |
|
|
|
console.log(`Loading panel ${panel}..`) |
|
|
|
let newRendered: RenderedScene |
|
try { |
|
newRendered = await newRender({ prompt, width, height }) |
|
} catch (err) { |
|
console.log("Failed to load the panel! Don't worry, we are retrying..") |
|
newRendered = await newRender({ prompt, width, height }) |
|
} |
|
|
|
if (newRendered) { |
|
|
|
setRendered(renderedRef.current = newRendered) |
|
|
|
|
|
} else { |
|
setRendered(renderedRef.current = { |
|
renderId: "", |
|
status: "error", |
|
assetUrl: "", |
|
alt: "", |
|
maskUrl: "", |
|
error: "failed to fetch the data", |
|
segments: [] |
|
}) |
|
setGeneratingImages(panel, false) |
|
return |
|
} |
|
}) |
|
}, 2000 * panel) |
|
}, [prompt, width, height]) |
|
|
|
|
|
const checkStatus = () => { |
|
startTransition(async () => { |
|
clearTimeout(timeoutRef.current) |
|
|
|
if (!renderedRef.current?.renderId || renderedRef.current?.status !== "pending") { |
|
timeoutRef.current = setTimeout(checkStatus, delay) |
|
return |
|
} |
|
try { |
|
setGeneratingImages(panel, true) |
|
|
|
const newRendered = await getRender(renderedRef.current.renderId) |
|
|
|
|
|
if (JSON.stringify(renderedRef.current) !== JSON.stringify(newRendered)) { |
|
console.log("updated panel:", newRendered) |
|
setRendered(renderedRef.current = newRendered) |
|
setGeneratingImages(panel, true) |
|
} |
|
|
|
|
|
if (newRendered.status === "pending") { |
|
|
|
timeoutRef.current = setTimeout(checkStatus, delay) |
|
} else if (newRendered.status === "error" || |
|
(newRendered.status === "completed" && !newRendered.assetUrl?.length)) { |
|
console.log(`panel got an error and/or an empty asset url :/ "${newRendered.error}", but let's try to recover..`) |
|
try { |
|
const newAttempt = await newRender({ prompt, width, height }) |
|
setRendered(renderedRef.current = newAttempt) |
|
} catch (err) { |
|
console.error("yeah sorry, something is wrong.. aborting") |
|
setGeneratingImages(panel, false) |
|
} |
|
} else { |
|
console.log("panel finished!") |
|
setGeneratingImages(panel, false) |
|
} |
|
} catch (err) { |
|
console.error(err) |
|
timeoutRef.current = setTimeout(checkStatus, delay) |
|
} |
|
}) |
|
} |
|
|
|
useEffect(() => { |
|
|
|
clearTimeout(timeoutRef.current) |
|
|
|
|
|
timeoutRef.current = setTimeout(checkStatus, delay) |
|
|
|
return () => { |
|
clearTimeout(timeoutRef.current) |
|
} |
|
}, [prompt, width, height]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const frameClassName = cn( |
|
`w-full h-full`, |
|
`border-stone-800`, |
|
`transition-all duration-200 ease-in-out`, |
|
zoomLevel > 140 ? `border-[2px] md:border-[4px] rounded-sm md:rounded-md` : |
|
zoomLevel > 120 ? `border-[1.5px] md:border-[3px] rounded-xs md:rounded-sm` : |
|
zoomLevel > 90 ? `border-[1px] md:border-[2px] rounded-xs md:rounded-sm` : |
|
zoomLevel > 40 ? `border-[0.5px] md:border-[1px] rounded-none md:rounded-xs` : |
|
`border-transparent md:border-[0.5px] rounded-none md:rounded-none`, |
|
`shadow-sm`, |
|
`overflow-hidden`, |
|
`print:border-[1.5px] print:shadow-none`, |
|
) |
|
|
|
const [newMask, setNewMask] = useState("") |
|
|
|
useEffect(() => { |
|
const transformMask = async () => { |
|
if (rendered.maskUrl) { |
|
const imgSrc = await writeIntoBubble( |
|
rendered.maskUrl, |
|
"LOREM IPSUM! Dolor sit amet.." |
|
) |
|
setNewMask(imgSrc) |
|
} |
|
} |
|
transformMask() |
|
}, [rendered.maskUrl]) |
|
|
|
|
|
if (prompt && !rendered.assetUrl) { |
|
return ( |
|
<div className={cn( |
|
frameClassName, |
|
`flex flex-col items-center justify-center`, |
|
className |
|
)}> |
|
<Progress isLoading /> |
|
</div> |
|
) |
|
} |
|
|
|
return ( |
|
<div className={cn( |
|
frameClassName, |
|
{ "grayscale": preset.color === "grayscale" }, |
|
className |
|
)}> |
|
{rendered.assetUrl && <img |
|
src={rendered.assetUrl} |
|
width={width} |
|
height={height} |
|
alt={rendered.alt} |
|
className="h-full max-w-fit print:w-full print:object-cover" |
|
/>} |
|
|
|
|
|
|
|
{/*<Bubble className="absolute top-4 left-4"> |
|
Hello, world! |
|
</Bubble> |
|
*/} |
|
</div> |
|
) |
|
} |