Skip to content

Commit

Permalink
Fix: use debounce for switch (#3585)
Browse files Browse the repository at this point in the history
  • Loading branch information
JzoNgKVO authored Apr 18, 2024
1 parent 80e390b commit 8cc1944
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions web/app/components/datasets/documents/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use client'
import type { FC, SVGProps } from 'react'
import React, { useEffect, useState } from 'react'
import { useDebounceFn } from 'ahooks'
import { ArrowDownIcon, TrashIcon } from '@heroicons/react/24/outline'
import { ExclamationCircleIcon } from '@heroicons/react/24/solid'
import dayjs from 'dayjs'
Expand Down Expand Up @@ -154,6 +155,14 @@ export const OperationAction: FC<{
onUpdate(operationName)
}

const { run: handleSwitch } = useDebounceFn((operationName: OperationName) => {
if (operationName === 'enable' && enabled)
return
if (operationName === 'disable' && !enabled)
return
onOperate(operationName)
}, { wait: 500 })

return <div className='flex items-center' onClick={e => e.stopPropagation()}>
{isListScene && !embeddingAvailable && (
<Switch defaultValue={false} onChange={() => { }} disabled={true} size='md' />
Expand All @@ -166,7 +175,7 @@ export const OperationAction: FC<{
<Switch defaultValue={false} onChange={() => { }} disabled={true} size='md' />
</div>
</Tooltip>
: <Switch defaultValue={enabled} onChange={v => onOperate(v ? 'enable' : 'disable')} size='md' />
: <Switch defaultValue={enabled} onChange={v => handleSwitch(v ? 'enable' : 'disable')} size='md' />
}
<Divider className='!ml-4 !mr-2 !h-3' type='vertical' />
</>
Expand All @@ -189,7 +198,7 @@ export const OperationAction: FC<{
<div>
<Switch
defaultValue={archived ? false : enabled}
onChange={v => !archived && onOperate(v ? 'enable' : 'disable')}
onChange={v => !archived && handleSwitch(v ? 'enable' : 'disable')}
disabled={archived}
size='md'
/>
Expand Down

0 comments on commit 8cc1944

Please sign in to comment.