File size: 5,103 Bytes
15bfa8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client'

import * as React from 'react'
import Image from 'next/image'
import Textarea from 'react-textarea-autosize'
import { useAtomValue } from 'jotai'
import { useEnterSubmit } from '@/lib/hooks/use-enter-submit'
import { cn } from '@/lib/utils'

import BrushIcon from '@/assets/images/brush.svg'
import ChatIcon from '@/assets/images/chat.svg'
import VisualSearchIcon from '@/assets/images/visual-search.svg'
import SendIcon from '@/assets/images/send.svg'
import PinIcon from '@/assets/images/pin.svg'
import PinFillIcon from '@/assets/images/pin-fill.svg'

import { useBing } from '@/lib/hooks/use-bing'
import { voiceListenAtom } from '@/state'
import Voice from './voice'
import { ChatImage } from './chat-image'
import { ChatAttachments } from './chat-attachments'

export interface ChatPanelProps
  extends Pick<
    ReturnType<typeof useBing>,
    | 'generating'
    | 'input'
    | 'setInput'
    | 'sendMessage'
    | 'resetConversation'
    | 'isSpeaking'
    | 'attachmentList'
    | 'uploadImage'
    | 'setAttachmentList'
  > {
  id?: string
  className?: string
}

export function ChatPanel({
  isSpeaking,
  generating,
  input,
  setInput,
  className,
  sendMessage,
  resetConversation,
  attachmentList,
  uploadImage,
  setAttachmentList
}: ChatPanelProps) {
  const inputRef = React.useRef<HTMLTextAreaElement>(null)
  const {formRef, onKeyDown} = useEnterSubmit()
  const [focused, setFocused] = React.useState(false)
  const [active, setActive] = React.useState(false)
  const [pin, setPin] = React.useState(false)
  const [tid, setTid] = React.useState<any>()
  const voiceListening = useAtomValue(voiceListenAtom)

  const setBlur = React.useCallback(() => {
    clearTimeout(tid)
    setActive(false)
    const _tid = setTimeout(() => setFocused(false), 2000);
    setTid(_tid)
  }, [tid])

  const setFocus = React.useCallback(() => {
    setFocused(true)
    setActive(true)
    clearTimeout(tid)
    inputRef.current?.focus()
  }, [tid])

  React.useEffect(() => {
    if (input) {
      setFocus()
    }
  }, [input])

  return (
    <form
      className={cn('chat-panel', className)}
      onSubmit={async e => {
        e.preventDefault()
        if (generating) {
          return;
        }
        if (!input?.trim()) {
          return
        }
        setInput('')
        setPin(false)
        await sendMessage(input)
      }}
      ref={formRef}
    >
      <div className="action-bar pb-4">
        <div className={cn('action-root', { focus: active || pin })} speech-state="hidden" visual-search="" drop-target="">
          <div className="fade bottom">
            <div className="background"></div>
          </div>
          <div className={cn('outside-left-container', { collapsed: focused })}>
            <div className="button-compose-wrapper">
              <button className="body-2 button-compose" type="button" aria-label="新主题" onClick={resetConversation}>
                <div className="button-compose-content">
                  <Image className="pl-2" alt="brush" src={BrushIcon} width={40} />
                  <div className="button-compose-text">新主题</div>
                </div>
              </button>
            </div>
          </div>
          <div
            className={cn('main-container', { active: active || pin })}
            style={{ minHeight: pin ? '360px' : undefined }}
            onClick={setFocus}
            onBlur={setBlur}
          >
            <div className="main-bar">
              <Image alt="chat" src={ChatIcon} width={20} color="blue" />
              <Textarea
                ref={inputRef}
                tabIndex={0}
                onKeyDown={onKeyDown}
                rows={1}
                value={input}
                onChange={e => setInput(e.target.value.slice(0, 4000))}
                placeholder={voiceListening ? '持续对话中...对话完成说“发送”即可' : 'Shift + Enter 换行'}
                spellCheck={false}
                className="message-input min-h-[24px] -mx-1 w-full text-base resize-none bg-transparent focus-within:outline-none"
              />
              <ChatImage uploadImage={uploadImage}>
                <Image alt="visual-search" src={VisualSearchIcon} width={24} />
              </ChatImage>
              <Voice setInput={setInput} sendMessage={sendMessage} isSpeaking={isSpeaking} input={input} />
              <button type="submit">
                <Image alt="send" src={SendIcon} width={20} style={{ marginTop: '2px' }} />
              </button>
            </div>
            <ChatAttachments attachmentList={attachmentList} setAttachmentList={setAttachmentList} uploadImage={uploadImage} />
            <div className="body-1 bottom-bar">
              <div className="letter-counter"><span>{input.length}</span>/4000</div>
              <button onClick={() => {
                setPin(!pin)
              }} className="pr-2">
               <Image alt="pin" src={pin ? PinFillIcon : PinIcon} width={20} />
              </button>
            </div>
          </div>
        </div>
      </div>
    </form>
  )
}