File size: 11,824 Bytes
624088c
 
797cbb8
 
a296341
 
f4dea7d
624088c
 
 
 
 
 
 
 
 
c51d86a
624088c
c51d86a
932a7fd
aa15a35
3b81d2d
a296341
 
3b81d2d
932a7fd
b48537f
 
 
3b81d2d
2e12a66
932a7fd
797cbb8
b022cb9
 
 
 
 
 
 
 
3b81d2d
 
 
 
 
 
c32ec0d
95a4e14
 
 
3b81d2d
 
2f9a87c
3b81d2d
 
932a7fd
3b81d2d
f6f68f6
3b81d2d
f6f68f6
 
 
b022cb9
f6f68f6
 
 
 
b022cb9
f6f68f6
 
2e12a66
421fbba
f6f68f6
 
690ffd8
 
797cbb8
 
b48537f
a296341
b48537f
 
 
 
 
 
 
797cbb8
f6f68f6
 
 
 
 
 
2e12a66
 
 
 
 
797cbb8
a296341
b48537f
 
 
2f9a87c
797cbb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624088c
 
a40bf40
624088c
8c5d17c
624088c
8c5d17c
624088c
1c1e6e9
 
8c5d17c
624088c
ec121fd
 
8c5d17c
1c1e6e9
ec121fd
8c5d17c
797cbb8
ec121fd
797cbb8
8c5d17c
 
797cbb8
aa15a35
8c5d17c
1c1e6e9
ec121fd
8c5d17c
 
c51d86a
 
8c5d17c
 
 
 
797cbb8
 
1c1e6e9
ec121fd
797cbb8
 
ec121fd
797cbb8
3b81d2d
797cbb8
3b81d2d
797cbb8
3b81d2d
 
797cbb8
ec121fd
 
 
 
 
1c1e6e9
2e12a66
 
ec121fd
 
95a4e14
 
 
 
2e12a66
95a4e14
 
2e12a66
 
95a4e14
 
797cbb8
8c5d17c
 
1c1e6e9
8c5d17c
ec121fd
8c5d17c
 
 
 
 
 
ec121fd
8c5d17c
 
 
 
 
 
 
 
 
 
 
 
 
797cbb8
624088c
8c5d17c
 
1c1e6e9
8c5d17c
690ffd8
ec121fd
690ffd8
421fbba
b48537f
1c1e6e9
 
 
 
 
690ffd8
 
80f0eea
690ffd8
 
 
 
 
 
80f0eea
690ffd8
 
421fbba
8a579f9
1c1e6e9
 
 
 
 
690ffd8
 
80f0eea
690ffd8
 
 
 
 
 
80f0eea
690ffd8
 
b48537f
 
 
 
1c1e6e9
b48537f
 
 
 
 
 
 
 
 
 
 
ec121fd
624088c
797cbb8
 
 
8c5d17c
 
1c1e6e9
8c5d17c
ec121fd
624088c
f4dea7d
932a7fd
aa15a35
29e7afb
932a7fd
8c5d17c
ec121fd
624088c
 
 
 
 
 
 
 
 
 
 
 
 
797cbb8
624088c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"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<string>(
    "AI_COMIC_FACTORY_LAST_DRAFT_PROMPT_A",
    requestedStylePrompt
  )

  const [lastDraftPromptB, setLastDraftPromptB] = useLocalStorage<string>(
    "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<PresetName>(requestedPreset)
  const [draftLayout, setDraftLayout] = useState<LayoutName>(requestedLayout)
  
  const { isLoggedIn, enableOAuthWall } = useOAuth({ debug: false })
  
  const [hasGeneratedAtLeastOnce, setHasGeneratedAtLeastOnce] = useLocalStorage<boolean>(
    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 (
    <div className={cn(
      `print:hidden`,
      `z-10 fixed top-0 left-0 right-0`,
      `flex flex-col md:flex-row w-full justify-between items-center`,
      `backdrop-blur-xl`,
      `transition-all duration-200 ease-in-out`,
      `px-2 py-2 border-b-1 border-gray-50 dark:border-gray-50`,
      //`bg-[#2d435c] dark:bg-[#2d435c] text-gray-50 dark:text-gray-50`,
      `bg-gradient-to-r from-[#102c4c] to-[#1a426f] dark:bg-gradient-to-r dark:from-[#102c4c] dark:to-[#1a426f]`,
      `space-y-2 md:space-y-0 md:space-x-3 lg:space-x-6`
    )}>
      <div className="flex flex-row space-x-2 md:space-x-3 w-full md:w-auto">
        <div className={cn(
          `transition-all duration-200 ease-in-out`,
          `flex flex-row items-center justify-start space-x-3`,
          `flex-grow`
          )}>

          {/* <Label className="flex text-2xs md:text-sm md:w-24">Style:</Label> */}

          <Select
            defaultValue={defaultPreset}
            onValueChange={(value) => { setDraftPreset(value as PresetName) }}
            disabled={isBusy}
            >
            <SelectTrigger className="flex-grow bg-gray-100 text-gray-700 dark:bg-gray-100 dark:text-gray-700">
              <SelectValue className="text-2xs md:text-sm" placeholder="Style" />
            </SelectTrigger>
            <SelectContent>
              {nonRandomPresets.map(key =>
                <SelectItem key={key} value={key}>{presets[key].label}</SelectItem>
              )}
            </SelectContent>
          </Select>
        </div>
        <div className={cn(
          `transition-all duration-200 ease-in-out`,
          `flex flex-row items-center justify-start space-x-3`,
          `w-40`
          )}>

          {/* <Label className="flex text-2xs md:text-sm md:w-24">Style:</Label> */}

          <SelectLayout
            defaultValue={defaultLayout}
            onLayoutChange={setDraftLayout}
            disabled={isBusy}
            layouts={nonRandomLayouts}
          />
        </div>
        <div className="flex flex-row items-center space-x-3">
        <Switch
          checked={showCaptions}
          onCheckedChange={setShowCaptions}
        />
        <Label className="text-gray-200 dark:text-gray-200">
          <span className="hidden lg:inline">πŸ“–&nbsp;Captions</span>
          <span className="inline lg:hidden">πŸ“–</span>
        </Label>
        </div>
        <div className="flex flex-row items-center space-x-3">
        <Switch
          checked={showSpeeches}
          onCheckedChange={setShowSpeeches}
          defaultChecked={showSpeeches}
        />
        <Label className="text-gray-200 dark:text-gray-200">
          <span className="hidden lg:inline">πŸ’¬&nbsp;Bubbles</span>
          <span className="inline lg:hidden">πŸ’¬</span>
        </Label>
        </div>
        {/*
        <div className={cn(
          `transition-all duration-200 ease-in-out`,
          `flex flex-row items-center space-x-3 w-1/2 md:w-auto md:hidden`
        )}>
          <Label className="flex text-2xs md:text-sm md:w-24">Font:</Label>
          <Select
            defaultValue={fontList.includes(preset.font) ? preset.font : "cartoonist"}
            onValueChange={(value) => { setFont(value as FontName) }}
            disabled={atLeastOnePanelIsBusy}
            >
            <SelectTrigger className="flex-grow">
              <SelectValue className="text-2xs md:text-sm" placeholder="Type" />
            </SelectTrigger>
            <SelectContent>
              {Object.keys(fonts)
                .map((font) =>
                <SelectItem
                  key={font}
                  value={font}>{
                    font
                  }</SelectItem>
              )}
            </SelectContent>
          </Select>
        </div>
          */}
      </div>
      <div className={cn(
          `transition-all duration-200 ease-in-out`,
          `flex  flex-grow flex-col space-y-2 md:space-y-0 md:flex-row items-center md:space-x-3 w-full md:w-auto`
        )}>
        <div className="flex flex-row flex-grow w-full">
          <div className="flex flex-row flex-grow w-full">
            <Input
              id="top-menu-input-story-prompt"
              placeholder="1. Story (eg. detective dog)"
              className={cn(
                `w-1/2 rounded-r-none`,
                `bg-gray-100 text-gray-700 dark:bg-gray-100 dark:text-gray-700`,
                `border-r-stone-100`
              )}
              // disabled={atLeastOnePanelIsBusy}
              onChange={(e) => {
                setDraftPromptB(e.target.value)
              }}
              onKeyDown={({ key }) => {
                if (key === 'Enter') {
                  handleSubmit()
                }
              }}
              value={draftPromptB}
            />
            <Input
              id="top-menu-input-style-prompt"
              placeholder="2. Style (eg 'rain, shiba')"
              className={cn(
                `w-1/2`,
                `bg-gray-100 text-gray-700 dark:bg-gray-100 dark:text-gray-700`,
                `border-l-gray-300 rounded-l-none rounded-r-none`
              )}
              // disabled={atLeastOnePanelIsBusy}
              onChange={(e) => {
                setDraftPromptA(e.target.value)
              }}
              onKeyDown={({ key }) => {
                if (key === 'Enter') {
                  handleSubmit()
                }
              }}
              value={draftPromptA}
            />
          </div>
            <Button
            className={cn(
              `rounded-l-none cursor-pointer`,
              `transition-all duration-200 ease-in-out`,
              `text-xl`,
              `bg-[rgb(59,134,247)] hover:bg-[rgb(69,144,255)] disabled:bg-[rgb(59,134,247)]`
              )}
            onClick={() => {
              handleSubmit()
            }}
            disabled={!draftPrompt?.trim().length || isBusy}
          >
            Go
          </Button>

          <AuthWall show={showAuthWall} />
        </div>
      </div>
      {/*
        Let's add this feature later, because right now people
        are confused about why they can't activate it
      <div className={cn(
          `transition-all duration-200 ease-in-out`,
          `hidden md:flex flex-row items-center space-x-3 w-full md:w-auto`
      )}>
        <Label className="flex text-2xs md:text-sm w-24">Font:</Label>
        <Select
          defaultValue={fontList.includes(preset.font) ? preset.font : "actionman"}
          onValueChange={(value) => { setFont(value as FontName) }}
          // disabled={isBusy}
          disabled={true}
          >
          <SelectTrigger className="flex-grow">
            <SelectValue className="text-2xs md:text-sm" placeholder="Type" />
          </SelectTrigger>
          <SelectContent>
            {Object.keys(fonts)
              .map((font) =>
              <SelectItem
                key={font}
                value={font}>{
                  font
                }</SelectItem>
            )}
          </SelectContent>
        </Select>
      </div>
              */}
    </div>
  )
}