overleaf/services/web/frontend/js/features/history/hooks/use-dropdown-active-item.tsx
June Kelly 0346e32906 Merge pull request #12836 from overleaf/td-history-performance
History migration: Improve performance of selecting or comparing versions

GitOrigin-RevId: 2a18a93c246fb94ed8d8b9770449be83364177ea
2023-05-17 08:04:07 +00:00

36 lines
946 B
TypeScript

import { Dispatch, SetStateAction, useCallback, useState } from 'react'
import { LoadedUpdate, Version } from '../services/types/update'
type DropdownItem = LoadedUpdate | Version
export type ActiveDropdownValue = {
item: DropdownItem | null
isOpened: boolean
}
export type ActiveDropdown = {
activeDropdownItem: ActiveDropdownValue
setActiveDropdownItem: Dispatch<SetStateAction<ActiveDropdownValue>>
closeDropdownForItem: (item: DropdownItem) => void
}
function useDropdownActiveItem(): ActiveDropdown {
const [activeDropdownItem, setActiveDropdownItem] =
useState<ActiveDropdownValue>({
item: null,
isOpened: false,
})
const closeDropdownForItem = useCallback(
(item: DropdownItem) => setActiveDropdownItem({ item, isOpened: false }),
[setActiveDropdownItem]
)
return {
activeDropdownItem,
setActiveDropdownItem,
closeDropdownForItem,
}
}
export default useDropdownActiveItem