Skip to content

Commit

Permalink
enhance: user auth for agents updates (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivyjeong13 authored Jan 10, 2025
1 parent d6d6479 commit 21b76c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
23 changes: 21 additions & 2 deletions ui/admin/app/components/agent/UserAuthorizationSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { UserIcon, XIcon } from "lucide-react";
import { UserIcon, UsersIcon, XIcon } from "lucide-react";
import { toast } from "sonner";
import useSWR, { mutate } from "swr";

Expand All @@ -7,6 +7,7 @@ import { User } from "~/lib/model/users";
import { AgentService } from "~/lib/service/api/agentService";

import { ComboBox } from "~/components/composed/ComboBox";
import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar";
import { Button } from "~/components/ui/button";
import { Checkbox } from "~/components/ui/checkbox";
import { ScrollArea } from "~/components/ui/scroll-area";
Expand Down Expand Up @@ -70,6 +71,11 @@ export function UserAuthorizationSelect({
removeAuthorizationFromAgent.executeAsync(agentId, userID);
};

const validateCreate = (str: string) => {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(str);
};

// add Everyone option if it's not already in the list
const options = (
allUsers.has("*")
Expand All @@ -85,12 +91,14 @@ export function UserAuthorizationSelect({
<div className="flex flex-col gap-2">
<ComboBox
allowCreate
closeOnSelect={false}
emptyLabel="No Users Found."
placeholder="Select or Add Users..."
onChange={handleChange}
onCreate={handleCreate}
options={options}
renderOption={renderOption}
validateCreate={validateCreate}
value={null}
/>
<Separator />
Expand All @@ -116,7 +124,18 @@ export function UserAuthorizationSelect({
<div key={authorization.userID}>
<div className="flex w-full items-center justify-between gap-2 p-2">
<div className="flex items-center gap-2">
<UserIcon className="h-6 w-6" />
{authorization.userID === "*" ? (
<UsersIcon className="h-4 w-4" />
) : authorization.user ? (
<Avatar className="h-4 w-4">
<AvatarImage src={authorization.user.iconURL} />
<AvatarFallback>
<UserIcon />
</AvatarFallback>
</Avatar>
) : (
<UserIcon className="h-4 w-4" />
)}
<p className="text-sm">
{authorization.userID === "*"
? "Everyone"
Expand Down
9 changes: 7 additions & 2 deletions ui/admin/app/components/composed/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ type ComboBoxProps<T extends BaseOption> = {
onChange: (option: T | null) => void;
onCreate?: (value: string) => void;
options: (T | GroupedOption<T>)[];
closeOnSelect?: boolean;
placeholder?: string;
renderOption?: (option: T) => ReactNode;
validateCreate?: (value: string) => boolean;
value?: T | null;
};

Expand Down Expand Up @@ -115,9 +117,11 @@ function ComboBoxList<T extends BaseOption>({
options,
setOpen,
renderOption,
validateCreate,
value,
placeholder = "Filter...",
emptyLabel = "No results found.",
closeOnSelect = true,
}: { setOpen: (open: boolean) => void } & ComboBoxProps<T>) {
const [filteredOptions, setFilteredOptions] =
useState<typeof options>(options);
Expand Down Expand Up @@ -179,7 +183,7 @@ function ComboBoxList<T extends BaseOption>({
<CommandItem
onSelect={() => {
onChange(null);
setOpen(false);
if (closeOnSelect) setOpen(false);
}}
>
{clearLabel ?? "Clear Selection"}
Expand All @@ -193,6 +197,7 @@ function ComboBoxList<T extends BaseOption>({
onCreate?.(savedValue);
setOpen(false);
}}
disabled={validateCreate ? !validateCreate(savedValue) : false}
>
Add &quot;{savedValue}&quot;
</CommandItem>
Expand Down Expand Up @@ -226,7 +231,7 @@ function ComboBoxList<T extends BaseOption>({
value={option.name}
onSelect={() => {
onChange(option);
setOpen(false);
if (closeOnSelect) setOpen(false);
}}
className="justify-between"
>
Expand Down

0 comments on commit 21b76c6

Please sign in to comment.