Skip to content

Commit 448a6cd

Browse files
committed
Refactor bot switch dropdown
1 parent 8000f3a commit 448a6cd

4 files changed

Lines changed: 47 additions & 25 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { FC, memo } from 'react'
2+
import dropdownIcon from '~/assets/icons/dropdown.svg'
3+
import { BotId } from '~app/bots'
4+
import SwitchBotDropdown from '../SwitchBotDropdown'
5+
import Tooltip from '../Tooltip'
6+
7+
interface Props {
8+
botId: BotId
9+
name: string
10+
fullName?: string
11+
onSwitchBot?: (botId: BotId) => void
12+
}
13+
14+
const ChatbotName: FC<Props> = (props) => {
15+
const node = (
16+
<Tooltip content={props.fullName || props.name}>
17+
<span className="font-semibold text-primary-text text-sm cursor-pointer">{props.name}</span>
18+
</Tooltip>
19+
)
20+
if (!props.onSwitchBot) {
21+
return node
22+
}
23+
const triggerNode = (
24+
<div className="flex flex-row items-center gap-[2px]">
25+
{node}
26+
<img src={dropdownIcon} className="w-5 h-5" />
27+
</div>
28+
)
29+
return <SwitchBotDropdown selectedBotId={props.botId} onChange={props.onSwitchBot} triggerNode={triggerNode} />
30+
}
31+
32+
export default memo(ChatbotName)

src/app/components/Chat/ConversationPanel.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Tooltip from '../Tooltip'
1818
import ChatMessageInput from './ChatMessageInput'
1919
import ChatMessageList from './ChatMessageList'
2020
import WebAccessCheckbox from './WebAccessCheckbox'
21+
import ChatbotName from './ChatbotName'
2122

2223
interface Props {
2324
botId: BotId
@@ -93,15 +94,15 @@ const ConversationPanel: FC<Props> = (props) => {
9394
<div className="flex flex-row items-center">
9495
<motion.img
9596
src={botInfo.avatar}
96-
className="w-[18px] h-[18px] object-contain rounded-full"
97+
className="w-[18px] h-[18px] object-contain rounded-full mr-2"
9798
whileHover={{ rotate: 180 }}
9899
/>
99-
<Tooltip content={props.bot.name || botInfo.name}>
100-
<span className="font-semibold text-primary-text text-sm cursor-default ml-2 mr-1">{botInfo.name}</span>
101-
</Tooltip>
102-
{mode === 'compact' && props.onSwitchBot && (
103-
<SwitchBotDropdown selectedBotId={props.botId} onChange={props.onSwitchBot} />
104-
)}
100+
<ChatbotName
101+
botId={props.botId}
102+
name={botInfo.name}
103+
fullName={props.bot.name}
104+
onSwitchBot={mode === 'compact' ? props.onSwitchBot : undefined}
105+
/>
105106
</div>
106107
<WebAccessCheckbox botId={props.botId} />
107108
<div className="flex flex-row items-center gap-3">

src/app/components/SwitchBotDropdown.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import { Menu, Transition } from '@headlessui/react'
2-
import { FC, Fragment, useCallback } from 'react'
3-
import dropdownIcon from '~/assets/icons/dropdown.svg'
2+
import { FC, Fragment, ReactNode } from 'react'
43
import { BotId } from '~app/bots'
54
import { useEnabledBots } from '~app/hooks/use-enabled-bots'
65

76
interface Props {
7+
triggerNode: ReactNode
88
selectedBotId: BotId
99
onChange: (botId: BotId) => void
1010
}
1111

1212
const SwitchBotDropdown: FC<Props> = (props) => {
1313
const enabledBots = useEnabledBots()
14-
15-
const onSelect = useCallback(
16-
(botId: BotId) => {
17-
props.onChange(botId)
18-
},
19-
[props],
20-
)
21-
2214
return (
2315
<Menu as="div" className="relative inline-block text-left h-5">
24-
<Menu.Button>
25-
<img src={dropdownIcon} className="w-5 h-5" />
26-
</Menu.Button>
16+
<Menu.Button className="flex">{props.triggerNode}</Menu.Button>
2717
<Transition
2818
as={Fragment}
2919
enter="transition ease-out duration-100"
@@ -42,7 +32,7 @@ const SwitchBotDropdown: FC<Props> = (props) => {
4232
<Menu.Item key={botId}>
4333
<div
4434
className="px-4 py-2 ui-active:bg-primary-blue ui-active:text-white ui-not-active:text-secondary-text cursor-pointer flex flex-row items-center gap-3 pr-8"
45-
onClick={() => onSelect(botId)}
35+
onClick={() => props.onChange(botId)}
4636
>
4737
<div className="w-4 h-4">
4838
<img src={bot.avatar} className="w-4 h-4" />

src/app/pages/SidePanelPage.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { cx } from '~/utils'
21
import { useAtom } from 'jotai'
32
import { useCallback, useMemo } from 'react'
43
import { useTranslation } from 'react-i18next'
54
import clearIcon from '~/assets/icons/clear.svg'
5+
import { cx } from '~/utils'
66
import Button from '~app/components/Button'
77
import ChatMessageInput from '~app/components/Chat/ChatMessageInput'
88
import ChatMessageList from '~app/components/Chat/ChatMessageList'
9-
import SwitchBotDropdown from '~app/components/SwitchBotDropdown'
9+
import ChatbotName from '~app/components/Chat/ChatbotName'
1010
import { CHATBOTS } from '~app/consts'
1111
import { ConversationContext, ConversationContextValue } from '~app/context'
1212
import { useChat } from '~app/hooks/use-chat'
@@ -43,8 +43,7 @@ function SidePanelPage() {
4343
<div className="border-b border-solid border-primary-border flex flex-row items-center justify-between gap-2 py-3 mx-3">
4444
<div className="flex flex-row items-center gap-2">
4545
<img src={botInfo.avatar} className="w-4 h-4 object-contain rounded-full" />
46-
<span className="font-semibold text-primary-text text-xs">{botInfo.name}</span>
47-
<SwitchBotDropdown selectedBotId={botId} onChange={setBotId} />
46+
<ChatbotName botId={botId} name={botInfo.name} onSwitchBot={setBotId} />
4847
</div>
4948
<div className="flex flex-row items-center gap-3">
5049
<img

0 commit comments

Comments
 (0)