forked from pinks/eris
1
0
Fork 0
eris/ui/SettingsPage.tsx

276 lines
8.7 KiB
TypeScript
Raw Normal View History

2023-10-05 09:00:51 +00:00
import { cx } from "@twind/core";
import React, { ReactNode, useState } from "react";
import useSWR from "swr";
2023-10-08 21:23:54 +00:00
import { fetchApi, handleResponse } from "./apiClient.tsx";
2023-10-05 09:00:51 +00:00
export function SettingsPage(props: { sessionId: string }) {
const { sessionId } = props;
const session = useSWR(
2023-10-08 21:23:54 +00:00
sessionId ? ["sessions/{sessionId}", "GET", { params: { sessionId } }] as const : null,
(args) => fetchApi(...args).then(handleResponse),
2023-10-05 09:00:51 +00:00
);
const user = useSWR(
session.data?.userId
2023-10-08 21:23:54 +00:00
? ["users/{userId}", "GET", { params: { userId: String(session.data.userId) } }] as const
2023-10-05 09:00:51 +00:00
: null,
2023-10-08 21:23:54 +00:00
(args) => fetchApi(...args).then(handleResponse),
2023-10-05 09:00:51 +00:00
);
const params = useSWR(
["settings/params", "GET", {}] as const,
2023-10-08 21:23:54 +00:00
(args) => fetchApi(...args).then(handleResponse),
2023-10-05 09:00:51 +00:00
);
const [changedParams, setChangedParams] = useState<Partial<typeof params.data>>({});
const [error, setError] = useState<string>();
return (
<form
className="flex flex-col items-stretch gap-4"
onSubmit={(e) => {
e.preventDefault();
params.mutate(() =>
2023-10-08 21:23:54 +00:00
fetchApi("settings/params", "PATCH", {
2023-10-05 09:00:51 +00:00
query: { sessionId },
2023-10-08 21:23:54 +00:00
body: { type: "application/json", data: changedParams ?? {} },
2023-10-05 09:00:51 +00:00
}).then(handleResponse)
)
.then(() => setChangedParams({}))
.catch((e) => setError(String(e)));
}}
>
<label className="flex flex-col items-stretch gap-1">
<span className="text-sm">
Negative prompt {changedParams?.negative_prompt != null ? "(Changed)" : ""}
</span>
<textarea
className="input-text"
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.negative_prompt ??
params.data?.negative_prompt ??
""}
onChange={(e) =>
setChangedParams((params) => ({
...params,
negative_prompt: e.target.value,
}))}
/>
</label>
2023-10-10 16:21:46 +00:00
<label className="flex flex-col items-stretch gap-1">
<span className="text-sm">
Sampler {changedParams?.sampler_name != null ? "(Changed)" : ""}
</span>
<input
className="input-text"
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.sampler_name ??
params.data?.sampler_name ??
""}
onChange={(e) =>
setChangedParams((params) => ({
...params,
sampler_name: e.target.value,
}))}
/>
</label>
2023-10-05 09:00:51 +00:00
<label className="flex flex-col items-stretch gap-1">
<span className="text-sm">
Steps {changedParams?.steps != null ? "(Changed)" : ""}
</span>
<span className="flex items-center gap-1">
<input
className="input-text w-20"
type="number"
min={5}
max={50}
step={5}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.steps ??
params.data?.steps ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
steps: Number(e.target.value),
}))}
/>
<input
className="input-range flex-grow"
type="range"
min={5}
max={50}
step={5}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.steps ??
params.data?.steps ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
steps: Number(e.target.value),
}))}
/>
</span>
2023-10-10 16:21:46 +00:00
span
2023-10-05 09:00:51 +00:00
</label>
<label className="flex flex-col items-stretch gap-1">
<span className="text-sm">
Detail {changedParams?.cfg_scale != null ? "(Changed)" : ""}
</span>
<span className="flex items-center gap-1">
<input
className="input-text w-20"
type="number"
min={1}
max={20}
step={1}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.cfg_scale ??
params.data?.cfg_scale ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
cfg_scale: Number(e.target.value),
}))}
/>
<input
className="input-range flex-grow"
type="range"
min={1}
max={20}
step={1}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.cfg_scale ??
params.data?.cfg_scale ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
cfg_scale: Number(e.target.value),
}))}
/>
</span>
</label>
<div className="flex gap-4">
<label className="flex flex-1 flex-col items-stretch gap-1">
<span className="text-sm">
Width {changedParams?.width != null ? "(Changed)" : ""}
</span>
<input
className="input-text"
type="number"
min={64}
max={2048}
step={64}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.width ??
params.data?.width ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
width: Number(e.target.value),
}))}
/>
<input
className="input-range"
type="range"
min={64}
max={2048}
step={64}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.width ??
params.data?.width ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
width: Number(e.target.value),
}))}
/>
</label>
<label className="flex flex-1 flex-col items-stretch gap-1">
<span className="text-sm">
Height {changedParams?.height != null ? "(Changed)" : ""}
</span>
<input
className="input-text"
type="number"
min={64}
max={2048}
step={64}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.height ??
params.data?.height ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
height: Number(e.target.value),
}))}
/>
<input
className="input-range"
type="range"
min={64}
max={2048}
step={64}
disabled={params.isLoading || !user.data?.isAdmin}
value={changedParams?.height ??
params.data?.height ??
0}
onChange={(e) =>
setChangedParams((params) => ({
...params,
height: Number(e.target.value),
}))}
/>
</label>
</div>
{error ? <Alert onClose={() => setError(undefined)}>{error}</Alert> : null}
{params.error ? <Alert>{params.error.message}</Alert> : null}
<div className="flex gap-2 items-center justify-end">
<button
type="button"
className={cx("button-outlined ripple", params.isLoading && "bg-stripes")}
disabled={params.isLoading || !user.data?.isAdmin ||
Object.keys(changedParams ?? {}).length === 0}
onClick={() => setChangedParams({})}
>
Reset
</button>
<button
type="submit"
className={cx("button-filled ripple", params.isLoading && "bg-stripes")}
disabled={params.isLoading || !user.data?.isAdmin ||
Object.keys(changedParams ?? {}).length === 0}
>
Save
</button>
</div>
</form>
);
}
function Alert(props: { children: ReactNode; onClose?: () => void }) {
const { children, onClose } = props;
return (
<p
role="alert"
className="px-4 py-2 flex gap-2 items-center bg-red-500 text-white rounded-sm shadow-md"
>
<span className="flex-grow">{children}</span>
{onClose
? (
<button
type="button"
className="button-ghost"
onClick={() => onClose()}
>
Close
</button>
)
: null}
</p>
);
}