| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useRef |
| 4 | import { useQuery, keepPreviousData |
| 5 | import { orpc } |
| 6 | import { DitherAvatar } |
| 7 | |
| 8 | function UserAvatar({ name |
| 9 | return ( |
| 10 | <div className={` |
| 11 | {image ? ( |
| 12 | <img src={image |
| 13 | ) : ( |
| 14 | <DitherAvatar name={ |
| 15 | )} |
| 16 | </div> |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | function useDebounce<T |
| 21 | const [debounced, |
| 22 | useEffect(() => |
| 23 | const id = |
| 24 | return () => |
| 25 | }, [value, |
| 26 | return debounced; |
| 27 | } |
| 28 | |
| 29 | type UserResult |
| 30 | |
| 31 | export function UserCombobox({ |
| 32 | value, |
| 33 | onChange, |
| 34 | }: { |
| 35 | value: string |
| 36 | onChange: (id |
| 37 | }) { |
| 38 | const [query, |
| 39 | const [open, |
| 40 | const [selectedUser, |
| 41 | const ref = |
| 42 | |
| 43 | const debouncedQuery = |
| 44 | |
| 45 | const { data: |
| 46 | ...orpc.users. |
| 47 | enabled: debouncedQuery.length |
| 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 | setSelectedUser(null); |
| 62 | setQuery(""); |
| 63 | } |
| 64 | }, [value]); |
| 65 | |
| 66 | function select(u |
| 67 | onChange(u.id |
| 68 | setSelectedUser(u); |
| 69 | setQuery(""); |
| 70 | setOpen(false); |
| 71 | } |
| 72 | |
| 73 | return ( |
| 74 | <div ref={ref |
| 75 | {selectedUser && !open |
| 76 | <div className="flex items-center gap-2 rounded-lg border border-border bg-background px-3 py-2 |
| 77 | <UserAvatar name={ |
| 78 | <span className="flex-1 truncate text-sm text-foreground |
| 79 | {selectedUser.name} |
| 80 | </span> |
| 81 | <button |
| 82 | type="button" |
| 83 | onClick={() => |
| 84 | onChange(""); |
| 85 | setSelectedUser(null); |
| 86 | setOpen(true); |
| 87 | }} |
| 88 | className="text-[10px] text-muted-foreground hover:text-foreground transition-colors" |
| 89 | > |
| 90 | change |
| 91 | </button> |
| 92 | </div> |
| 93 | ) : ( |
| 94 | <input |
| 95 | autoFocus={open} |
| 96 | className="w-full rounded-lg border border-border bg-background px-3 py-2 text-sm text-foreground outline-none placeholder:text-muted-foreground/50 focus:border-ring transition-colors" |
| 97 | placeholder="Search by name or email…" |
| 98 | value={query} |
| 99 | onChange={(e) |
| 100 | setQuery(e.target |
| 101 | setOpen(true); |
| 102 | }} |
| 103 | onFocus={() => |
| 104 | /> |
| 105 | )} |
| 106 | {open && query |
| 107 | <div className="absolute left-0 right-0 top-full z-50 mt-1 rounded-xl border border-border bg-card shadow-lg overflow-hidden |
| 108 | {isFetching && debouncedQuery |
| 109 | <div className="px-3 py-2.5 text-xs text-muted-foreground |
| 110 | ) : results.length |
| 111 | <div className="px-3 py-2.5 text-xs text-muted-foreground |
| 112 | {debouncedQuery.length > |
| 113 | </div> |
| 114 | ) : ( |
| 115 | results.map((u |
| 116 | <button |
| 117 | key={u.id |
| 118 | type="button" |
| 119 | onMouseDown={(e) |
| 120 | e.preventDefault(); |
| 121 | select(u); |
| 122 | }} |
| 123 | className="flex w-full items-center gap-2.5 px-3 py-2.5 text-left hover:bg-muted transition-colors" |
| 124 | > |
| 125 | <UserAvatar name={ |
| 126 | <div className="flex flex-col min-w-0 |
| 127 | <span className="truncate text-sm font-medium text-foreground |
| 128 | <span className="truncate text-xs text-muted-foreground |
| 129 | </div> |
| 130 | </button> |
| 131 | )) |
| 132 | )} |
| 133 | </div> |
| 134 | )} |
| 135 | </div> |
| 136 | ); |
| 137 | } |