|
| 1 | +import { ACCOUNT_PERMISSION_OBJECT } from "@/config/constants"; |
| 2 | +import { useObjects } from "@/entities/nodes/object/domain/get-objects.query"; |
| 3 | +import { AddRelationshipAction } from "@/entities/nodes/relationships/ui/add-relationship-action"; |
| 4 | +import { NodeCore } from "@/entities/nodes/types"; |
| 5 | +import { useSchema } from "@/entities/schema/ui/hooks/useSchema"; |
| 6 | +import { Button } from "@/shared/components/buttons/button-primitive"; |
| 7 | +import ErrorScreen from "@/shared/components/errors/error-screen"; |
| 8 | +import { Badge } from "@/shared/components/ui/badge"; |
| 9 | +import { |
| 10 | + Combobox, |
| 11 | + ComboboxContent, |
| 12 | + ComboboxEmpty, |
| 13 | + ComboboxItem, |
| 14 | + ComboboxList, |
| 15 | +} from "@/shared/components/ui/combobox"; |
| 16 | +import { PopoverTrigger } from "@/shared/components/ui/popover"; |
| 17 | +import { Spinner } from "@/shared/components/ui/spinner"; |
| 18 | +import { inputStyle } from "@/shared/components/ui/style"; |
| 19 | +import { classNames, debounce } from "@/shared/utils/common"; |
| 20 | +import { Icon } from "@iconify-icon/react"; |
| 21 | +import { PopoverTriggerProps } from "@radix-ui/react-popover"; |
| 22 | +import React, { forwardRef } from "react"; |
| 23 | + |
| 24 | +type PermissionNode = NodeCore & { identifier: { value: string } }; |
| 25 | + |
| 26 | +export interface PermissionComboboxProps extends Omit<PopoverTriggerProps, "value" | "onChange"> { |
| 27 | + value: PermissionNode[] | null; |
| 28 | + onChange: (value: PermissionNode[]) => void; |
| 29 | +} |
| 30 | + |
| 31 | +// This component is a temporary solution to display the permissions in a combobox |
| 32 | +// We cannot use relationship many because the general beheviour is to use hfid/display_label |
| 33 | +// On permission, label is it an attribute called identifier |
| 34 | +export function PermissionCombobox({ |
| 35 | + value, |
| 36 | + onChange, |
| 37 | + className, |
| 38 | + ...props |
| 39 | +}: PermissionComboboxProps) { |
| 40 | + const [open, setOpen] = React.useState(false); |
| 41 | + |
| 42 | + const handleSelect = (relationship: PermissionNode) => { |
| 43 | + onChange(value ? [...value, relationship] : [relationship]); |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <Combobox open={open} onOpenChange={setOpen}> |
| 48 | + <PopoverTrigger asChild> |
| 49 | + <div |
| 50 | + className={classNames( |
| 51 | + inputStyle, |
| 52 | + "has-[>:last-child:focus]:outline-none has-[>:last-child:focus]:ring-2 has-[>:last-child:focus]:ring-custom-blue-600/25 has-[>:last-child:focus]:border-custom-blue-600", |
| 53 | + "cursor-pointer", |
| 54 | + className |
| 55 | + )} |
| 56 | + > |
| 57 | + <div className="flex-grow flex flex-wrap gap-2"> |
| 58 | + {value?.map((node) => ( |
| 59 | + <Badge key={node.id} className="flex items-center gap-1 pr-0.5"> |
| 60 | + {node.identifier.value} |
| 61 | + |
| 62 | + <Button |
| 63 | + size="icon" |
| 64 | + variant="ghost" |
| 65 | + onClick={(e) => { |
| 66 | + e.stopPropagation(); |
| 67 | + onChange(value.filter((item) => item.id !== node.id)); |
| 68 | + }} |
| 69 | + className="text-gray-500 hover:text-gray-800 h-4 w-4" |
| 70 | + aria-label="Remove" |
| 71 | + data-testid="remove-option" |
| 72 | + > |
| 73 | + × |
| 74 | + </Button> |
| 75 | + </Badge> |
| 76 | + ))} |
| 77 | + </div> |
| 78 | + |
| 79 | + <button |
| 80 | + type="button" |
| 81 | + className="text-gray-600 outline-none w-3.5 h-3.5" |
| 82 | + onClick={() => setOpen(!open)} |
| 83 | + {...props} |
| 84 | + > |
| 85 | + <Icon icon="mdi:unfold-more-horizontal" /> |
| 86 | + </button> |
| 87 | + </div> |
| 88 | + </PopoverTrigger> |
| 89 | + |
| 90 | + <ComboboxContent> |
| 91 | + <PermissionComboboxList onSelect={handleSelect} value={value} /> |
| 92 | + <AddRelationshipAction peer={ACCOUNT_PERMISSION_OBJECT} onSuccess={handleSelect} /> |
| 93 | + </ComboboxContent> |
| 94 | + </Combobox> |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +export interface RelationshipComboboxListProps { |
| 99 | + value: PermissionNode[] | null; |
| 100 | + onSelect: (value: PermissionNode) => void; |
| 101 | +} |
| 102 | + |
| 103 | +export const PermissionComboboxList = forwardRef<HTMLDivElement, RelationshipComboboxListProps>( |
| 104 | + ({ value, onSelect }, ref) => { |
| 105 | + const [search, setSearch] = React.useState(""); |
| 106 | + const { schema } = useSchema(ACCOUNT_PERMISSION_OBJECT); |
| 107 | + const { isPending, data, error, fetchNextPage, hasNextPage, isFetchingNextPage } = useObjects({ |
| 108 | + schema: schema!, |
| 109 | + filters: search ? [{ name: "any__value", value: search }] : undefined, |
| 110 | + }); |
| 111 | + |
| 112 | + if (error) return <ErrorScreen message={error.message} />; |
| 113 | + |
| 114 | + const setSearchDebounced = debounce(setSearch, 300); |
| 115 | + |
| 116 | + return ( |
| 117 | + <ComboboxList |
| 118 | + ref={ref} |
| 119 | + onValueChange={(newValue) => setSearchDebounced(newValue)} |
| 120 | + shouldFilter={false} |
| 121 | + > |
| 122 | + {isPending ? ( |
| 123 | + <Spinner className="flex justify-center m-2" /> |
| 124 | + ) : ( |
| 125 | + <> |
| 126 | + <ComboboxEmpty>No {schema?.label ?? "results"} found</ComboboxEmpty> |
| 127 | + |
| 128 | + {data.pages.map((page) => { |
| 129 | + return page |
| 130 | + .filter((node) => !value?.some((v) => v.id === node.id)) |
| 131 | + .map((n) => { |
| 132 | + const node = n as unknown as PermissionNode; |
| 133 | + return ( |
| 134 | + <ComboboxItem |
| 135 | + key={node.id} |
| 136 | + value={node.id} |
| 137 | + selectedValue={null} |
| 138 | + onSelect={() => |
| 139 | + onSelect({ |
| 140 | + id: node.id, |
| 141 | + display_label: node.identifier.value, |
| 142 | + identifier: { value: node.identifier.value }, |
| 143 | + __typename: node.__typename, |
| 144 | + }) |
| 145 | + } |
| 146 | + > |
| 147 | + <span className="truncate">{node.identifier.value}</span> |
| 148 | + </ComboboxItem> |
| 149 | + ); |
| 150 | + }); |
| 151 | + })} |
| 152 | + </> |
| 153 | + )} |
| 154 | + |
| 155 | + {hasNextPage && ( |
| 156 | + <ComboboxItem |
| 157 | + value="Load more" |
| 158 | + onSelect={() => fetchNextPage()} |
| 159 | + disabled={!hasNextPage || isFetchingNextPage} |
| 160 | + className="justify-center text-custom-blue-700" |
| 161 | + > |
| 162 | + {isFetchingNextPage ? "Loading more..." : "Load more"} |
| 163 | + </ComboboxItem> |
| 164 | + )} |
| 165 | + </ComboboxList> |
| 166 | + ); |
| 167 | + } |
| 168 | +); |
0 commit comments