-
Notifications
You must be signed in to change notification settings - Fork 28
/
user-combobox.tsx
133 lines (121 loc) · 3.76 KB
/
user-combobox.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use client";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Check, ChevronsUpDown } from "lucide-react";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";
import { useQuery } from "react-query";
import { toast } from "sonner";
export function UserCombobox({
setSelectedUser,
selectedUser,
}: {
setSelectedUser: (user: string) => void;
selectedUser?: string;
}) {
const project_id = useParams()?.project_id as string;
const [open, setOpen] = useState(false);
const [selectedUserId, setSelectedUserIdState] = useState(selectedUser || "");
const [searchQuery, setSearchQuery] = useState("");
const [userIds, setUserIds] = useState<string[]>([]);
const [showLoader, setShowLoader] = useState(false);
const [internalSelectedUser, setInternalSelectedUser] =
useState(selectedUser);
const handleSelectUser = (currentValue: string) => {
const newUserId = currentValue === selectedUserId ? "" : currentValue;
setSelectedUserIdState(newUserId);
setSelectedUser(newUserId);
setOpen(false);
};
useEffect(() => {
setSelectedUserIdState(selectedUser || "");
setInternalSelectedUser(selectedUser || "");
}, [selectedUser]);
const fetchUserIds = useQuery({
queryKey: ["fetch-user-ids-query", project_id],
queryFn: async () => {
const response = await fetch(`/api/user-ids?projectId=${project_id}`);
if (!response.ok) {
throw new Error("Failed to fetch user ids");
}
const result = await response.json();
return result;
},
onSuccess: (data: { userIDs: any }) => {
setUserIds(data?.userIDs || []);
},
onError: (error) => {
setShowLoader(false);
toast.error("Failed to fetch user ids", {
description: error instanceof Error ? error.message : String(error),
});
},
});
if (fetchUserIds.isLoading) {
return <div>Loading...</div>;
}
const onInputChange = (value: string) => {
setSearchQuery(value);
};
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={open}
className="w-[200px] justify-between"
>
{selectedUserId ? selectedUserId : "Filter by user id..."}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-[200px] p-0">
<Command>
<CommandInput
placeholder="Search users..."
value={searchQuery}
onValueChange={onInputChange}
/>
<CommandEmpty>No attribute found.</CommandEmpty>
<CommandGroup>
{userIds.map((id: string) => (
<CommandItem
key={id}
value={id}
onSelect={(currentValue) => {
setSelectedUserIdState(
currentValue === selectedUserId ? "" : currentValue
);
setSelectedUser(
currentValue === selectedUserId ? "" : currentValue
);
handleSelectUser(currentValue);
setOpen(false);
}}
>
<Check
className={`mr-2 h-4 w-4 ${
selectedUserId === id ? "opacity-100" : "opacity-0"
}`}
/>
{id}
</CommandItem>
))}
</CommandGroup>
</Command>
</PopoverContent>
</Popover>
);
}