Skip to content

Commit 5a89523

Browse files
committed
refactor: review
1 parent 12a1691 commit 5a89523

File tree

3 files changed

+40
-52
lines changed

3 files changed

+40
-52
lines changed

src/components/SortableArea.tsx

+29-28
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,43 @@ import {
77
UniqueIdentifier,
88
useSensor,
99
useSensors,
10-
} from "@dnd-kit/core";
10+
} from '@dnd-kit/core'
1111
import {
1212
arrayMove,
1313
SortableContext,
1414
sortableKeyboardCoordinates,
1515
verticalListSortingStrategy,
16-
} from "@dnd-kit/sortable";
17-
import { CSS } from "@dnd-kit/utilities";
18-
import { useSortable } from "@dnd-kit/sortable";
19-
import { Drag } from "./icons";
16+
} from '@dnd-kit/sortable'
17+
import { CSS } from '@dnd-kit/utilities'
18+
import { useSortable } from '@dnd-kit/sortable'
19+
import { Drag } from './icons'
2020

2121
type Props<T> = {
22-
children: (item: T, index: number) => React.ReactNode;
23-
setItems: (items: T[]) => void;
24-
items: T[];
25-
disableDragByIndex?: number;
26-
};
22+
children: (item: T, index: number) => React.ReactNode
23+
setItems: (items: T[]) => void
24+
items: T[]
25+
disableDragByIndex?: number
26+
}
2727

2828
function ItemWrapper({
2929
children,
3030
id,
31-
disabledDrag,
31+
hasDragDisabled,
3232
}: {
33-
children: React.ReactNode;
34-
id: UniqueIdentifier;
35-
disabledDrag: boolean;
33+
children: React.ReactNode
34+
id: UniqueIdentifier
35+
hasDragDisabled: boolean
3636
}) {
3737
const { attributes, listeners, setNodeRef, transform, transition } =
38-
useSortable({ id });
38+
useSortable({ id })
3939
const style = {
4040
transform: CSS.Transform.toString(transform),
4141
transition,
42-
};
42+
}
4343

4444
return (
45-
<div style={style} className="flex items-center w-full">
46-
{disabledDrag ? (
45+
<div style={style} className="flex w-full items-center">
46+
{hasDragDisabled ? (
4747
<div className="size-8" />
4848
) : (
4949
<div ref={setNodeRef} {...attributes} {...listeners} className="size-8">
@@ -52,7 +52,7 @@ function ItemWrapper({
5252
)}
5353
<div className="grow">{children}</div>
5454
</div>
55-
);
55+
)
5656
}
5757

5858
export function SortableArea<T extends { id: UniqueIdentifier }>({
@@ -65,21 +65,22 @@ export function SortableArea<T extends { id: UniqueIdentifier }>({
6565
useSensor(PointerSensor),
6666
useSensor(KeyboardSensor, {
6767
coordinateGetter: sortableKeyboardCoordinates,
68-
}),
69-
);
68+
})
69+
)
7070

7171
function handleDragEnd(event: DragEndEvent) {
72-
const { active, over } = event;
72+
const { active, over } = event
7373

7474
if (over == null) {
75-
return;
75+
// The item was dropped in it's original place
76+
return
7677
}
7778

7879
if (active.id !== over.id) {
79-
const oldIndex = items.findIndex(({ id }) => id === active.id);
80-
const newIndex = items.findIndex(({ id }) => id === over.id);
80+
const oldIndex = items.findIndex(({ id }) => id === active.id)
81+
const newIndex = items.findIndex(({ id }) => id === over.id)
8182

82-
setItems(arrayMove(items, oldIndex, newIndex));
83+
setItems(arrayMove(items, oldIndex, newIndex))
8384
}
8485
}
8586

@@ -94,12 +95,12 @@ export function SortableArea<T extends { id: UniqueIdentifier }>({
9495
<ItemWrapper
9596
key={index}
9697
id={item.id}
97-
disabledDrag={disableDragByIndex === index}
98+
hasDragDisabled={disableDragByIndex === index}
9899
>
99100
{children(item, index)}
100101
</ItemWrapper>
101102
))}
102103
</SortableContext>
103104
</DndContext>
104-
);
105+
)
105106
}

src/features/workspace/components/workspace-models-dropdown.tsx

+10-23
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
OptionsSchema,
1515
} from '@stacklok/ui-kit'
1616
import { ChevronDown, SearchMd } from '@untitled-ui/icons-react'
17+
import { map, groupBy } from 'lodash'
1718
import { useState } from 'react'
1819

1920
type Props = {
@@ -32,28 +33,14 @@ type Props = {
3233
function groupModelsByProviderName(
3334
models: ModelByProvider[]
3435
): OptionsSchema<'listbox', string>[] {
35-
return models.reduce<OptionsSchema<'listbox', string>[]>(
36-
(groupedProviders, item) => {
37-
const providerData = groupedProviders.find(
38-
(group) => group.id === item.provider_name
39-
)
40-
if (!providerData) {
41-
groupedProviders.push({
42-
id: item.provider_name,
43-
items: [],
44-
textValue: item.provider_name,
45-
})
46-
}
47-
48-
;(providerData?.items ?? []).push({
49-
id: `${item.provider_id}/${item.name}`,
50-
textValue: item.name,
51-
})
52-
53-
return groupedProviders
54-
},
55-
[]
56-
)
36+
return map(groupBy(models, 'provider_name'), (items, providerName) => ({
37+
id: providerName,
38+
textValue: providerName,
39+
items: items.map((item) => ({
40+
id: `${item.provider_id}/${item.name}`,
41+
textValue: item.name,
42+
})),
43+
}))
5744
}
5845

5946
function filterModels({
@@ -95,7 +82,7 @@ export function WorkspaceModelsDropdown({
9582
currentProvider && rule.model
9683
? `${currentProvider?.provider_name}/${rule.model}`
9784
: ''
98-
const selectedKey = `${rule.provider_id}_${rule.model}`
85+
const selectedKey = `${rule.provider_id}/${rule.model}`
9986

10087
return (
10188
<div className="flex w-full">

src/features/workspace/components/workspace-muxing-model.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function SortableItem({
7777
isArchived,
7878
isDefaultRule,
7979
}: SortableItemProps) {
80-
const placeholder = isDefaultRule ? 'Catch All' : 'eg file type, file name'
80+
const placeholder = isDefaultRule ? 'Catch-all' : 'e.g. file type, file name'
8181
return (
8282
<div className="flex items-center gap-2" key={rule.id}>
8383
<div className="flex w-full justify-between">

0 commit comments

Comments
 (0)