|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Label, |
| 4 | + Tooltip, |
| 5 | + TooltipInfoButton, |
| 6 | + TooltipTrigger, |
| 7 | +} from '@stacklok/ui-kit' |
| 8 | +import { useFieldArray, useFormContext } from 'react-hook-form' |
| 9 | +import { |
| 10 | + WORKSPACE_CONFIG_FIELD_NAME, |
| 11 | + WorkspaceConfigFieldValues, |
| 12 | +} from '../lib/schema-mux' |
| 13 | +import { MuxMatcherType } from '@/api/generated' |
| 14 | +import { uniqueId } from 'lodash' |
| 15 | +import { Plus } from '@untitled-ui/icons-react' |
| 16 | +import { tv } from 'tailwind-variants' |
| 17 | +import { |
| 18 | + SortableContext, |
| 19 | + sortableKeyboardCoordinates, |
| 20 | + verticalListSortingStrategy, |
| 21 | +} from '@dnd-kit/sortable' |
| 22 | +import { |
| 23 | + closestCenter, |
| 24 | + DndContext, |
| 25 | + DragEndEvent, |
| 26 | + KeyboardSensor, |
| 27 | + PointerSensor, |
| 28 | + UniqueIdentifier, |
| 29 | + useSensor, |
| 30 | + useSensors, |
| 31 | +} from '@dnd-kit/core' |
| 32 | +import { ReactNode, useCallback } from 'react' |
| 33 | +import { FormMuxRuleRow } from './form-mux-rule-row' |
| 34 | +import { DndSortProvider } from './form-mux-dnd-provider' |
| 35 | + |
| 36 | +function getIndicesOnDragEnd<T extends { id: UniqueIdentifier }>( |
| 37 | + event: DragEndEvent, |
| 38 | + items: T[] |
| 39 | +): { |
| 40 | + from: number |
| 41 | + to: number |
| 42 | +} | null { |
| 43 | + const { active, over } = event |
| 44 | + |
| 45 | + if (over == null || active.id || over.id) return null // no-op |
| 46 | + |
| 47 | + const from = items.findIndex(({ id }) => id === active.id) |
| 48 | + const to = items.findIndex(({ id }) => id === over.id) |
| 49 | + |
| 50 | + return { |
| 51 | + from, |
| 52 | + to, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +const gridStyles = tv({ |
| 57 | + base: 'grid grid-cols-[2fr_1fr_2.5rem] items-center gap-2', |
| 58 | +}) |
| 59 | + |
| 60 | +function Labels() { |
| 61 | + return ( |
| 62 | + <div className={gridStyles()}> |
| 63 | + <Label className="flex items-center gap-1"> |
| 64 | + Filter by |
| 65 | + <TooltipTrigger delay={0}> |
| 66 | + <TooltipInfoButton aria-label="Filter by description" /> |
| 67 | + <Tooltip placement="right" className="max-w-72 text-balance"> |
| 68 | + Filters are applied in top-down order. The first rule that matches |
| 69 | + each prompt determines the chosen model. An empty filter applies to |
| 70 | + all prompts. |
| 71 | + </Tooltip> |
| 72 | + </TooltipTrigger> |
| 73 | + </Label> |
| 74 | + <Label>Preferred model</Label> |
| 75 | + </div> |
| 76 | + ) |
| 77 | +} |
| 78 | + |
| 79 | +export function FormMuxFieldsArray() { |
| 80 | + const { control } = useFormContext<WorkspaceConfigFieldValues>() |
| 81 | + |
| 82 | + const { fields, swap, prepend } = useFieldArray<WorkspaceConfigFieldValues>({ |
| 83 | + control, |
| 84 | + name: WORKSPACE_CONFIG_FIELD_NAME.muxing_rules, |
| 85 | + }) |
| 86 | + console.debug('👉 fields:', fields) |
| 87 | + |
| 88 | + const onDragEnd = useCallback( |
| 89 | + (event: DragEndEvent) => { |
| 90 | + const { from, to } = getIndicesOnDragEnd(event, fields) || {} |
| 91 | + if (from && to) swap(from, to) |
| 92 | + }, |
| 93 | + [fields, swap] |
| 94 | + ) |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <Labels /> |
| 99 | + <DndSortProvider key={fields.length} items={fields} onDragEnd={onDragEnd}> |
| 100 | + <ul> |
| 101 | + {fields.map((item, index) => { |
| 102 | + console.debug('👉 MAPPED item:', item) |
| 103 | + return <FormMuxRuleRow index={index} key={item.id} item={item} /> |
| 104 | + })} |
| 105 | + </ul> |
| 106 | + </DndSortProvider> |
| 107 | + |
| 108 | + <div className="flex gap-2"> |
| 109 | + <Button |
| 110 | + className="w-fit" |
| 111 | + variant="tertiary" |
| 112 | + onPress={() => |
| 113 | + prepend({ |
| 114 | + id: uniqueId(), |
| 115 | + model: undefined, |
| 116 | + matcher: '', |
| 117 | + matcher_type: MuxMatcherType.FILENAME_MATCH, |
| 118 | + }) |
| 119 | + } |
| 120 | + // isDisabled={isArchived} |
| 121 | + > |
| 122 | + <Plus /> Add Filter |
| 123 | + </Button> |
| 124 | + |
| 125 | + {/* <LinkButton className="w-fit" variant="tertiary" href="/providers"> |
| 126 | + <LayersThree01 /> Manage providers |
| 127 | + </LinkButton> */} |
| 128 | + </div> |
| 129 | + </> |
| 130 | + ) |
| 131 | +} |
0 commit comments