-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.ts
217 lines (190 loc) · 4.9 KB
/
code.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
const { getScale, changeScale } = (() => {
const scales = [0.5, 1, 2];
let currentScale = 1;
function getScale() {
return currentScale;
}
function changeScale() {
const currentIndex = scales.findIndex((item) => item === currentScale);
const nextIndex = currentIndex + 1;
currentScale = nextIndex === scales.length ? scales[0] : scales[nextIndex];
return currentScale;
}
return { getScale, changeScale };
})();
const { showNotify, closeNotify } = (() => {
let notify: NotificationHandler | null = null;
function closeNotify() {
if (notify) {
notify.cancel();
notify = null;
}
}
function showNotify(text: string, settings: NotificationOptions) {
closeNotify();
setTimeout(() => (notify = figma.notify(text, settings)), 10);
}
return { showNotify, closeNotify };
})();
const { initLocker, changeLocker, clearLocker } = (() => {
let interval: number | null = null;
function initLocker() {
interval = setInterval(() => {
figma.viewport.zoom = getScale();
}, 14);
}
function changeLocker() {
if (interval) clearInterval(interval);
const scale = changeScale();
setTimeout(() => {
interval = setInterval(() => {
figma.viewport.zoom = scale;
}, 14);
}, 10);
}
function clearLocker() {
if (interval) clearInterval(interval);
}
return { initLocker, changeLocker, clearLocker };
})();
function updateScale() {
const currentScale = getScale();
showNotify("Zoom is locked", {
timeout: Infinity,
button: {
text: `${currentScale}x`,
action: () => {
changeLocker();
updateScale();
},
},
onDequeue: (reason) => {
if (reason !== "action_button_click") {
clearLocker();
closeNotify();
figma.closePlugin();
}
},
});
}
async function checkSubscription(email?: string) {
const userId = figma.currentUser?.id || "";
let uri = `https://mcrprdcts.eduhund.com/api/fixiq/check_subscription?user_id=${userId}`;
if (email) {
uri += `&email=${email}`;
}
try {
const response = await fetch(uri, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (response.status !== 200) {
showNotify(
"We have some problem. Please, run plugin again, or mail us: we@eduhund.com",
{
error: true,
timeout: 10 * 1000,
onDequeue: () => {
figma.closePlugin();
},
}
);
return null;
}
const data = await response.json();
return data?.access;
} catch {
console.error("Subscribtion check error:");
figma.closePlugin();
}
}
async function run() {
figma.showUI(__html__, { visible: false, width: 280, height: 480 });
showNotify("Cheking your subscription...", {
timeout: Infinity,
});
const result = await checkSubscription();
if (result === null) {
return;
}
if (result) {
initLocker();
updateScale();
} else {
initLocker();
const timeFromFirstRun = figma?.payments?.getUserFirstRanSecondsAgo() || 0;
if (timeFromFirstRun > 3 * 24 * 60 * 60) {
clearLocker();
showNotify("You have reached 3 days free trial", {
timeout: Infinity,
button: {
text: "Get full version",
action: () => {
figma.ui.show();
},
},
onDequeue: (reason) => {
if (reason !== "action_button_click") {
clearLocker();
closeNotify();
figma.closePlugin();
}
},
});
} else {
showNotify("Zoom is locked (free trial)", {
timeout: 300 * 1000,
button: {
text: "Get full version",
action: () => {
figma.ui.show();
},
},
onDequeue: (reason) => {
if (reason === "timeout") {
showNotify("You have reached 5 minutes lock limit", {
timeout: Infinity,
button: {
text: "Get full version",
action: () => {
figma.ui.show();
},
},
onDequeue: (reason) => {
if (reason !== "action_button_click") {
clearLocker();
closeNotify();
figma.closePlugin();
}
},
});
clearLocker();
}
if (reason === "dismiss") {
clearLocker();
closeNotify();
figma.closePlugin();
}
},
});
}
}
}
run();
figma.ui.onmessage = async (message) => {
const { type } = message;
switch (type) {
case "checkEmail": {
const { email } = message;
const result = await checkSubscription(email);
if (result) {
updateScale();
figma.ui.hide();
} else {
figma.ui.postMessage({ type: "invalidEmail" });
}
}
}
};