fix: toggle tool

This commit is contained in:
Joel 2024-11-13 15:04:23 +08:00
parent 19dc983d30
commit 89b470d0d5
3 changed files with 18 additions and 32 deletions

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useEffect } from 'react' import React from 'react'
import type { ToolWithProvider } from '../../../types' import type { ToolWithProvider } from '../../../types'
import type { BlockEnum } from '../../../types' import type { BlockEnum } from '../../../types'
import type { ToolDefaultValue } from '../../types' import type { ToolDefaultValue } from '../../types'
@ -20,16 +20,6 @@ const ToolViewFlatView: FC<Props> = ({
hasSearchText, hasSearchText,
onSelect, onSelect,
}) => { }) => {
const [fold, setFold] = React.useState<boolean>(true)
useEffect(() => {
if (hasSearchText && fold) {
setFold(false)
return
}
if (!hasSearchText && !fold)
setFold(true)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasSearchText])
return ( return (
<div> <div>
{payload.map(tool => ( {payload.map(tool => (
@ -38,8 +28,7 @@ const ToolViewFlatView: FC<Props> = ({
payload={tool} payload={tool}
viewType={ViewType.flat} viewType={ViewType.flat}
isShowLetterIndex={isShowLetterIndex} isShowLetterIndex={isShowLetterIndex}
isFold={fold} hasSearchText={hasSearchText}
onFoldChange={setFold}
onSelect={onSelect} onSelect={onSelect}
/> />
))} ))}

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useEffect } from 'react' import React from 'react'
import type { ToolWithProvider } from '../../../types' import type { ToolWithProvider } from '../../../types'
import Tool from '../tool' import Tool from '../tool'
import type { BlockEnum } from '../../../types' import type { BlockEnum } from '../../../types'
@ -20,16 +20,6 @@ const Item: FC<Props> = ({
hasSearchText, hasSearchText,
onSelect, onSelect,
}) => { }) => {
const [fold, setFold] = React.useState<boolean>(true)
useEffect(() => {
if (hasSearchText && fold) {
setFold(false)
return
}
if (!hasSearchText && !fold)
setFold(true)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasSearchText])
return ( return (
<div> <div>
<div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'> <div className='flex items-center px-3 h-[22px] text-xs font-medium text-gray-500'>
@ -42,8 +32,7 @@ const Item: FC<Props> = ({
payload={tool} payload={tool}
viewType={ViewType.tree} viewType={ViewType.tree}
isShowLetterIndex={false} isShowLetterIndex={false}
isFold={fold} hasSearchText={hasSearchText}
onFoldChange={setFold}
onSelect={onSelect} onSelect={onSelect}
/> />
))} ))}

View File

@ -1,6 +1,6 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useMemo } from 'react' import React, { useEffect, useMemo } from 'react'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { RiArrowDownSLine, RiArrowRightSLine } from '@remixicon/react' import { RiArrowDownSLine, RiArrowRightSLine } from '@remixicon/react'
import { useGetLanguage } from '@/context/i18n' import { useGetLanguage } from '@/context/i18n'
@ -18,8 +18,7 @@ type Props = {
payload: ToolWithProvider payload: ToolWithProvider
viewType: ViewType viewType: ViewType
isShowLetterIndex: boolean isShowLetterIndex: boolean
isFold: boolean hasSearchText: boolean
onFoldChange: (fold: boolean) => void
onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void onSelect: (type: BlockEnum, tool?: ToolDefaultValue) => void
} }
@ -28,8 +27,7 @@ const Tool: FC<Props> = ({
payload, payload,
viewType, viewType,
isShowLetterIndex, isShowLetterIndex,
isFold, hasSearchText,
onFoldChange,
onSelect, onSelect,
}) => { }) => {
const { t } = useTranslation() const { t } = useTranslation()
@ -37,6 +35,16 @@ const Tool: FC<Props> = ({
const isFlatView = viewType === ViewType.flat const isFlatView = viewType === ViewType.flat
const actions = payload.tools const actions = payload.tools
const hasAction = true // Now always support actions const hasAction = true // Now always support actions
const [isFold, setFold] = React.useState<boolean>(true)
useEffect(() => {
if (hasSearchText && isFold) {
setFold(false)
return
}
if (!hasSearchText && !isFold)
setFold(true)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasSearchText])
const FoldIcon = isFold ? RiArrowRightSLine : RiArrowDownSLine const FoldIcon = isFold ? RiArrowRightSLine : RiArrowDownSLine
@ -63,7 +71,7 @@ const Tool: FC<Props> = ({
className='flex items-center justify-between pl-3 pr-1 w-full rounded-lg hover:bg-gray-50 cursor-pointer select-none' className='flex items-center justify-between pl-3 pr-1 w-full rounded-lg hover:bg-gray-50 cursor-pointer select-none'
onClick={() => { onClick={() => {
if (hasAction) if (hasAction)
onFoldChange(!isFold) setFold(!isFold)
// Now always support actions // Now always support actions
// if (payload.parameters) { // if (payload.parameters) {