|
| 1 | +import { datasetSchemaTypeGroups } from '@/config'; |
| 2 | +import { cn } from '@/lib/utils'; |
| 3 | +import { SelectLabel } from '@radix-ui/react-select'; |
| 4 | +import { ChevronDown, Plus, SlidersHorizontal, X } from 'lucide-react'; |
| 5 | +import { useState } from 'react'; |
| 6 | +import { Button } from '@/components/ui/button'; |
| 7 | +import { Input } from '@/components/ui/input'; |
| 8 | +import { |
| 9 | + Select, |
| 10 | + SelectContent, |
| 11 | + SelectGroup, |
| 12 | + SelectItem, |
| 13 | + SelectTrigger, |
| 14 | + SelectValue, |
| 15 | +} from '@/components/ui/select'; |
| 16 | +import { borderTypeColor } from './borderTypeColor'; |
| 17 | +import { SchemaFilter } from './schemaFilters'; |
| 18 | + |
| 19 | +export interface SchemaSearchProps { |
| 20 | + filters: SchemaFilter[]; |
| 21 | + onAddFilter: (filter: SchemaFilter) => void; |
| 22 | + onRemoveFilter: (index: number) => void; |
| 23 | + onClearAllFilters?: () => void; |
| 24 | + isOpen: boolean; |
| 25 | + setIsOpen: (open: boolean) => void; |
| 26 | +} |
| 27 | + |
| 28 | +export function SchemaSearch({ |
| 29 | + filters, |
| 30 | + onAddFilter, |
| 31 | + onRemoveFilter, |
| 32 | + onClearAllFilters, |
| 33 | + isOpen, |
| 34 | + setIsOpen, |
| 35 | +}: SchemaSearchProps) { |
| 36 | + const [inputPathValue, setInputPathValue] = useState(''); |
| 37 | + const [selectedType, setSelectedType] = useState<string>(''); |
| 38 | + |
| 39 | + const handleAdd = () => { |
| 40 | + if (!inputPathValue.trim() || !selectedType) return; |
| 41 | + onAddFilter({ path: inputPathValue.trim(), type: selectedType }); |
| 42 | + setInputPathValue(''); |
| 43 | + setSelectedType(''); |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="rounded-2xl border border-[#303038]"> |
| 48 | + <button |
| 49 | + className={cn('flex w-full items-center gap-2 px-10 py-6 duration-300')} |
| 50 | + onClick={() => setIsOpen(!isOpen)} |
| 51 | + > |
| 52 | + <SlidersHorizontal size={16} /> |
| 53 | + <p className="flex-1 text-left font-bold"> |
| 54 | + Schema Search{' '} |
| 55 | + <span className="text-muted-foreground font-normal"> |
| 56 | + Add field types to filter datasets by their schema structure. |
| 57 | + Filters are applied automatically. |
| 58 | + </span> |
| 59 | + </p> |
| 60 | + <ChevronDown |
| 61 | + className={cn( |
| 62 | + 'ml-auto transition-transform', |
| 63 | + isOpen && '-rotate-180' |
| 64 | + )} |
| 65 | + /> |
| 66 | + </button> |
| 67 | + <div |
| 68 | + className={cn( |
| 69 | + 'grid transition-all duration-300', |
| 70 | + isOpen |
| 71 | + ? 'translate-y-0 grid-rows-[1fr]' |
| 72 | + : 'translate-y-2 grid-rows-[0fr]' |
| 73 | + )} |
| 74 | + > |
| 75 | + <div className={cn('text-grey-200 grid overflow-hidden px-6 md:px-10')}> |
| 76 | + <hr className="border-secondary" /> |
| 77 | + <div |
| 78 | + className={cn( |
| 79 | + 'grid transition-all duration-300', |
| 80 | + filters.length > 0 |
| 81 | + ? 'mt-6 translate-y-0 grid-rows-[1fr]' |
| 82 | + : 'translate-y-2 grid-rows-[0fr]' |
| 83 | + )} |
| 84 | + > |
| 85 | + <div className="flex flex-wrap items-center gap-2.5 overflow-hidden"> |
| 86 | + Filter by field types:{' '} |
| 87 | + {filters.map((schema, index) => { |
| 88 | + const borderColor = borderTypeColor.find((color) => |
| 89 | + color.keywords.some((keyword) => |
| 90 | + schema.type?.toLowerCase().includes(keyword) |
| 91 | + ) |
| 92 | + )?.color; |
| 93 | + return ( |
| 94 | + <span |
| 95 | + key={index} |
| 96 | + className={cn( |
| 97 | + 'inline-flex w-fit items-center rounded-full border px-4 py-2 text-xs', |
| 98 | + borderColor |
| 99 | + )} |
| 100 | + > |
| 101 | + <span className={cn('inline-block')}>{schema.path}</span> |
| 102 | + <span className={cn('inline-block')}>: {schema.type}</span> |
| 103 | + <button onClick={() => onRemoveFilter(index)}> |
| 104 | + <X className="ml-1 text-white" size={12} /> |
| 105 | + </button> |
| 106 | + </span> |
| 107 | + ); |
| 108 | + })} |
| 109 | + {filters.length > 0 && ( |
| 110 | + <button |
| 111 | + className="text-xs text-white" |
| 112 | + type="button" |
| 113 | + onClick={onClearAllFilters} |
| 114 | + > |
| 115 | + Clear all |
| 116 | + </button> |
| 117 | + )} |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + <div className="mt-6 mb-6 flex translate-y-1 flex-col items-center gap-4 sm:flex-row"> |
| 121 | + <Input |
| 122 | + type="text" |
| 123 | + id="schema-path" |
| 124 | + autoComplete="off" |
| 125 | + autoCapitalize="off" |
| 126 | + value={inputPathValue} |
| 127 | + onChange={(e) => setInputPathValue(e.target.value)} |
| 128 | + className={cn( |
| 129 | + 'bg-muted border-secondary col-span-2 w-full rounded-2xl px-4 py-6 text-sm text-white' |
| 130 | + )} |
| 131 | + placeholder="Field path (e.g email, telegram_chatId, nested)" |
| 132 | + /> |
| 133 | + |
| 134 | + <Select value={selectedType} onValueChange={setSelectedType}> |
| 135 | + <SelectTrigger className="bg-muted border-secondary w-full rounded-2xl px-4 py-6 text-sm text-white sm:max-w-1/3"> |
| 136 | + <SelectValue placeholder="Select type" /> |
| 137 | + </SelectTrigger> |
| 138 | + <SelectContent className="bg-muted border-secondary overflow-visible p-6"> |
| 139 | + {datasetSchemaTypeGroups.map((group) => ( |
| 140 | + <SelectGroup |
| 141 | + key={group.label} |
| 142 | + className="overflow-visible not-first:mt-2" |
| 143 | + > |
| 144 | + <SelectLabel className="text-grey-300 text-sm"> |
| 145 | + {group.label} |
| 146 | + </SelectLabel> |
| 147 | + {group.items.map((type) => ( |
| 148 | + <SelectItem |
| 149 | + key={type.value} |
| 150 | + className="text-base font-bold data-[state=checked]:text-yellow-400" |
| 151 | + value={type.value} |
| 152 | + > |
| 153 | + {type.value} |
| 154 | + </SelectItem> |
| 155 | + ))} |
| 156 | + </SelectGroup> |
| 157 | + ))} |
| 158 | + </SelectContent> |
| 159 | + </Select> |
| 160 | + <Button |
| 161 | + size="lg" |
| 162 | + onClick={handleAdd} |
| 163 | + disabled={!inputPathValue.trim() || !selectedType} |
| 164 | + > |
| 165 | + Add filter |
| 166 | + <Plus size={20} /> |
| 167 | + </Button> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + ); |
| 173 | +} |
0 commit comments