File size: 5,208 Bytes
4304c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useEffect, useState } from 'react'
import type { FC } from 'react'
import { useTranslation } from 'react-i18next'
import {
  PencilSquareIcon,
} from '@heroicons/react/24/outline'
import cn from 'classnames'
import Button from '../../../base/button'
import List from './list'
import AppInfo from '@/app/components/share/chat/sidebar/app-info'
// import Card from './card'
import type { ConversationItem, SiteInfo } from '@/models/share'
import { fetchConversations } from '@/service/share'

export type ISidebarProps = {
  copyRight: string
  currentId: string
  onCurrentIdChange: (id: string) => void
  list: ConversationItem[]
  isClearConversationList: boolean
  pinnedList: ConversationItem[]
  isClearPinnedConversationList: boolean
  isInstalledApp: boolean
  installedAppId?: string
  siteInfo: SiteInfo
  onMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void
  onPinnedMoreLoaded: (res: { data: ConversationItem[]; has_more: boolean }) => void
  isNoMore: boolean
  isPinnedNoMore: boolean
  onPin: (id: string) => void
  onUnpin: (id: string) => void
  controlUpdateList: number
  onDelete: (id: string) => void
}

const Sidebar: FC<ISidebarProps> = ({

  copyRight,

  currentId,

  onCurrentIdChange,

  list,

  isClearConversationList,

  pinnedList,

  isClearPinnedConversationList,

  isInstalledApp,

  installedAppId,

  siteInfo,

  onMoreLoaded,

  onPinnedMoreLoaded,

  isNoMore,

  isPinnedNoMore,

  onPin,

  onUnpin,

  controlUpdateList,

  onDelete,

}) => {
  const { t } = useTranslation()
  const [hasPinned, setHasPinned] = useState(false)

  const checkHasPinned = async () => {
    const { data }: any = await fetchConversations(isInstalledApp, installedAppId, undefined, true)
    setHasPinned(data.length > 0)
  }

  useEffect(() => {
    checkHasPinned()
  }, [])

  useEffect(() => {
    if (controlUpdateList !== 0)
      checkHasPinned()
  }, [controlUpdateList])

  const maxListHeight = isInstalledApp ? 'max-h-[30vh]' : 'max-h-[40vh]'

  return (
    <div

      className={

        cn(

          isInstalledApp ? 'tablet:h-[calc(100vh_-_74px)]' : 'tablet:h-[calc(100vh_-_3rem)]',

          'shrink-0 flex flex-col bg-white pc:w-[244px] tablet:w-[192px] mobile:w-[240px]  border-r border-gray-200 mobile:h-screen',

        )

      }

    >

      {isInstalledApp && (

        <AppInfo

          className='my-4 px-4'

          name={siteInfo.title || ''}

          icon={siteInfo.icon || ''}

          icon_background={siteInfo.icon_background}

        />

      )}

      <div className="flex flex-shrink-0 p-4 !pb-0">

        <Button

          onClick={() => { onCurrentIdChange('-1') }}

          className="group flex w-full flex-shrink-0 !justify-start !h-9 text-primary-600 items-center text-sm">

          <PencilSquareIcon className="mr-2 h-4 w-4" /> {t('share.chat.newChat')}

        </Button>

      </div>

      <div className={'flex-grow flex flex-col h-0 overflow-y-auto overflow-x-hidden'}>

        {/* pinned list */}

        {hasPinned && (

          <div className={cn('mt-4 px-4', list.length === 0 && 'flex flex-col flex-grow')}>

            <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.pinnedTitle')}</div>

            <List

              className={cn(list.length > 0 ? maxListHeight : 'flex-grow')}

              currentId={currentId}

              onCurrentIdChange={onCurrentIdChange}

              list={pinnedList}

              isClearConversationList={isClearPinnedConversationList}

              isInstalledApp={isInstalledApp}

              installedAppId={installedAppId}

              onMoreLoaded={onPinnedMoreLoaded}

              isNoMore={isPinnedNoMore}

              isPinned={true}

              onPinChanged={id => onUnpin(id)}

              controlUpdate={controlUpdateList + 1}

              onDelete={onDelete}

            />

          </div>

        )}

        {/* unpinned list */}

        <div className={cn('mt-4 px-4', !hasPinned && 'flex flex-col flex-grow')}>

          {(hasPinned && list.length > 0) && (

            <div className='mb-1.5 leading-[18px] text-xs text-gray-500 font-medium uppercase'>{t('share.chat.unpinnedTitle')}</div>

          )}

          <List

            className={cn(hasPinned ? maxListHeight : 'flex-grow')}

            currentId={currentId}

            onCurrentIdChange={onCurrentIdChange}

            list={list}

            isClearConversationList={isClearConversationList}

            isInstalledApp={isInstalledApp}

            installedAppId={installedAppId}

            onMoreLoaded={onMoreLoaded}

            isNoMore={isNoMore}

            isPinned={false}

            onPinChanged={id => onPin(id)}

            controlUpdate={controlUpdateList + 1}

            onDelete={onDelete}

          />

        </div>



      </div>

      <div className="flex flex-shrink-0 pr-4 pb-4 pl-4">

        <div className="text-gray-400 font-normal text-xs">© {copyRight} {(new Date()).getFullYear()}</div>

      </div>

    </div>
  )
}

export default React.memo(Sidebar)