File size: 6,743 Bytes
b48537f
421fbba
b48537f
 
 
 
 
 
 
 
 
4fafd8c
b48537f
 
 
 
c20780a
421fbba
b48537f
 
2f9a87c
c20780a
 
 
2f9a87c
c20780a
 
 
2f9a87c
c20780a
 
 
b48537f
 
 
 
421fbba
 
c20780a
 
 
 
b48537f
 
421fbba
c20780a
 
b48537f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421fbba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b48537f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fafd8c
b48537f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
421fbba
 
 
 
c20780a
 
421fbba
c20780a
421fbba
c20780a
b48537f
 
 
 
 
421fbba
b48537f
 
421fbba
b48537f
 
 
 
 
 
 
 
 
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
import { startTransition, useEffect, useState } from "react"
import { useFilePicker } from 'use-file-picker'

import { useStore } from "@/app/store"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { upscaleImage } from "@/app/engine/render"
import { sleep } from "@/lib/sleep"

import { Share } from "../share"
import { About } from "../about"
import { Discord } from "../discord"
import { SettingsDialog } from "../settings-dialog"
import { useLocalStorage } from "usehooks-ts"
import { localStorageKeys } from "../settings-dialog/localStorageKeys"
import { defaultSettings } from "../settings-dialog/defaultSettings"
import { getParam } from "@/lib/getParam"
import { Input } from "@/components/ui/input"

function BottomBar() {
  // deprecated, as HTML-to-bitmap didn't work that well for us
  // const page = useStore(s => s.page)
  // const download = useStore(s => s.download)
  // const pageToImage = useStore(s => s.pageToImage)

  const isGeneratingStory = useStore(s => s.isGeneratingStory)
  const prompt = useStore(s => s.prompt)
  const panelGenerationStatus = useStore(s => s.panelGenerationStatus)

  const preset = useStore(s => s.preset)
  
  const canSeeBetaFeatures = getParam<boolean>("beta", false)

  const allStatus = Object.values(panelGenerationStatus)
  const remainingImages = allStatus.reduce((acc, s) => (acc + (s ? 1 : 0)), 0)

  const currentClap = useStore(s => s.currentClap)
  
  const upscaleQueue = useStore(s => s.upscaleQueue)
  const renderedScenes = useStore(s => s.renderedScenes)
  const removeFromUpscaleQueue = useStore(s => s.removeFromUpscaleQueue)
  const setRendered = useStore(s => s.setRendered)
  const [isUpscaling, setUpscaling] = useState(false)

  const loadClap = useStore(s => s.loadClap)
  const downloadClap = useStore(s => s.downloadClap)

  const [hasGeneratedAtLeastOnce, setHasGeneratedAtLeastOnce] = useLocalStorage<boolean>(
    localStorageKeys.hasGeneratedAtLeastOnce,
    defaultSettings.hasGeneratedAtLeastOnce
  )

  const handleUpscale = () => {
    setUpscaling(true)
    startTransition(() => {
      const fn = async () => {
        for (let [panelId, renderedScene] of Object.entries(upscaleQueue)) {
          try {
            console.log(`upscaling panel ${panelId} (${renderedScene.renderId})`)
            const result = await upscaleImage(renderedScene.assetUrl)
            await sleep(1000)
            if (result.assetUrl) {
              console.log(`upscale successful, removing ${panelId} (${renderedScene.renderId}) from upscale queue`)
              setRendered(panelId, {
                ...renderedScene,
                assetUrl: result.assetUrl
              })
              removeFromUpscaleQueue(panelId)
            }

          } catch (err) {
            console.error(`failed to upscale: ${err}`)
          }
        }
        
        setUpscaling(false)
      }

      fn()
    })
  }

  const handlePrint = () => {
    window.print()
  }
  const hasFinishedGeneratingImages = allStatus.length > 0 && (allStatus.length - remainingImages) === allStatus.length

  // keep track of the first generation, independently of the login status
  useEffect(() => {
    if (hasFinishedGeneratingImages && !hasGeneratedAtLeastOnce) {
      setHasGeneratedAtLeastOnce(true)
    }
  }, [hasFinishedGeneratingImages, hasGeneratedAtLeastOnce])

  const { openFilePicker, filesContent } = useFilePicker({
    accept: '.clap',
    readAs: "ArrayBuffer"
  })
  const fileData = filesContent[0]

  useEffect(() => {
    const fn = async () => {
      if (fileData?.name) {
        try {
          const blob = new Blob([fileData.content])
          await loadClap(blob)
        } catch (err) {
          console.error("failed to load the Clap file:", err)
        }
      }
    }
    fn()
  }, [fileData?.name])


  return (
    <div className={cn(
      `print:hidden`,
      `fixed bottom-2 md:bottom-4 left-2 right-0 md:left-3 md:right-1`,
      `flex flex-row`,
      `justify-between`,
      `pointer-events-none`
    )}>
      <div className={cn(
        `flex flex-row`,
        `items-end`,
        `pointer-events-auto`,
        `animation-all duration-300 ease-in-out`,
        isGeneratingStory ? `scale-0 opacity-0` : ``,
        `space-x-3`,
        `scale-[0.9]`
      )}>
        <About />
        <Discord />
       {/* 
       Thank you clip factory for your service 🫑
       <AIClipFactory />
       */}
      </div>
      <div className={cn(
      `flex flex-row`,
      `pointer-events-auto`,
      `animation-all duration-300 ease-in-out`,
      isGeneratingStory ? `scale-0 opacity-0` : ``,
      `space-x-3`,
      `scale-[0.9]`
    )}>
      <SettingsDialog />
      {/*<Button
        onClick={handleUpscale}
        disabled={!prompt?.length || remainingImages > 0 || isUpscaling || !Object.values(upscaleQueue).length}
      >
        {isUpscaling
            ? `${allStatus.length - Object.values(upscaleQueue).length}/${allStatus.length} βŒ›`
            : "Upscale"}
        </Button>*/}

        {/*
        <div>
          <Button
            onClick={handlePrint}
            disabled={!prompt?.length}
          >
            Print
          </Button>
        </div>
        <div>
          <Button
            onClick={download}
            disabled={!prompt?.length}
          >
            <span className="hidden md:inline">{
            remainingImages ? `${allStatus.length - remainingImages}/${allStatus.length} panels βŒ›` : `Save`
            }</span>
            <span className="inline md:hidden">{
              remainingImages ? `${allStatus.length - remainingImages}/${allStatus.length} βŒ›` : `Save`
            }</span>
           </Button>
        </div>
          */}
          {canSeeBetaFeatures ? <Button
            onClick={openFilePicker}
            disabled={remainingImages > 0}
          >Load</Button> : null}
          {canSeeBetaFeatures ? <Button
            onClick={downloadClap}
            disabled={!prompt?.length || remainingImages > 0 || !currentClap}
          >
          {remainingImages ? `${allStatus.length - remainingImages}/${allStatus.length} βŒ›` : `Save`}
        </Button> : null}
          <Button
            onClick={handlePrint}
            disabled={!prompt?.length}
          >
            <span className="hidden md:inline">{
            remainingImages ? `${allStatus.length - remainingImages}/${allStatus.length} panels βŒ›` : `Get PDF`
            }</span>
            <span className="inline md:hidden">{
              remainingImages ? `${allStatus.length - remainingImages}/${allStatus.length} βŒ›` : `PDF`
            }</span>
        </Button>
        <Share />
      </div>
    </div>
  )
}

export default BottomBar