-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
stuff.svelte
133 lines (117 loc) · 4.55 KB
/
stuff.svelte
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<script lang="ts">
import { onMount } from "svelte";
import isDebugMode from "src/utils/debugMode";
import "./app.css";
import magicWand from "./lib//assets/magicWand.svg";
import loadingCircle from "./lib/assets/loadingCircle.svg";
let isLoading: boolean = false;
let responseStatus: number;
let isOnline: boolean;
let image: HTMLImageElement;
onMount(() => {
isOnline = checkInternetConnection();
});
const handleClick = () => {
parent.postMessage({ pluginMessage: { type: "clickPredictButton" } }, "*");
};
//Handle the demand from the sandbox API to make a network request when the order is recieved
window.onmessage = async (event) => {
if (event.data.pluginMessage.type === "networkRequest") {
if (isDebugMode) {
renderSelectedImageInPlugin(event);
}
try {
//const url = "https://figma-autoname-backend.herokuapp.com/api/predictNode" //Heroku
//const url = "https://figma-autoname-backend-dy9w4.ondigitalocean.app/api/predictNode"; //DigitalOcean
const url = "http://localhost:4001/api/predictNode"; //Localhost
const initObject = {
method: "POST",
headers: {
"Content-type": "application/json",
"x-api-key": "theBrownFox",
},
body: JSON.stringify({ imagesData: event.data.pluginMessage.data }),
};
isLoading = true;
const response = await fetch(url, initObject);
responseStatus = await response.status;
const responseJson = await response.json();
window.parent.postMessage(
{ pluginMessage: { type: "response", payload: responseJson } },
"*"
); //Close plugin only when the request end
isLoading = false;
} catch (error) {
console.log(`[Svelte]: Error: ${error.message}`);
isLoading = false;
}
}
};
function closePlugin(): void {
window.parent.postMessage({ pluginMessage: { type: "close" } }, "*");
}
function checkInternetConnection(): boolean {
let isOnline: boolean;
isOnline = navigator.onLine ? true : false;
console.log(
`[Svelte]: Connection status: ${isOnline ? "Online" : "Offline"}`
);
return isOnline;
}
function renderSelectedImageInPlugin(e): void {
let bytesFromFigma = e.data.pluginMessage.data[0].imageDataBytes;
const uintArray = Uint8Array.from(bytesFromFigma);
image.src = URL.createObjectURL(
new Blob([uintArray.buffer], { type: "image/png" })
);
}
</script>
<main class="flex flex-col items-center justify-between px-4 py-4 h-full bg-Black">
<title-container class="flex flex-col items-center w-full space-y-4">
<h1 class="text-base font-medium text-white text-center mt-2">
Select layers and press "Name"
</h1>
{#if isDebugMode}
<p class="text-gray-400 text-xs px-2 py-1 border-[1px] w-fit border-gray-400 rounded">
Debug mode
</p>
<p class="text-gray-400 justify-center text-xs">Image sent to the model 👇</p>
<img src="" alt="Pixels sent to the model" bind:this={image} class="rounded-md" />
{/if}
</title-container>
<body-container class="flex flex-col w-full items-center">
{#if responseStatus === 401}
<p class="text-xs text-white font-medium mb-4">Non authorized :/</p>
{:else if !isDebugMode}
<img src={magicWand} alt="Magic wand icon" class="mb-8" />
{/if}
{#if responseStatus > 250}
<button
class="w-full flex flex-row justify-center items-center bg-Blue px-3 py-[7px] text-xs text-white font-medium rounded-md"
on:click={closePlugin}
>
Close
</button>
{:else if !isOnline}
<button
class="w-full flex flex-row justify-center items-center bg-Blue px-3 py-[7px] text-xs cursor-not-allowed grayscale text-white font-medium rounded-md"
>
No connection :/
</button>
{:else}
<button
class="w-full flex flex-row justify-center items-center bg-Blue px-3 py-[7px] text-xs text-white font-medium rounded-md"
on:click={handleClick}
>
{#if isLoading}
<img
src={loadingCircle}
alt="Loading circle"
class="animate-spin mr-2"
/>
{/if}
{isLoading ? `Processing...` : `Name`}
</button>
{/if}
</body-container>
</main>