| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useRef |
| 4 | import { useQuery, keepPreviousData |
| 5 | import { orpc } |
| 6 | |
| 7 | function useDebounce<T |
| 8 | const [debounced, |
| 9 | useEffect(() => |
| 10 | const id = |
| 11 | return () => |
| 12 | }, [value, |
| 13 | return debounced; |
| 14 | } |
| 15 | |
| 16 | type AllocationResult |
| 17 | |
| 18 | function formatAllocation(a: |
| 19 | return a.ipAlias |
| 20 | } |
| 21 | |
| 22 | export function AllocationCombobox({ |
| 23 | nodeId, |
| 24 | value, |
| 25 | onChange, |
| 26 | initialAllocation, |
| 27 | placeholder = "Search by IP or port…" |
| 28 | disabledPlaceholder = "Select a node first" |
| 29 | emptyText = "No free allocations found" |
| 30 | }: { |
| 31 | nodeId: string |
| 32 | value: string |
| 33 | onChange: (id |
| 34 | initialAllocation?: AllocationResult |
| 35 | placeholder?: string |
| 36 | disabledPlaceholder?: string |
| 37 | emptyText?: string |
| 38 | }) { |
| 39 | const [query, |
| 40 | const [open, |
| 41 | const [selected, |
| 42 | const ref = |
| 43 | |
| 44 | const debouncedQuery = |
| 45 | |
| 46 | const { data: |
| 47 | ...orpc.allocations. |
| 48 | placeholderData: keepPreviousData, |
| 49 | }); |
| 50 | |
| 51 | useEffect(() => |
| 52 | function onClickOutside(e |
| 53 | if (ref.current |
| 54 | } |
| 55 | document.addEventListener("mousedown |
| 56 | return () => |
| 57 | }, []); |
| 58 | |
| 59 | useEffect(() => |
| 60 | if (!value) |
| 61 | }, [value]); |
| 62 | |
| 63 | useEffect(() => |
| 64 | setSelected(initialAllocation ?? |
| 65 | }, [initialAllocation?.id |
| 66 | |
| 67 | function select(a |
| 68 | onChange(a.id |
| 69 | setSelected(a); |
| 70 | setQuery(""); |
| 71 | setOpen(false); |
| 72 | } |
| 73 | |
| 74 | if (!nodeId) |
| 75 | return ( |
| 76 | <div className="w-full rounded-lg border border-border bg-muted/30 px-3 py-2 text-sm text-muted-foreground/50 |
| 77 | {disabledPlaceholder} |
| 78 | </div> |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | return ( |
| 83 | <div ref={ref |
| 84 | {selected && !open |
| 85 | <div className="flex items-center gap-2 rounded-lg border border-border bg-background px-3 py-2 |
| 86 | <span className="flex-1 truncate font-mono text-sm text-foreground |
| 87 | <button |
| 88 | type="button" |
| 89 | onClick={() => |
| 90 | onChange(""); |
| 91 | setSelected(null); |
| 92 | setOpen(true); |
| 93 | }} |
| 94 | className="text-[10px] text-muted-foreground hover:text-foreground transition-colors" |
| 95 | > |
| 96 | change |
| 97 | </button> |
| 98 | </div> |
| 99 | ) : ( |
| 100 | <input |
| 101 | autoFocus={open} |
| 102 | className="w-full rounded-lg border border-border bg-background px-3 py-2 font-mono text-sm text-foreground outline-none placeholder:font-sans placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 103 | placeholder={placeholder} |
| 104 | value={query} |
| 105 | onChange={(e) |
| 106 | setQuery(e.target |
| 107 | setOpen(true); |
| 108 | }} |
| 109 | onFocus={() => |
| 110 | /> |
| 111 | )} |
| 112 | {open && ( |
| 113 | <div className="absolute left-0 right-0 top-full z-50 mt-1 rounded-xl border border-border bg-card shadow-lg overflow-hidden |
| 114 | {isFetching && debouncedQuery |
| 115 | <div className="px-3 py-2.5 text-xs text-muted-foreground |
| 116 | ) : results.length |
| 117 | <div className="px-3 py-2.5 text-xs text-muted-foreground |
| 118 | ) : ( |
| 119 | results.map((a |
| 120 | <button |
| 121 | key={a.id |
| 122 | type="button" |
| 123 | onMouseDown={(e) |
| 124 | e.preventDefault(); |
| 125 | select(a); |
| 126 | }} |
| 127 | className="flex w-full items-center gap-2.5 px-3 py-2.5 text-left font-mono text-sm text-foreground hover:bg-muted transition-colors" |
| 128 | > |
| 129 | {formatAllocation(a)} |
| 130 | </button> |
| 131 | )) |
| 132 | )} |
| 133 | </div> |
| 134 | )} |
| 135 | </div> |
| 136 | ); |
| 137 | } |