Skip to content

Commit bc6454d

Browse files
Refactor auto-selection logic in useAutoSelectLatestChild to improve parent handling and prevent redundant selections
1 parent 0a7f4a9 commit bc6454d

4 files changed

Lines changed: 8 additions & 12 deletions

File tree

web/packages/agenta-entity-ui/src/selection/components/UnifiedEntityPicker/variants/PopoverCascaderVariant.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import React, {useCallback, useEffect, useMemo, useRef, useState, type CSSProperties} from "react"
1818

19-
import {cn} from "@agenta/ui"
19+
import {bgColors, cn} from "@agenta/ui"
2020
import {EntityListItem, SearchInput} from "@agenta/ui/components/selection"
2121
import {CaretDown, Plus, X} from "@phosphor-icons/react"
2222
import {Button, Checkbox, Empty, Popover, Spin, Tabs} from "antd"
@@ -176,7 +176,7 @@ function ChildPanelContent({
176176
"flex items-center gap-2 px-2 py-1.5 rounded-md",
177177
isDisabled
178178
? "opacity-50 cursor-not-allowed"
179-
: "cursor-pointer hover:bg-[var(--ag-rgba-051729-04)]",
179+
: cn("cursor-pointer", bgColors.hoverSubtle),
180180
)}
181181
onClick={() => {
182182
if (!isDisabled) onSelect(item)

web/packages/agenta-entity-ui/src/selection/hooks/modes/useListPopoverMode.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -559,25 +559,21 @@ export function useAutoSelectLatestChild<TSelection = EntitySelectionResult>({
559559
onSelect,
560560
onComplete,
561561
}: UseAutoSelectLatestChildOptions<TSelection>): void {
562-
const hasSelectedRef = useRef(false)
562+
const handledParentIdRef = useRef<string | null>(null)
563563

564564
// Fetch children
565565
const {items: children, query} = useChildrenData(childLevelConfig, parentId, true)
566566

567-
useEffect(() => {
568-
hasSelectedRef.current = false
569-
}, [parentId])
570-
571567
// Auto-select first enabled child when loaded
572568
useEffect(() => {
573-
if (hasSelectedRef.current || query.isPending) return
569+
if (handledParentIdRef.current === parentId || query.isPending) return
574570

575571
const firstChild = children.find(
576572
(child) => !disabledChildIds?.has(childLevelConfig.getId(child)),
577573
)
578574

579575
if (firstChild) {
580-
hasSelectedRef.current = true
576+
handledParentIdRef.current = parentId
581577
const parentPathItem: SelectionPathItem = {
582578
type: parentLevelConfig.type,
583579
id: parentId,
@@ -598,7 +594,7 @@ export function useAutoSelectLatestChild<TSelection = EntitySelectionResult>({
598594
// settled, or when loaded children are all disabled.
599595
if (!query.isError && !query.isFetched && children.length === 0) return
600596

601-
hasSelectedRef.current = true
597+
handledParentIdRef.current = parentId
602598
onComplete()
603599
}, [
604600
query.isPending,

web/packages/agenta-ui/src/components/selection/ListItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export function ListItem({
173173
"border-r-2 border-primary",
174174
)
175175
: isHovered
176-
? cn(bgColors.subtle, "cursor-pointer", textColors.primary)
176+
? cn(bgColors.subtle, bgColors.hoverState, "cursor-pointer", textColors.primary)
177177
: cn("cursor-pointer", bgColors.hoverSubtle, textColors.iconHover)
178178

179179
return (

web/packages/agenta-ui/src/utils/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export const bgColors = {
175175
/** Elevated background - colorBgElevated (#ffffff) */
176176
elevated: "bg-[var(--ag-c-FFFFFF)]",
177177
/** Hover subtle - for hover states on subtle backgrounds */
178-
hoverSubtle: "hover:bg-zinc-1",
178+
hoverSubtle: "hover:bg-zinc-1 dark:hover:bg-zinc-2",
179179
} as const
180180

181181
/**

0 commit comments

Comments
 (0)