forked from Stability-AI/StableStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
62 lines (49 loc) · 1.58 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Generation } from "~/Generation";
import { Modal } from "./Modal";
export declare namespace Download {
export { Modal };
}
export namespace Download {
Download.Modal = Modal;
export const fileName = (input: Generation.Image.Input) => {
const promptText = input.prompts.map(({ text }) => text).join(" ");
const promptTrimmed = (promptText ?? "").slice(0, 50);
const model = input.model
.replace("stable-diffusion-", "")
.replace("stable-", "");
const seed = input.seed;
return `${seed}_${promptTrimmed}_${model}.png`;
};
export const execute = async (
image: Generation.Image,
input: Generation.Image.Input,
name?: string
) => {
const href = await Generation.Image.blobURL(image);
if (!href) return;
const a = document.createElement("a");
a.href = href;
a.download = name ?? fileName(input);
a.click();
};
export const use = (image?: Generation.Image) => {
const { image: modalImage, fileName, setImage } = Modal.State.use();
return useCallback(
async (overrideImage?: Generation.Image) => {
if (overrideImage) {
const input = Generation.Image.Input.get(overrideImage.inputID);
if (input) return execute(overrideImage, input);
}
if (!image) return;
const input = Generation.Image.Input.get(image.inputID);
if (!input) return;
if (image === modalImage) {
await execute(image, input, fileName);
} else {
setImage(image);
}
},
[fileName, image, modalImage, setImage]
);
};
}