Skip to content

Commit

Permalink
fix: 快速按下删除会话导致的问题 Chanzhaoyu#917
Browse files Browse the repository at this point in the history
  • Loading branch information
Chanzhaoyu authored and jingfelix committed Apr 7, 2023
1 parent b271e24 commit 64bc76e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/utils/functions/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type CallbackFunc<T extends unknown[]> = (...args: T) => void

export function debounce<T extends unknown[]>(
func: CallbackFunc<T>,
wait: number,
): (...args: T) => void {
let timeoutId: ReturnType<typeof setTimeout> | undefined

return (...args: T) => {
const later = () => {
clearTimeout(timeoutId)
func(...args)
}

clearTimeout(timeoutId)
timeoutId = setTimeout(later, wait)
}
}
8 changes: 5 additions & 3 deletions src/views/chat/layout/sider/List.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NInput, NPopconfirm, NScrollbar } from 'naive-ui'
import { SvgIcon } from '@/components/common'
import { useAppStore, useChatStore } from '@/store'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { debounce } from '@/utils/functions/debounce'
const { isMobile } = useBasicLayout()
Expand Down Expand Up @@ -36,6 +37,8 @@ function handleDelete(index: number, event?: MouseEvent | TouchEvent) {
appStore.setSiderCollapsed(true)
}
const handleDeleteDebounce = debounce(handleDelete, 600)
function handleEnter({ uuid }: Chat.History, isEdit: boolean, event: KeyboardEvent) {
event?.stopPropagation()
if (event.key === 'Enter')
Expand Down Expand Up @@ -69,8 +72,7 @@ function isActive(uuid: number) {
<div class="relative flex-1 overflow-hidden break-all text-ellipsis whitespace-nowrap">
<NInput
v-if="item.isEdit"
v-model:value="item.title"
size="tiny"
v-model:value="item.title" size="tiny"
@keypress="handleEnter(item, false, $event)"
/>
<span v-else>{{ item.title }}</span>
Expand All @@ -85,7 +87,7 @@ function isActive(uuid: number) {
<button class="p-1">
<SvgIcon icon="ri:edit-line" @click="handleEdit(item, true, $event)" />
</button>
<NPopconfirm placement="bottom" @positive-click="handleDelete(index, $event)">
<NPopconfirm placement="bottom" @positive-click="handleDeleteDebounce(index, $event)">
<template #trigger>
<button class="p-1">
<SvgIcon icon="ri:delete-bin-line" />
Expand Down

0 comments on commit 64bc76e

Please sign in to comment.