|
"use client" |
|
|
|
import { useEffect, useState, useTransition } from "react" |
|
|
|
import { cn } from "@/lib/utils" |
|
import { fonts } from "@/lib/fonts" |
|
import { GeneratedPanel } from "@/types" |
|
import { joinWords } from "@/lib/joinWords" |
|
|
|
import { TopMenu } from "./interface/top-menu" |
|
import { useStore } from "./store" |
|
import { Zoom } from "./interface/zoom" |
|
import { BottomBar } from "./interface/bottom-bar" |
|
import { Page } from "./interface/page" |
|
import { getStoryContinuation } from "./queries/getStoryContinuation" |
|
import { useDynamicConfig } from "@/lib/useDynamicConfig" |
|
|
|
export default function Main() { |
|
const [_isPending, startTransition] = useTransition() |
|
|
|
const { config, isConfigReady } = useDynamicConfig() |
|
const isGeneratingStory = useStore(s => s.isGeneratingStory) |
|
const setGeneratingStory = useStore(s => s.setGeneratingStory) |
|
|
|
const font = useStore(s => s.font) |
|
const preset = useStore(s => s.preset) |
|
const prompt = useStore(s => s.prompt) |
|
|
|
const nbPages = useStore(s => s.nbPages) |
|
const nbPanelsPerPage = useStore(s => s.nbPanelsPerPage) |
|
const nbTotalPanels = useStore(s => s.nbTotalPanels) |
|
const setNbPages = useStore(s => s.setNbPages) |
|
const setNbPanelsPerPage = useStore(s => s.setNbPanelsPerPage) |
|
|
|
const setPanels = useStore(s => s.setPanels) |
|
const setCaptions = useStore(s => s.setCaptions) |
|
|
|
const zoomLevel = useStore(s => s.zoomLevel) |
|
|
|
const [waitABitMore, setWaitABitMore] = useState(false) |
|
|
|
useEffect(() => { |
|
if (isConfigReady) { |
|
setNbPages(config.maxNbPages) |
|
setNbPanelsPerPage(config.nbPanelsPerPage) |
|
} |
|
}, [JSON.stringify(config), isConfigReady]) |
|
|
|
useEffect(() => { |
|
if (!prompt) { return } |
|
|
|
startTransition(async () => { |
|
setWaitABitMore(false) |
|
setGeneratingStory(true) |
|
|
|
|
|
const enableRateLimiter = false |
|
|
|
const [stylePrompt, userStoryPrompt] = prompt.split("||").map(x => x.trim()) |
|
|
|
|
|
|
|
let limitedStylePrompt = stylePrompt.trim().slice(0, 77).trim() |
|
if (limitedStylePrompt.length !== stylePrompt.length) { |
|
console.log("Sorry folks, the style prompt was cut to:", limitedStylePrompt) |
|
} |
|
|
|
|
|
const lightPanelPromptPrefix = joinWords(preset.imagePrompt(limitedStylePrompt)) |
|
|
|
|
|
const degradedPanelPromptPrefix = joinWords([ |
|
...preset.imagePrompt(limitedStylePrompt), |
|
|
|
|
|
userStoryPrompt |
|
]) |
|
|
|
let existingPanels: GeneratedPanel[] = [] |
|
const newPanelsPrompts: string[] = [] |
|
const newCaptions: string[] = [] |
|
|
|
|
|
const nbPanelsToGenerate = 2 |
|
|
|
for ( |
|
let currentPanel = 0; |
|
currentPanel < nbTotalPanels; |
|
currentPanel += nbPanelsToGenerate |
|
) { |
|
try { |
|
const candidatePanels = await getStoryContinuation({ |
|
preset, |
|
stylePrompt, |
|
userStoryPrompt, |
|
nbPanelsToGenerate, |
|
nbTotalPanels, |
|
existingPanels, |
|
}) |
|
console.log("LLM generated some new panels:", candidatePanels) |
|
|
|
existingPanels.push(...candidatePanels) |
|
|
|
console.log(`Converting the ${nbPanelsToGenerate} new panels into image prompts..`) |
|
|
|
const startAt = currentPanel |
|
const endAt = currentPanel + nbPanelsToGenerate |
|
for (let p = startAt; p < endAt; p++) { |
|
newCaptions.push(existingPanels[p]?.caption.trim() || "...") |
|
const newPanel = joinWords([ |
|
|
|
|
|
|
|
existingPanels[p]?.instructions |
|
? lightPanelPromptPrefix |
|
: degradedPanelPromptPrefix, |
|
|
|
existingPanels[p]?.instructions |
|
]) |
|
newPanelsPrompts.push(newPanel) |
|
|
|
console.log(`Image prompt for panel ${p} => "${newPanel}"`) |
|
} |
|
|
|
|
|
|
|
setCaptions(newCaptions) |
|
setPanels(newPanelsPrompts) |
|
|
|
setGeneratingStory(false) |
|
} catch (err) { |
|
console.log("failed to generate the story, aborting here") |
|
setGeneratingStory(false) |
|
break |
|
} |
|
if (currentPanel > (nbTotalPanels / 2)) { |
|
console.log("good, we are half way there, hold tight!") |
|
|
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
}, [prompt, preset?.label, nbPages, nbPanelsPerPage, nbTotalPanels]) |
|
|
|
return ( |
|
<div> |
|
<TopMenu /> |
|
<div className={cn( |
|
`flex items-start w-screen h-screen pt-24 md:pt-[72px] overflow-y-scroll`, |
|
`transition-all duration-200 ease-in-out`, |
|
zoomLevel > 105 ? `px-0` : `pl-1 pr-8 md:pl-16 md:pr-16`, |
|
`print:pt-0 print:px-0 print:pl-0 print:pr-0`, |
|
fonts.actionman.className |
|
)}> |
|
<div |
|
className={cn( |
|
`flex flex-col w-full`, |
|
zoomLevel > 105 ? `items-start` : `items-center` |
|
)}> |
|
<div |
|
className={cn( |
|
`comic-page`, |
|
`flex flex-col md:flex-row md:space-x-8 lg:space-x-12 xl:space-x-16 md:items-center md:justify-start`, |
|
`print:space-x-4 print:flex-row`, |
|
)} |
|
style={{ |
|
width: `${zoomLevel}%` |
|
}}> |
|
{Array(nbPages).fill(0).map((_, i) => <Page key={i} page={i} />)} |
|
</div> |
|
</div> |
|
</div> |
|
<Zoom /> |
|
<BottomBar /> |
|
<div className={cn( |
|
`print:hidden`, |
|
`z-20 fixed inset-0`, |
|
`flex flex-row items-center justify-center`, |
|
`transition-all duration-300 ease-in-out`, |
|
isGeneratingStory |
|
? `bg-zinc-50/30 backdrop-blur-md` |
|
: `bg-zinc-50/0 backdrop-blur-none pointer-events-none`, |
|
fonts.actionman.className |
|
)}> |
|
<div className={cn( |
|
`text-center text-xl text-stone-700 w-[70%]`, |
|
isGeneratingStory ? ``: `scale-0 opacity-0`, |
|
`transition-all duration-300 ease-in-out`, |
|
)}> |
|
{waitABitMore ? `Story is ready, but server is a bit busy!`: 'Generating a new story..'}<br/> |
|
{waitABitMore ? `Please hold tight..` : ''} |
|
</div> |
|
</div> |
|
</div> |
|
) |
|
} |