"use client" import { useEffect, useState } from "react" import { useSearchParams } from "next/navigation" import { StaticImageData } from "next/image" import { useLocalStorage } from "usehooks-ts" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Label } from "@/components/ui/label" import { cn } from "@/lib/utils" import { FontName, defaultFont } from "@/lib/fonts" import { Input } from "@/components/ui/input" import { PresetName, defaultPreset, nonRandomPresets, presets } from "@/app/engine/presets" import { useStore } from "@/app/store" import { Button } from "@/components/ui/button" import { LayoutName, defaultLayout, nonRandomLayouts } from "@/app/layouts" import { Switch } from "@/components/ui/switch" import { useOAuth } from "@/lib/useOAuth" import { useIsBusy } from "@/lib/useIsBusy" import { localStorageKeys } from "../settings-dialog/localStorageKeys" import { defaultSettings } from "../settings-dialog/defaultSettings" import { AuthWall } from "../auth-wall" import { SelectLayout } from "../select-layout" import { getLocalStorageShowSpeeches } from "@/lib/getLocalStorageShowSpeeches" export function TopMenu() { const searchParams = useSearchParams() const requestedPreset = (searchParams?.get('preset') as PresetName) || defaultPreset const requestedFont = (searchParams?.get('font') as FontName) || defaultFont const requestedStylePrompt = (searchParams?.get('stylePrompt') as string) || "" const requestedStoryPrompt = (searchParams?.get('storyPrompt') as string) || "" const requestedLayout = (searchParams?.get('layout') as LayoutName) || defaultLayout // const font = useStore(s => s.font) // const setFont = useStore(s => s.setFont) const preset = useStore(s => s.preset) const prompt = useStore(s => s.prompt) const layout = useStore(s => s.layout) const setLayout = useStore(s => s.setLayout) const setShowSpeeches = useStore(s => s.setShowSpeeches) const showSpeeches = useStore(s => s.showSpeeches) const setShowCaptions = useStore(s => s.setShowCaptions) const showCaptions = useStore(s => s.showCaptions) const currentNbPages = useStore(s => s.currentNbPages) const setCurrentNbPages = useStore(s => s.setCurrentNbPages) const generate = useStore(s => s.generate) const isBusy = useIsBusy() const [lastDraftPromptA, setLastDraftPromptA] = useLocalStorage( "AI_COMIC_FACTORY_LAST_DRAFT_PROMPT_A", requestedStylePrompt ) const [lastDraftPromptB, setLastDraftPromptB] = useLocalStorage( "AI_COMIC_FACTORY_LAST_DRAFT_PROMPT_B", requestedStoryPrompt ) // TODO should be in the store const [draftPromptA, setDraftPromptA] = useState(lastDraftPromptA) const [draftPromptB, setDraftPromptB] = useState(lastDraftPromptB) const draftPrompt = `${draftPromptA}||${draftPromptB}` const [draftPreset, setDraftPreset] = useState(requestedPreset) const [draftLayout, setDraftLayout] = useState(requestedLayout) const { isLoggedIn, enableOAuthWall } = useOAuth({ debug: false }) const [hasGeneratedAtLeastOnce, setHasGeneratedAtLeastOnce] = useLocalStorage( localStorageKeys.hasGeneratedAtLeastOnce, defaultSettings.hasGeneratedAtLeastOnce ) const [showAuthWall, setShowAuthWall] = useState(false) // we synchronize the draft prompt with the local storage useEffect(() => { if (lastDraftPromptA !== draftPromptA) { setLastDraftPromptA(draftPromptA) } }, [draftPromptA]) useEffect(() => { if (lastDraftPromptA !== draftPromptA) { setDraftPromptA(lastDraftPromptA) } }, [lastDraftPromptA]) useEffect(() => { if (lastDraftPromptB !== draftPromptB) { setLastDraftPromptB(draftPromptB) } }, [draftPromptB]) useEffect(() => { if (lastDraftPromptB !== draftPromptB) { setDraftPromptB(lastDraftPromptB) } }, [lastDraftPromptB]) // we need a use effect to properly read the local storage useEffect(() => { setShowSpeeches(getLocalStorageShowSpeeches(true)) }, []) const handleSubmit = () => { if (enableOAuthWall && hasGeneratedAtLeastOnce && !isLoggedIn) { setShowAuthWall(true) return } const promptChanged = draftPrompt.trim() !== prompt.trim() const presetChanged = draftPreset !== preset.id const layoutChanged = draftLayout !== layout if (!isBusy && (promptChanged || presetChanged || layoutChanged)) { generate(draftPrompt, draftPreset, draftLayout) } } useEffect(() => { const layoutChanged = draftLayout !== layout if (layoutChanged && !isBusy) { setLayout(draftLayout) } }, [layout, draftLayout, isBusy]) return (
{/* */}
{/* */}
{/*
*/}
{ setDraftPromptB(e.target.value) }} onKeyDown={({ key }) => { if (key === 'Enter') { handleSubmit() } }} value={draftPromptB} /> { setDraftPromptA(e.target.value) }} onKeyDown={({ key }) => { if (key === 'Enter') { handleSubmit() } }} value={draftPromptA} />
{/* Let's add this feature later, because right now people are confused about why they can't activate it
*/}
) }