File size: 3,349 Bytes
a64395a
 
7b89082
1f122c3
7b89082
1f122c3
7b89082
6967c22
 
b6516e1
16891a6
 
1f122c3
f62b8d3
7b89082
16891a6
 
7b89082
1185ec1
a3f1817
7b89082
a3f1817
 
7b89082
 
 
 
16891a6
1185ec1
 
16891a6
1185ec1
 
 
 
 
 
 
 
 
7b89082
 
16891a6
 
24fa60f
 
1f122c3
7b89082
 
24fa60f
 
 
 
16891a6
 
 
 
 
 
 
 
 
24fa60f
16891a6
 
7b89082
16891a6
24fa60f
 
 
 
 
a3f1817
7b89082
d160b97
 
 
 
 
 
a3f1817
d160b97
 
 
7b89082
 
d160b97
16891a6
24fa60f
 
 
 
 
 
1f122c3
 
 
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
"use client"

import { useEffect, useState, useTransition } from "react"

import { useStore } from "@/app/state/useStore"
import { cn } from "@/lib/utils"
import { ChannelList } from "@/app/interface/channel-list"

import { getPrivateChannels } from "@/app/server/actions/ai-tube-hf/getPrivateChannels"
import { useCurrentUser } from "@/app/state/useCurrentUser"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"

export function UserAccountView() {
  const [_isPending, startTransition] = useTransition()

  const { user, login, apiKey, longStandingApiKey, setLongStandingApiKey } = useCurrentUser({ isLoginRequired: true })
  const setView = useStore(s => s.setView)
  const userChannel = useStore(s => s.userChannel)
  const setUserChannel = useStore(s => s.setUserChannel)

  const userChannels = useStore(s => s.userChannels)
  const setUserChannels = useStore(s => s.setUserChannels)
  const [isLoaded, setLoaded] = useState(false)

  useEffect(() => {
      startTransition(async () => {
        if (!isLoaded && apiKey) {
          try {
            const newUserChannels = await getPrivateChannels({
              apiKey,
              renewCache: true,
            })
            setUserChannels(newUserChannels)
          } catch (err) {
            console.error("failed to load the channel for the current user:", err)
            setUserChannels([])
          } finally {
            setLoaded(true)
          }
        }
      })
  }, [isLoaded, apiKey, userChannels.map(c => c.id).join(","), setUserChannels, setLoaded])
 
  const showSecretFeature = user?.userName.startsWith("jbilcke")

  return (
    <div className={cn(`flex flex-col space-y-4`)}>

      <div className="flex flex-row space-x-2 items-center">
        {showSecretFeature
          ? <label className="flex w-64">Save videos to my HF account</label>
          : <label className="flex w-64">Note: currently only the API login mode is working.</label>}
        <Input
          placeholder="Hugging Face token (with WRITE access)"
          type="password"
          className="font-mono"
          onChange={(x) => {
            setLongStandingApiKey(x.target.value, false)
          }}
          value={longStandingApiKey}
        />
      </div>

      {apiKey ? 
      <div className="flex flex-col space-y-4">
        <h2 className="text-3xl font-bold">@{user?.userName} channels</h2>
        {showSecretFeature
          ? <p>Don&apos;t see your channel? try to <Button onClick={() => login("/account")}>synchronize</Button> again.</p>
          : null
        }
       
        {userChannels?.length ? <ChannelList
          layout="grid"
          channels={[
            // add a fake button to the list, at the beginning
            // { id: "" } as ChannelInfo,

            ...userChannels
          ]}
          onSelect={(userChannel) => {
            if (userChannel.id) {
              setUserChannel(userChannel)
            }
            setView("user_channel")
          }}
        />
        : isLoaded ? null : <p>Loading channels owned by @{user?.userName}..</p>}
      </div> : 
      (
        showSecretFeature
        ? <p>To create a channel, comment or like a video please <Button onClick={() => login("/account")}>Login with Hugging Face</Button>.</p>
        : null)
      }
    </div>
  )
}