Skip to content

Commit 170d542

Browse files
committed
Enhance OpenAI integration: Add support for loading models based on selected credentials, update UI components for better user experience, and improve localization for new features. Refactor form handling and state management in OpenaiForm and CredentialsOpenai components.
1 parent 4fb8b07 commit 170d542

File tree

9 files changed

+191
-87
lines changed

9 files changed

+191
-87
lines changed

src/components/ui/form.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,14 @@ const FormSelect = <
292292
required,
293293
options,
294294
placeholder,
295+
disabled,
295296
...props
296297
}: Omit<ControllerProps<TFieldValues, TName>, "render"> & {
297298
label?: string;
298299
required?: boolean;
299300
helper?: string;
300301
placeholder?: string;
302+
disabled?: boolean;
301303
options: { value: string; label: string }[];
302304
}) => {
303305
return (
@@ -313,7 +315,11 @@ const FormSelect = <
313315
</FormLabel>
314316
)}
315317
<FormControl>
316-
<Select onValueChange={field.onChange} defaultValue={field.value}>
318+
<Select
319+
onValueChange={field.onChange}
320+
defaultValue={field.value}
321+
disabled={disabled}
322+
>
317323
<FormControl>
318324
<SelectTrigger>
319325
<SelectValue placeholder={placeholder} />

src/components/ui/select.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const SelectTrigger = React.forwardRef<
1717
<SelectPrimitive.Trigger
1818
ref={ref}
1919
className={cn(
20-
"flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
20+
"flex h-10 w-full items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-default disabled:opacity-50 [&>span]:line-clamp-1",
2121
className
2222
)}
2323
{...props}

src/lib/queries/openai/getModels.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { GetModelsResponse } from "./types";
66

77
interface IParams {
88
instanceName: string;
9+
openaiCredsId?: string;
910
token?: string | null;
1011
}
1112

@@ -15,26 +16,34 @@ const queryKey = (params: Partial<IParams>) => [
1516
JSON.stringify(params),
1617
];
1718

18-
export const getModels = async ({ instanceName, token }: IParams) => {
19+
export const getModels = async ({
20+
instanceName,
21+
openaiCredsId,
22+
token,
23+
}: IParams) => {
24+
const params = openaiCredsId ? { openaiCredsId } : {};
25+
1926
const response = await api.get(`/openai/getModels/${instanceName}`, {
2027
headers: { apiKey: token },
28+
params,
2129
});
2230
return response.data;
2331
};
2432

2533
export const useGetModels = (
2634
props: UseQueryParams<GetModelsResponse> & Partial<IParams>,
2735
) => {
28-
const { instanceName, token, ...rest } = props;
36+
const { instanceName, openaiCredsId, token, ...rest } = props;
2937
return useQuery<GetModelsResponse>({
3038
staleTime: 1000 * 60 * 60 * 6, // 6 hours
3139
...rest,
32-
queryKey: queryKey({ instanceName }),
40+
queryKey: queryKey({ instanceName, openaiCredsId }),
3341
queryFn: () =>
3442
getModels({
3543
instanceName: instanceName!,
44+
openaiCredsId,
3645
token,
3746
}),
38-
enabled: !!instanceName && (props.enabled ?? true),
47+
enabled: !!instanceName && !!openaiCredsId && (props.enabled ?? true),
3948
});
4049
};

src/pages/instance/Openai/CredentialsOpenai.tsx

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { zodResolver } from "@hookform/resolvers/zod";
33
import { ColumnDef, SortingState } from "@tanstack/react-table";
4-
import { ArrowUpDown, Lock, MoreHorizontal } from "lucide-react";
5-
import { useState } from "react";
4+
import { ArrowUpDown, Lock, Plus, MoreHorizontal } from "lucide-react";
5+
import React, { useState } from "react";
66
import { useForm, FormProvider } from "react-hook-form";
77
import { useTranslation } from "react-i18next";
88
import { toast } from "react-toastify";
@@ -42,14 +42,22 @@ const FormSchema = z.object({
4242
apiKey: z.string(),
4343
});
4444

45-
function CredentialsOpenai() {
45+
interface CredentialsOpenaiProps {
46+
onCredentialsUpdate?: () => void;
47+
showText?: boolean;
48+
}
49+
50+
function CredentialsOpenai({
51+
onCredentialsUpdate,
52+
showText = true,
53+
}: CredentialsOpenaiProps) {
4654
const { t } = useTranslation();
4755
const { instance } = useInstance();
4856

4957
const { createOpenaiCreds, deleteOpenaiCreds } = useManageOpenai();
5058
const [open, setOpen] = useState(false);
5159
const [sorting, setSorting] = useState<SortingState>([]);
52-
const { data: creds, refetch: refetchCreds } = useFindOpenaiCreds({
60+
const { data: creds } = useFindOpenaiCreds({
5361
instanceName: instance?.name,
5462
enabled: open,
5563
});
@@ -79,19 +87,17 @@ function CredentialsOpenai() {
7987
data: credsData,
8088
});
8189
toast.success(t("openai.toast.success.credentialsCreate"));
82-
onReset();
90+
form.reset();
91+
if (onCredentialsUpdate) {
92+
onCredentialsUpdate();
93+
}
8394
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8495
} catch (error: any) {
8596
console.error("Error:", error);
8697
toast.error(`Error: ${error?.response?.data?.response?.message}`);
8798
}
8899
};
89100

90-
function onReset() {
91-
form.reset();
92-
refetchCreds();
93-
}
94-
95101
const handleDelete = async (id: string) => {
96102
if (!instance?.name) {
97103
toast.error("Instance not found.");
@@ -103,7 +109,9 @@ function CredentialsOpenai() {
103109
instanceName: instance?.name,
104110
});
105111
toast.success(t("openai.toast.success.credentialsDelete"));
106-
refetchCreds();
112+
if (onCredentialsUpdate) {
113+
onCredentialsUpdate();
114+
}
107115
} catch (error: any) {
108116
console.error("Error:", error);
109117
toast.error(`Error: ${error?.response?.data?.response?.message}`);
@@ -171,45 +179,58 @@ function CredentialsOpenai() {
171179
return (
172180
<Dialog open={open} onOpenChange={setOpen}>
173181
<DialogTrigger asChild>
174-
<Button variant="secondary" size="sm">
175-
<Lock size={16} className="mr-1" />
176-
<span className="hidden md:inline">
177-
{t("openai.credentials.title")}
178-
</span>
182+
<Button variant="secondary" size="sm" type="button">
183+
{showText ? (
184+
<>
185+
<Lock size={16} className="mr-1" />
186+
<span className="hidden md:inline">
187+
{t("openai.credentials.title")}
188+
</span>
189+
</>
190+
) : (
191+
<Plus size={16} />
192+
)}
179193
</Button>
180194
</DialogTrigger>
181-
<DialogContent
182-
className="overflow-y-auto sm:max-h-[600px] sm:max-w-[740px]"
183-
onCloseAutoFocus={onReset}
184-
>
195+
<DialogContent className="overflow-y-auto sm:max-h-[600px] sm:max-w-[740px]">
185196
<DialogHeader>
186197
<DialogTitle>{t("openai.credentials.title")}</DialogTitle>
187198
</DialogHeader>
188199
<FormProvider {...form}>
189-
<form
190-
onSubmit={form.handleSubmit(onSubmit)}
191-
className="w-full space-y-6"
200+
<div
201+
onClick={(e) => e.stopPropagation()}
202+
onSubmit={(e) => e.stopPropagation()}
203+
onKeyDown={(e) => e.stopPropagation()}
192204
>
193-
<div>
194-
<div className="grid gap-3 md:grid-cols-2">
195-
<FormInput
196-
name="name"
197-
label={t("openai.credentials.table.name")}
198-
>
199-
<Input />
200-
</FormInput>
201-
<FormInput
202-
name="apiKey"
203-
label={t("openai.credentials.table.apiKey")}
204-
>
205-
<Input type="password" />
206-
</FormInput>
205+
<form
206+
onSubmit={(e) => {
207+
e.preventDefault();
208+
e.stopPropagation();
209+
form.handleSubmit(onSubmit)(e);
210+
}}
211+
className="w-full space-y-6"
212+
>
213+
<div>
214+
<div className="grid gap-3 md:grid-cols-2">
215+
<FormInput
216+
name="name"
217+
label={t("openai.credentials.table.name")}
218+
>
219+
<Input />
220+
</FormInput>
221+
<FormInput
222+
name="apiKey"
223+
label={t("openai.credentials.table.apiKey")}
224+
>
225+
<Input type="password" />
226+
</FormInput>
227+
</div>
207228
</div>
208-
</div>
209-
<DialogFooter>
210-
<Button type="submit">{t("openai.button.save")}</Button>
211-
</DialogFooter>
212-
</form>
229+
<DialogFooter>
230+
<Button type="submit">{t("openai.button.save")}</Button>
231+
</DialogFooter>
232+
</form>
233+
</div>
213234
</FormProvider>
214235
<Separator />
215236
<div>

0 commit comments

Comments
 (0)