File size: 17,610 Bytes
f27679f
 
bde82a3
f27679f
a40111f
2156c54
a40111f
 
3a944ef
22d3c77
ac7030c
1f122c3
 
f42b4a1
8f2b05f
f42b4a1
 
80ea539
3d4392e
f42b4a1
 
 
8f2b05f
 
f42b4a1
 
38d787b
b3c7e0f
38d787b
 
3d4392e
b6516e1
f42b4a1
 
8f2b05f
f42b4a1
f70dd7e
38d787b
 
 
 
 
ac7030c
bde82a3
 
 
 
16891a6
38d787b
bde82a3
38d787b
 
16891a6
38d787b
16891a6
38d787b
 
 
 
 
 
 
bde82a3
3d4392e
1f122c3
ac7030c
9cea1bb
a40111f
 
ac7030c
3d4392e
80ea539
38d787b
 
 
 
ac7030c
2156c54
9cea1bb
 
 
 
 
ac7030c
 
 
 
 
9cea1bb
ac7030c
9cea1bb
 
 
ac7030c
9cea1bb
 
ac7030c
9cea1bb
a40111f
 
 
 
 
 
 
80ea539
 
 
38d787b
 
80ea539
 
 
f70dd7e
 
ac7030c
b161bd3
 
 
09a7c47
 
 
 
 
 
 
 
 
 
f70dd7e
 
ac7030c
f70dd7e
38d787b
 
 
ac7030c
38d787b
 
ac7030c
38d787b
 
 
ac7030c
38d787b
 
 
 
 
 
e70dbb1
 
 
 
 
 
 
 
 
ac7030c
9cea1bb
38d787b
 
 
ac7030c
38d787b
 
 
ac7030c
38d787b
 
 
 
 
 
 
 
 
 
 
1f122c3
 
f27679f
38d787b
1f122c3
f27679f
 
 
d160b97
38d787b
f27679f
ac7030c
 
 
38d787b
bde82a3
 
 
 
ac7030c
f27679f
 
ac7030c
f27679f
38d787b
d160b97
 
 
f27679f
ac7030c
22d3c77
 
 
 
 
 
 
 
 
ac7030c
4c34e70
22d3c77
f27679f
 
ac7030c
f27679f
d160b97
 
 
a40111f
38d787b
f27679f
d160b97
f27679f
a40111f
 
f27679f
a40111f
d160b97
 
 
 
 
 
ac7030c
d160b97
a40111f
80ea539
 
 
 
 
 
 
 
 
 
ac7030c
e4d3d8a
 
 
 
 
a40111f
d160b97
f27679f
a40111f
d160b97
 
 
 
 
ac7030c
d160b97
a40111f
 
d160b97
 
a40111f
ac7030c
 
a40111f
 
 
 
 
139ef57
 
 
 
a40111f
 
d160b97
 
 
a40111f
 
d160b97
a40111f
 
 
 
 
8f2b05f
ac7030c
8f2b05f
a40111f
f27679f
a40111f
 
 
 
ac7030c
a40111f
38d787b
 
 
 
3a944ef
a40111f
38d787b
 
a40111f
 
38d787b
 
 
a40111f
 
f27679f
3a944ef
22d3c77
 
ac7030c
22d3c77
ac7030c
22d3c77
 
 
 
38d787b
 
ac7030c
38d787b
 
ac7030c
38d787b
22d3c77
 
2156c54
ac7030c
2156c54
 
 
 
 
 
 
3a944ef
 
 
ac7030c
3a944ef
ac7030c
3a944ef
ac7030c
3a944ef
 
 
38d787b
 
 
 
3a944ef
8f2b05f
ac7030c
8f2b05f
f27679f
 
 
 
ac7030c
a40111f
 
d160b97
a40111f
 
f70dd7e
a40111f
38d787b
 
f70dd7e
ac7030c
 
f70dd7e
ac7030c
a40111f
38d787b
 
 
 
 
 
 
 
 
 
 
 
 
16891a6
38d787b
 
 
 
 
 
 
 
 
 
 
 
 
 
16891a6
38d787b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
664b6fe
38d787b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f27679f
38d787b
f27679f
38d787b
 
 
 
 
 
 
d160b97
f27679f
ac7030c
f27679f
1f122c3
 
f27679f
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
"use client"

import { useEffect, useRef, useState, useTransition } from "react"
import { RiCheckboxCircleFill } from "react-icons/ri"
import { PiShareFatLight } from "react-icons/pi"
import { BsHeadsetVr } from "react-icons/bs"
import CopyToClipboard from "react-copy-to-clipboard"
import { LuCopyCheck } from "react-icons/lu"
import { LuScrollText } from "react-icons/lu"
import { BiCameraMovie } from "react-icons/bi"
import { useLocalStorage } from "usehooks-ts"

import { useStore } from "@/app/state/useStore"
import { cn } from "@/lib/utils/cn"

import { ActionButton, actionButtonClassName } from "@/components/interface/action-button"
import { RecommendedVideos } from "@/components/interface/recommended-videos"
import { isCertifiedUser } from "@/app/certification"
import { countNewMediaView } from "@/app/api/actions/stats"
import { formatTimeAgo } from "@/lib/formatters/formatTimeAgo"
import { DefaultAvatar } from "@/components/interface/default-avatar"
import { LikeButton } from "@/components/interface/like-button"

import { ReportModal } from "../report-modal"
import { formatLargeNumber } from "@/lib/formatters/formatLargeNumber"
import { CommentList } from "@/components/interface/comment-list"
import { Input } from "@/components/ui/input"

import { localStorageKeys } from "@/app/state/localStorageKeys"
import { defaultSettings } from "@/app/state/defaultSettings"
import { getComments, submitComment } from "@/app/api/actions/comments"
import { useCurrentUser } from "@/app/state/useCurrentUser"
import { parseMediaProjectionType } from "@/lib/utils/parseMediaProjectionType"
import { MediaPlayer } from "@/components/interface/media-player"

export function PublicMediaView() {
  const [_pending, startTransition] = useTransition()

  const [commentDraft, setCommentDraft] = useState("")
  const [isCommenting, setCommenting] = useState(false)
  const [isFocusedOnInput, setFocusedOnInput] = useState(false)

  // current time in the media
  // note: this is used to *set* the current time, not to read it
  // EDIT: you know what, let's do this the dirty way for now
  // const [desiredCurrentTime, setDesiredCurrentTime] = useState()

  const { user } = useCurrentUser()

  const [userThumbnail, setUserThumbnail] = useState("")
  
  useEffect(() => {
    setUserThumbnail(user?.thumbnail || "")
  
  }, [user?.thumbnail])

  const handleBadUserThumbnail = () => {
    if (userThumbnail) {
      setUserThumbnail("")
    }
  }


  const media = useStore(s => s.publicMedia)

  const mediaId = `${media?.id || ""}`

  const [copied, setCopied] = useState<boolean>(false)

  const [channelThumbnail, setChannelThumbnail] = useState(`${media?.channel.thumbnail || ""}`)
  const setPublicMedia = useStore(s => s.setPublicMedia)

  const publicComments = useStore(s => s.publicComments)

  const setPublicComments = useStore(s => s.setPublicComments)

  const isEquirectangular = parseMediaProjectionType(media) === "equirectangular"
  
  // we inject the current videoId in the URL, if it's not already present
  // this is a hack for Hugging Face iframes
  useEffect(() => {
    const queryString = new URL(location.href).search
    const searchParams = new URLSearchParams(queryString)
    if (mediaId) {
      // TODO: the "v" parameter is legacy, it made sense in the past when
      // AiTube used to only support traditional videos
      if (searchParams.get("v") !== mediaId || searchParams.get("m") !== mediaId) {
        console.log(`current mediaId "${mediaId}" isn't set in the URL query params.. TODO we should set it`)
        
        // searchParams.set("m", mediaId)
        // location.search = searchParams.toString()
      }
    } else {
      // searchParams.delete("m")
      // location.search = searchParams.toString()
    }
  }, [mediaId])

  useEffect(() => {
    if (copied) {
      setTimeout(() => {
        setCopied(false)
      }, 2000)
    }
  }, [copied])


  const handleBadChannelThumbnail = () => {
    if (channelThumbnail) {
      setChannelThumbnail("")
    }
  }

  useEffect(() => {
    startTransition(async () => {
      if (!media || !media.id) {
        return
      }

      try {
        const numberOfViews = await countNewMediaView(mediaId)

        setPublicMedia({
          ...media,
          numberOfViews
        })
      } catch (err) {
        console.error(`failed to count the number of view for mediaId ${mediaId}`)
      }
    })

  }, [media?.id])


  useEffect(() => {
    startTransition(async () => {
      if (!media || !media.id) {
        return
      }
      const comments = await getComments(mediaId)
      setPublicComments(comments)
    })

  }, [media?.id])

  const [huggingfaceApiKey] = useLocalStorage<string>(
    localStorageKeys.huggingfaceApiKey,
    defaultSettings.huggingfaceApiKey
  )
  
  /*
  useEffect(() => {
    window.addEventListener("keydown", function (e) {
      if (e.code === "Space") {
        e.preventDefault();
      }
    })
  }, [])
  */
  if (!media) { return null }

  const handleSubmitComment = () => {

    startTransition(async () => {
      if (!commentDraft || !huggingfaceApiKey || !mediaId) { return }
    
      const limitedSizeComment = commentDraft.trim().slice(0, 1024).trim()

      const comment = await submitComment(media.id, limitedSizeComment, huggingfaceApiKey)

      setPublicComments(
        [comment].concat(publicComments)
      )

      setCommentDraft("")
      setFocusedOnInput(false)
      setCommenting(false)
    })
  }
  
  return (
    <div className={cn(
      `w-full`,
      `flex flex-col lg:flex-row`
    )}>
      <div className={cn(
        `flex-grow`,
        `flex flex-col`,
        `transition-all duration-200 ease-in-out`,
        `px-2 xl:px-0`
      )}>
        {/** AI MEDIA PLAYER - HORIZONTAL */}
        <MediaPlayer
          media={media}
          enableShortcuts={!isFocusedOnInput}

          // that could be, but let's do it the dirty way for now
          // currentTime={desiredCurrentTime}

          className="rounded-xl overflow-hidden mb-4"
        />

        {/** AI MEDIA TITLE - HORIZONTAL */}
        <div className={cn(
          `flex flex-row space-x-2`,
          `transition-all duration-200 ease-in-out`,
          `text-lg lg:text-xl text-zinc-100 font-medium mb-0 line-clamp-2`,
          `mb-2`,
        )}>
          <div className="">{media.label}</div>
          {/*
          <div className={cn(
            `flex flex-row`, // `inline-block`,
            `bg-neutral-700 text-neutral-300 rounded-lg`,
            // `items-center justify-center`,
            `text-center`,
            `px-1.5 py-0.5`,
            `text-xs`
            )}>
            AI Media Model: {media.model || "HotshotXL"}
          </div>
          */}
        </div>
        
        {/** MEDIA TOOLBAR - HORIZONTAL */}
        <div className={cn(
          `flex flex-col space-y-3 xl:space-y-0 xl:flex-row`,
          `transition-all duration-200 ease-in-out`,
          `items-start xl:items-center`,
          `justify-between`,
          `mb-2 lg:mb-3`,
        )}>
          {/** LEFT PART OF THE TOOLBAR */}
          <div className={cn(
            `flex flex-row`,
            `items-center`
          )}>
            {/** CHANNEL LOGO - VERTICAL */}
            <a
              className={cn(
                `flex flex-col`,
                `mr-3`,
                `cursor-pointer`
              )}
              href={`https://huggingface.co/datasets/${media.channel.datasetUser}/${media.channel.datasetName}`}
              target="_blank">
              <div className="flex w-10 rounded-full overflow-hidden">
              {
                channelThumbnail ? <div className="flex flex-col">
                  <div className="flex w-9 rounded-full overflow-hidden">
                    <img
                      src={channelThumbnail}
                      onError={handleBadChannelThumbnail}
                    />
                  </div>
                </div>
                : <DefaultAvatar
                    username={media.channel.datasetUser}
                    bgColor="#fde047"
                    textColor="#1c1917"
                    width={36}
                    roundShape
                  />}
              </div>
            </a>

            {/** CHANNEL INFO - VERTICAL */}
            <a className={cn(
              `flex flex-row sm:flex-col`,
              `transition-all duration-200 ease-in-out`,
              `cursor-pointer`,
              )}
              href={`https://huggingface.co/datasets/${media.channel.datasetUser}/${media.channel.datasetName}`}
              target="_blank">
              <div className={cn(
                `flex flex-row items-center`,
                `transition-all duration-200 ease-in-out`,
                `text-zinc-100 text-sm lg:text-base font-medium space-x-1`,
                )}>
                <div>{media.channel.label}</div>
                {isCertifiedUser(media.channel.datasetUser) ? <div className="text-sm text-neutral-400"><RiCheckboxCircleFill className="" /></div> : null}
              </div>
              <div className={cn(
                `flex flex-row items-center`,
                `text-neutral-400 text-xs font-normal space-x-1`,
                )}>
                <div>{
                  // TODO implement the follower system
                  formatLargeNumber(0)
                } followers</div>
                <div></div>
              </div>
            </a>


          </div>

          {/** RIGHT PART OF THE TOOLBAR */}
          <div className={cn(
            `flex flex-row`,
            `items-center`,
            `space-x-2`
          )}>

            <LikeButton media={media} />

            {/* SHARE */}
            <div className={cn(
              `flex flex-row`,
              `items-center`
            )}>
              <CopyToClipboard
                text={`${process.env.NEXT_PUBLIC_DOMAIN}/watch?v=${media.id}`}
                onCopy={() => setCopied(true)}>
                <div className={cn(
                  actionButtonClassName,
                  `bg-neutral-700/50 hover:bg-neutral-700/90 text-zinc-100`
                )}>
                  <div className="flex items-center justify-center">
                    {
                      copied ? <LuCopyCheck className="w-5 h-5" />
                      : <PiShareFatLight className="w-6 h-6" />
                    }
                  </div>
                  <span>
                      {copied ? "Copied!" : "Share"}
                  </span>
                </div>
              </CopyToClipboard>
            </div>

            <ActionButton
              href={
                media.model === "LaVie"
                ? "https://huggingface.co/vdo/LaVie"
                : media.model === "SVD"
                ? "https://huggingface.co/stabilityai/stable-video-diffusion-img2vid"
                : "https://huggingface.co/hotshotco/Hotshot-XL"
              }
            >
              <BiCameraMovie className="w-5 h-5" />
              <span className="hidden 2xl:inline">
                Made with {media.model}
              </span>
              <span className="inline 2xl:hidden">
                {media.model}
              </span>
            </ActionButton>

            {isEquirectangular && <ActionButton
              href={`/api/video/${media.id}`}
            >
              <BsHeadsetVr className="w-5 h-5" />
              <span>
                See in VR
              </span>
            </ActionButton>}

            <ActionButton
              href={
                `https://huggingface.co/datasets/${
                  media.channel.datasetUser
                }/${
                  media.channel.datasetName
                }/raw/main/prompt_${
                  media.id
                }.md`
              }
            >
              <LuScrollText className="w-5 h-5" />
              <span>
                Source
              </span>
            </ActionButton>

            <ReportModal media={media} />

          </div>

        </div>

        {/** MEDIA DESCRIPTION - VERTICAL */}
        <div className={cn(
          `flex flex-col p-3`,
          `transition-all duration-200 ease-in-out`,
          `rounded-xl`,
          `bg-neutral-700/50`,
          `text-sm text-zinc-100`,
        )}>

          {/* DESCRIPTION BLOCK */}
          <div className="flex flex-row space-x-2 font-medium mb-1">
            <div>{formatLargeNumber(media.numberOfViews)} views</div>
            <div>{formatTimeAgo(media.updatedAt).replace("about ", "")}</div>
          </div>
          <p>{media.description}</p>
        </div>

        {/* COMMENTS */}
        <div className={cn(
          `flex-col font-medium mb-1 py-6`,
        )}>

          <div className="flex flex-row text-xl text-zinc-100 w-full mb-4">
            {Number(publicComments?.length || 0).toLocaleString()} Comment{
            Number(publicComments?.length || 0) === 1 ? '' : 's'
            }
          </div>
          
          {/* COMMENT INPUT BLOCK - HORIZONTAL */}
          {user && <div className="flex flex-row w-full">
            
            {/* AVATAR */}
            <div 
              // className="flex flex-col w-10 pr-13 overflow-hidden"
              className="flex flex-none flex-col w-10 pr-13 overflow-hidden">
              {
              userThumbnail ? 
                <div className="flex w-9 rounded-full overflow-hidden">
                  <img
                    src={userThumbnail}
                    onError={handleBadUserThumbnail}
                  />
              </div>
              : <DefaultAvatar
                  username={user?.userName}
                  bgColor="#fde047"
                  textColor="#1c1917"
                  width={36}
                  roundShape
                />}
            </div>

            {/* COMMENT INPUTS AND BUTTONS - VERTICAL */}
            <div className="flex flex-col flex-grow">
              <Input
                placeholder="Add a comment.."
                type="text"
                className={cn(
                  `w-full`,
                  `rounded-none`,
                  `bg-transparent dark:bg-transparent`,
                  `border-l-transparent border-r-transparent border-t-transparent dark:border-l-transparent dark:border-r-transparent dark:border-t-transparent`,
                  `border-b border-b-zinc-600 dark:border-b dark:border-b-zinc-600`,
                  `hover:pt-[2px] hover:pb-[1px] hover:border-b-2 hover:border-b-zinc-200 dark:hover:border-b-2 dark:hover:border-b-zinc-200`,
                  
                  `outline-transparent ring-transparent ring-offset-transparent`,
                  `dark:outline-transparent dark:ring-transparent dark:ring-offset-transparent`,
                  `focus-visible:outline-transparent focus-visible:ring-transparent focus-visible:ring-offset-transparent`,
                  `dark:focus-visible:outline-transparent dark:focus-visible:ring-transparent dark:focus-visible:ring-offset-transparent`,
                  
                  `font-normal`,
                  `pl-0 h-8`,

                  `mb-3`
                )}
                onChange={(x) => {
                  if (!isFocusedOnInput) {
                    setFocusedOnInput(true)
                  }
                  if (!isCommenting) {
                    setCommenting(true)
                  }
                  setCommentDraft(x.target.value)
                }}
                value={commentDraft}
                onFocus={() => {
                  if (!isFocusedOnInput) {
                    setFocusedOnInput(true)
                  }
                  if (!isCommenting) {
                    setCommenting(true)
                  }
                }}

                onBlur={() => {
                  setFocusedOnInput(false)
                }}
                onKeyDown={({ key }) => {
                  if (key === 'Enter') {
                    handleSubmitComment()
                  } else {
                    if (!isFocusedOnInput) {
                      setFocusedOnInput(true)
                    }
                    if (!isCommenting) {
                      setCommenting(true)
                    }
                  }
                }}
              />
          
              <div className={cn(
                `flex-row space-x-3 w-full justify-end`,
                isCommenting ? `flex` : `hidden`
              )}>
                <div className="flex flex-row space-x-3">
                <ActionButton
                  variant="ghost"
                  onClick={() => {
                    setCommentDraft("")
                    setCommenting(false)
                    setFocusedOnInput(false)
                  }}
                  >Cancel</ActionButton>
                <ActionButton
                  variant={commentDraft ? "primary" : "secondary"}
                  onClick={handleSubmitComment}
                  >Comment</ActionButton>
                </div>
              </div>
            </div>
          </div>}

          <CommentList
            comments={publicComments}
          />
        </div>

      </div>

      <div className={cn(

        // this one is very important to make sure the right panel is not compressed
        `flex flex-col`,
        `flex-none`,
        `pl-2 lg:pl-4 lg:pr-2`,

        `w-full md:w-[360px] lg:w-[400px] xl:w-[450px]`,
        `transition-all duration-200 ease-in-out`,
      )}>
       <RecommendedVideos media={media} />
      </div>
    </div>
  )
}