@craft-native/react 0.0.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +368 -0
- package/dist/hooks/useDatabase.d.ts +13 -0
- package/dist/hooks/useDatabase.d.ts.map +1 -0
- package/dist/hooks/useFileSystem.d.ts +15 -0
- package/dist/hooks/useFileSystem.d.ts.map +1 -0
- package/dist/hooks/useHttp.d.ts +21 -0
- package/dist/hooks/useHttp.d.ts.map +1 -0
- package/dist/hooks/useNotification.d.ts +20 -0
- package/dist/hooks/useNotification.d.ts.map +1 -0
- package/dist/hooks/useTray.d.ts +22 -0
- package/dist/hooks/useTray.d.ts.map +1 -0
- package/dist/hooks/useWindow.d.ts +19 -0
- package/dist/hooks/useWindow.d.ts.map +1 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +648 -0
- package/dist/index.mjs +608 -0
- package/package.json +50 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { useEffect as useEffect4, useState as useState7, useCallback as useCallback8, useRef as useRef2, useMemo } from "react";
|
|
3
|
+
|
|
4
|
+
// src/hooks/useWindow.ts
|
|
5
|
+
import { useState as useState2, useCallback as useCallback2 } from "react";
|
|
6
|
+
|
|
7
|
+
// src/index.tsx
|
|
8
|
+
import { useEffect, useState, useCallback } from "react";
|
|
9
|
+
function useCraft() {
|
|
10
|
+
return window.craft;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/hooks/useWindow.ts
|
|
14
|
+
function useWindow() {
|
|
15
|
+
const { craft, isReady } = useCraft();
|
|
16
|
+
const [isFullscreen, setIsFullscreen] = useState2(false);
|
|
17
|
+
const [isMaximized, setIsMaximized] = useState2(false);
|
|
18
|
+
const [isMinimized, setIsMinimized] = useState2(false);
|
|
19
|
+
const setTitle = useCallback2(
|
|
20
|
+
async (title) => {
|
|
21
|
+
if (!isReady || !craft) return;
|
|
22
|
+
await craft.invoke("window.setTitle", { title });
|
|
23
|
+
},
|
|
24
|
+
[craft, isReady]
|
|
25
|
+
);
|
|
26
|
+
const setSize = useCallback2(
|
|
27
|
+
async (width, height) => {
|
|
28
|
+
if (!isReady || !craft) return;
|
|
29
|
+
await craft.invoke("window.setSize", { width, height });
|
|
30
|
+
},
|
|
31
|
+
[craft, isReady]
|
|
32
|
+
);
|
|
33
|
+
const setPosition = useCallback2(
|
|
34
|
+
async (x, y) => {
|
|
35
|
+
if (!isReady || !craft) return;
|
|
36
|
+
await craft.invoke("window.setPosition", { x, y });
|
|
37
|
+
},
|
|
38
|
+
[craft, isReady]
|
|
39
|
+
);
|
|
40
|
+
const maximize = useCallback2(async () => {
|
|
41
|
+
if (!isReady || !craft) return;
|
|
42
|
+
await craft.invoke("window.maximize");
|
|
43
|
+
setIsMaximized(true);
|
|
44
|
+
}, [craft, isReady]);
|
|
45
|
+
const minimize = useCallback2(async () => {
|
|
46
|
+
if (!isReady || !craft) return;
|
|
47
|
+
await craft.invoke("window.minimize");
|
|
48
|
+
setIsMinimized(true);
|
|
49
|
+
}, [craft, isReady]);
|
|
50
|
+
const restore = useCallback2(async () => {
|
|
51
|
+
if (!isReady || !craft) return;
|
|
52
|
+
await craft.invoke("window.restore");
|
|
53
|
+
setIsMaximized(false);
|
|
54
|
+
setIsMinimized(false);
|
|
55
|
+
}, [craft, isReady]);
|
|
56
|
+
const toggleFullscreen = useCallback2(async () => {
|
|
57
|
+
if (!isReady || !craft) return;
|
|
58
|
+
await craft.invoke("window.toggleFullscreen");
|
|
59
|
+
setIsFullscreen(!isFullscreen);
|
|
60
|
+
}, [craft, isReady, isFullscreen]);
|
|
61
|
+
const close = useCallback2(async () => {
|
|
62
|
+
if (!isReady || !craft) return;
|
|
63
|
+
await craft.invoke("window.close");
|
|
64
|
+
}, [craft, isReady]);
|
|
65
|
+
const center = useCallback2(async () => {
|
|
66
|
+
if (!isReady || !craft) return;
|
|
67
|
+
await craft.invoke("window.center");
|
|
68
|
+
}, [craft, isReady]);
|
|
69
|
+
const setAlwaysOnTop = useCallback2(
|
|
70
|
+
async (alwaysOnTop) => {
|
|
71
|
+
if (!isReady || !craft) return;
|
|
72
|
+
await craft.invoke("window.setAlwaysOnTop", { alwaysOnTop });
|
|
73
|
+
},
|
|
74
|
+
[craft, isReady]
|
|
75
|
+
);
|
|
76
|
+
return {
|
|
77
|
+
isFullscreen,
|
|
78
|
+
isMaximized,
|
|
79
|
+
isMinimized,
|
|
80
|
+
setTitle,
|
|
81
|
+
setSize,
|
|
82
|
+
setPosition,
|
|
83
|
+
maximize,
|
|
84
|
+
minimize,
|
|
85
|
+
restore,
|
|
86
|
+
toggleFullscreen,
|
|
87
|
+
close,
|
|
88
|
+
center,
|
|
89
|
+
setAlwaysOnTop
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/hooks/useTray.ts
|
|
94
|
+
import { useCallback as useCallback3, useState as useState3 } from "react";
|
|
95
|
+
function useTray() {
|
|
96
|
+
const { craft, isReady } = useCraft();
|
|
97
|
+
const [isVisible, setIsVisible] = useState3(false);
|
|
98
|
+
const create = useCallback3(
|
|
99
|
+
async (icon, tooltip) => {
|
|
100
|
+
if (!isReady || !craft) return;
|
|
101
|
+
await craft.invoke("tray.create", { icon, tooltip });
|
|
102
|
+
setIsVisible(true);
|
|
103
|
+
},
|
|
104
|
+
[craft, isReady]
|
|
105
|
+
);
|
|
106
|
+
const destroy = useCallback3(async () => {
|
|
107
|
+
if (!isReady || !craft) return;
|
|
108
|
+
await craft.invoke("tray.destroy");
|
|
109
|
+
setIsVisible(false);
|
|
110
|
+
}, [craft, isReady]);
|
|
111
|
+
const setIcon = useCallback3(
|
|
112
|
+
async (icon) => {
|
|
113
|
+
if (!isReady || !craft) return;
|
|
114
|
+
await craft.invoke("tray.setIcon", { icon });
|
|
115
|
+
},
|
|
116
|
+
[craft, isReady]
|
|
117
|
+
);
|
|
118
|
+
const setTooltip = useCallback3(
|
|
119
|
+
async (tooltip) => {
|
|
120
|
+
if (!isReady || !craft) return;
|
|
121
|
+
await craft.invoke("tray.setTooltip", { tooltip });
|
|
122
|
+
},
|
|
123
|
+
[craft, isReady]
|
|
124
|
+
);
|
|
125
|
+
const setMenu = useCallback3(
|
|
126
|
+
async (menu) => {
|
|
127
|
+
if (!isReady || !craft) return;
|
|
128
|
+
await craft.invoke("tray.setMenu", { menu });
|
|
129
|
+
},
|
|
130
|
+
[craft, isReady]
|
|
131
|
+
);
|
|
132
|
+
const setTitle = useCallback3(
|
|
133
|
+
async (title) => {
|
|
134
|
+
if (!isReady || !craft) return;
|
|
135
|
+
await craft.invoke("tray.setTitle", { title });
|
|
136
|
+
},
|
|
137
|
+
[craft, isReady]
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
isVisible,
|
|
141
|
+
create,
|
|
142
|
+
destroy,
|
|
143
|
+
setIcon,
|
|
144
|
+
setTooltip,
|
|
145
|
+
setMenu,
|
|
146
|
+
setTitle
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/hooks/useNotification.ts
|
|
151
|
+
import { useCallback as useCallback4 } from "react";
|
|
152
|
+
function useNotification() {
|
|
153
|
+
const { craft, isReady } = useCraft();
|
|
154
|
+
const send = useCallback4(
|
|
155
|
+
async (options) => {
|
|
156
|
+
if (!isReady || !craft) return;
|
|
157
|
+
await craft.invoke("notification.send", options);
|
|
158
|
+
},
|
|
159
|
+
[craft, isReady]
|
|
160
|
+
);
|
|
161
|
+
const requestPermission = useCallback4(async () => {
|
|
162
|
+
if (!isReady || !craft) return false;
|
|
163
|
+
const result = await craft.requestPermission("notifications");
|
|
164
|
+
return result.granted;
|
|
165
|
+
}, [craft, isReady]);
|
|
166
|
+
return {
|
|
167
|
+
send,
|
|
168
|
+
requestPermission
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// src/hooks/useFileSystem.ts
|
|
173
|
+
import { useCallback as useCallback5, useState as useState4 } from "react";
|
|
174
|
+
function useFileSystem() {
|
|
175
|
+
const { craft, isReady } = useCraft();
|
|
176
|
+
const [loading, setLoading] = useState4(false);
|
|
177
|
+
const [error, setError] = useState4(null);
|
|
178
|
+
const readFile = useCallback5(
|
|
179
|
+
async (path, encoding = "utf8") => {
|
|
180
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
181
|
+
setLoading(true);
|
|
182
|
+
setError(null);
|
|
183
|
+
try {
|
|
184
|
+
const result = await craft.invoke("fs.readFile", { path, encoding });
|
|
185
|
+
return result;
|
|
186
|
+
} catch (err) {
|
|
187
|
+
setError(err);
|
|
188
|
+
throw err;
|
|
189
|
+
} finally {
|
|
190
|
+
setLoading(false);
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
[craft, isReady]
|
|
194
|
+
);
|
|
195
|
+
const writeFile = useCallback5(
|
|
196
|
+
async (path, data, encoding = "utf8") => {
|
|
197
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
198
|
+
setLoading(true);
|
|
199
|
+
setError(null);
|
|
200
|
+
try {
|
|
201
|
+
await craft.invoke("fs.writeFile", { path, data, encoding });
|
|
202
|
+
} catch (err) {
|
|
203
|
+
setError(err);
|
|
204
|
+
throw err;
|
|
205
|
+
} finally {
|
|
206
|
+
setLoading(false);
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
[craft, isReady]
|
|
210
|
+
);
|
|
211
|
+
const readDir = useCallback5(
|
|
212
|
+
async (path) => {
|
|
213
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
214
|
+
setLoading(true);
|
|
215
|
+
setError(null);
|
|
216
|
+
try {
|
|
217
|
+
const result = await craft.invoke("fs.readDir", { path });
|
|
218
|
+
return result;
|
|
219
|
+
} catch (err) {
|
|
220
|
+
setError(err);
|
|
221
|
+
throw err;
|
|
222
|
+
} finally {
|
|
223
|
+
setLoading(false);
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
[craft, isReady]
|
|
227
|
+
);
|
|
228
|
+
const mkdir = useCallback5(
|
|
229
|
+
async (path, recursive = false) => {
|
|
230
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
231
|
+
setLoading(true);
|
|
232
|
+
setError(null);
|
|
233
|
+
try {
|
|
234
|
+
await craft.invoke("fs.mkdir", { path, recursive });
|
|
235
|
+
} catch (err) {
|
|
236
|
+
setError(err);
|
|
237
|
+
throw err;
|
|
238
|
+
} finally {
|
|
239
|
+
setLoading(false);
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
[craft, isReady]
|
|
243
|
+
);
|
|
244
|
+
const remove = useCallback5(
|
|
245
|
+
async (path, recursive = false) => {
|
|
246
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
247
|
+
setLoading(true);
|
|
248
|
+
setError(null);
|
|
249
|
+
try {
|
|
250
|
+
await craft.invoke("fs.remove", { path, recursive });
|
|
251
|
+
} catch (err) {
|
|
252
|
+
setError(err);
|
|
253
|
+
throw err;
|
|
254
|
+
} finally {
|
|
255
|
+
setLoading(false);
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
[craft, isReady]
|
|
259
|
+
);
|
|
260
|
+
const exists = useCallback5(
|
|
261
|
+
async (path) => {
|
|
262
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
263
|
+
try {
|
|
264
|
+
const result = await craft.invoke("fs.exists", { path });
|
|
265
|
+
return result;
|
|
266
|
+
} catch (err) {
|
|
267
|
+
setError(err);
|
|
268
|
+
return false;
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
[craft, isReady]
|
|
272
|
+
);
|
|
273
|
+
const stat = useCallback5(
|
|
274
|
+
async (path) => {
|
|
275
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
276
|
+
setLoading(true);
|
|
277
|
+
setError(null);
|
|
278
|
+
try {
|
|
279
|
+
const result = await craft.invoke("fs.stat", { path });
|
|
280
|
+
return result;
|
|
281
|
+
} catch (err) {
|
|
282
|
+
setError(err);
|
|
283
|
+
throw err;
|
|
284
|
+
} finally {
|
|
285
|
+
setLoading(false);
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
[craft, isReady]
|
|
289
|
+
);
|
|
290
|
+
return {
|
|
291
|
+
readFile,
|
|
292
|
+
writeFile,
|
|
293
|
+
readDir,
|
|
294
|
+
mkdir,
|
|
295
|
+
remove,
|
|
296
|
+
exists,
|
|
297
|
+
stat,
|
|
298
|
+
loading,
|
|
299
|
+
error
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/hooks/useDatabase.ts
|
|
304
|
+
import { useCallback as useCallback6, useState as useState5 } from "react";
|
|
305
|
+
function useDatabase(dbPath) {
|
|
306
|
+
const { craft, isReady } = useCraft();
|
|
307
|
+
const [loading, setLoading] = useState5(false);
|
|
308
|
+
const [error, setError] = useState5(null);
|
|
309
|
+
const execute = useCallback6(
|
|
310
|
+
async (sql, params = []) => {
|
|
311
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
312
|
+
setLoading(true);
|
|
313
|
+
setError(null);
|
|
314
|
+
try {
|
|
315
|
+
const result = await craft.invoke("db.execute", { path: dbPath, sql, params });
|
|
316
|
+
return result;
|
|
317
|
+
} catch (err) {
|
|
318
|
+
setError(err);
|
|
319
|
+
throw err;
|
|
320
|
+
} finally {
|
|
321
|
+
setLoading(false);
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
[craft, isReady, dbPath]
|
|
325
|
+
);
|
|
326
|
+
const query = useCallback6(
|
|
327
|
+
async (sql, params = []) => {
|
|
328
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
329
|
+
setLoading(true);
|
|
330
|
+
setError(null);
|
|
331
|
+
try {
|
|
332
|
+
const result = await craft.invoke("db.query", { path: dbPath, sql, params });
|
|
333
|
+
return result;
|
|
334
|
+
} catch (err) {
|
|
335
|
+
setError(err);
|
|
336
|
+
throw err;
|
|
337
|
+
} finally {
|
|
338
|
+
setLoading(false);
|
|
339
|
+
}
|
|
340
|
+
},
|
|
341
|
+
[craft, isReady, dbPath]
|
|
342
|
+
);
|
|
343
|
+
const transaction = useCallback6(
|
|
344
|
+
async (callback) => {
|
|
345
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
346
|
+
setLoading(true);
|
|
347
|
+
setError(null);
|
|
348
|
+
try {
|
|
349
|
+
await craft.invoke("db.transaction.begin", { path: dbPath });
|
|
350
|
+
try {
|
|
351
|
+
await callback({
|
|
352
|
+
execute: async (sql, params = []) => {
|
|
353
|
+
return await craft.invoke("db.execute", { path: dbPath, sql, params });
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
await craft.invoke("db.transaction.commit", { path: dbPath });
|
|
357
|
+
} catch (err) {
|
|
358
|
+
await craft.invoke("db.transaction.rollback", { path: dbPath });
|
|
359
|
+
throw err;
|
|
360
|
+
}
|
|
361
|
+
} catch (err) {
|
|
362
|
+
setError(err);
|
|
363
|
+
throw err;
|
|
364
|
+
} finally {
|
|
365
|
+
setLoading(false);
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
[craft, isReady, dbPath]
|
|
369
|
+
);
|
|
370
|
+
return {
|
|
371
|
+
execute,
|
|
372
|
+
query,
|
|
373
|
+
transaction,
|
|
374
|
+
loading,
|
|
375
|
+
error
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// src/hooks/useHttp.ts
|
|
380
|
+
import { useCallback as useCallback7, useState as useState6 } from "react";
|
|
381
|
+
function useHttp() {
|
|
382
|
+
const { craft, isReady } = useCraft();
|
|
383
|
+
const [loading, setLoading] = useState6(false);
|
|
384
|
+
const [error, setError] = useState6(null);
|
|
385
|
+
const fetch = useCallback7(
|
|
386
|
+
async (url, options = {}) => {
|
|
387
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
388
|
+
setLoading(true);
|
|
389
|
+
setError(null);
|
|
390
|
+
try {
|
|
391
|
+
const result = await craft.invoke("http.fetch", { url, ...options });
|
|
392
|
+
return result;
|
|
393
|
+
} catch (err) {
|
|
394
|
+
setError(err);
|
|
395
|
+
throw err;
|
|
396
|
+
} finally {
|
|
397
|
+
setLoading(false);
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
[craft, isReady]
|
|
401
|
+
);
|
|
402
|
+
const download = useCallback7(
|
|
403
|
+
async (url, path, options = {}) => {
|
|
404
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
405
|
+
setLoading(true);
|
|
406
|
+
setError(null);
|
|
407
|
+
if (options.onProgress) {
|
|
408
|
+
craft.on("download.progress", options.onProgress);
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
await craft.invoke("http.download", { url, path });
|
|
412
|
+
} catch (err) {
|
|
413
|
+
setError(err);
|
|
414
|
+
throw err;
|
|
415
|
+
} finally {
|
|
416
|
+
if (options.onProgress) {
|
|
417
|
+
craft.off("download.progress", options.onProgress);
|
|
418
|
+
}
|
|
419
|
+
setLoading(false);
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
[craft, isReady]
|
|
423
|
+
);
|
|
424
|
+
const upload = useCallback7(
|
|
425
|
+
async (url, filePath, options = {}) => {
|
|
426
|
+
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
427
|
+
setLoading(true);
|
|
428
|
+
setError(null);
|
|
429
|
+
if (options.onProgress) {
|
|
430
|
+
craft.on("upload.progress", options.onProgress);
|
|
431
|
+
}
|
|
432
|
+
try {
|
|
433
|
+
const result = await craft.invoke("http.upload", { url, filePath, ...options });
|
|
434
|
+
return result;
|
|
435
|
+
} catch (err) {
|
|
436
|
+
setError(err);
|
|
437
|
+
throw err;
|
|
438
|
+
} finally {
|
|
439
|
+
if (options.onProgress) {
|
|
440
|
+
craft.off("upload.progress", options.onProgress);
|
|
441
|
+
}
|
|
442
|
+
setLoading(false);
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
[craft, isReady]
|
|
446
|
+
);
|
|
447
|
+
return {
|
|
448
|
+
fetch,
|
|
449
|
+
download,
|
|
450
|
+
upload,
|
|
451
|
+
loading,
|
|
452
|
+
error
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// src/index.ts
|
|
457
|
+
function useCraft2() {
|
|
458
|
+
const [craft, setCraft] = useState7(null);
|
|
459
|
+
const [isReady, setIsReady] = useState7(false);
|
|
460
|
+
useEffect4(() => {
|
|
461
|
+
if (window.craft) {
|
|
462
|
+
setCraft(window.craft);
|
|
463
|
+
setIsReady(true);
|
|
464
|
+
} else {
|
|
465
|
+
const checkCraft = setInterval(() => {
|
|
466
|
+
if (window.craft) {
|
|
467
|
+
setCraft(window.craft);
|
|
468
|
+
setIsReady(true);
|
|
469
|
+
clearInterval(checkCraft);
|
|
470
|
+
}
|
|
471
|
+
}, 100);
|
|
472
|
+
return () => clearInterval(checkCraft);
|
|
473
|
+
}
|
|
474
|
+
}, []);
|
|
475
|
+
return { craft, isReady };
|
|
476
|
+
}
|
|
477
|
+
function usePlatform() {
|
|
478
|
+
const { craft, isReady } = useCraft2();
|
|
479
|
+
const [platform, setPlatform] = useState7(null);
|
|
480
|
+
const [loading, setLoading] = useState7(true);
|
|
481
|
+
const [error, setError] = useState7(null);
|
|
482
|
+
useEffect4(() => {
|
|
483
|
+
if (!isReady || !craft) return;
|
|
484
|
+
craft.getPlatform().then(setPlatform).catch(setError).finally(() => setLoading(false));
|
|
485
|
+
}, [craft, isReady]);
|
|
486
|
+
return { platform, loading, error };
|
|
487
|
+
}
|
|
488
|
+
function useDeviceInfo() {
|
|
489
|
+
const { craft, isReady } = useCraft2();
|
|
490
|
+
const [deviceInfo, setDeviceInfo] = useState7(null);
|
|
491
|
+
const [loading, setLoading] = useState7(true);
|
|
492
|
+
const [error, setError] = useState7(null);
|
|
493
|
+
useEffect4(() => {
|
|
494
|
+
if (!isReady || !craft) return;
|
|
495
|
+
craft.getDeviceInfo().then(setDeviceInfo).catch(setError).finally(() => setLoading(false));
|
|
496
|
+
}, [craft, isReady]);
|
|
497
|
+
return { deviceInfo, loading, error };
|
|
498
|
+
}
|
|
499
|
+
function useToast() {
|
|
500
|
+
const { craft, isReady } = useCraft2();
|
|
501
|
+
const showToast = useCallback8(
|
|
502
|
+
async (message, duration = "short") => {
|
|
503
|
+
if (!isReady || !craft) {
|
|
504
|
+
console.warn("Craft not ready");
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
await craft.showToast(message, duration);
|
|
508
|
+
},
|
|
509
|
+
[craft, isReady]
|
|
510
|
+
);
|
|
511
|
+
return { showToast };
|
|
512
|
+
}
|
|
513
|
+
function useHaptic() {
|
|
514
|
+
const { craft, isReady } = useCraft2();
|
|
515
|
+
const haptic = useCallback8(
|
|
516
|
+
async (type = "selection") => {
|
|
517
|
+
if (!isReady || !craft) {
|
|
518
|
+
console.warn("Craft not ready");
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
await craft.haptic(type);
|
|
522
|
+
},
|
|
523
|
+
[craft, isReady]
|
|
524
|
+
);
|
|
525
|
+
return { haptic };
|
|
526
|
+
}
|
|
527
|
+
function usePermission(permission) {
|
|
528
|
+
const { craft, isReady } = useCraft2();
|
|
529
|
+
const [granted, setGranted] = useState7(null);
|
|
530
|
+
const [loading, setLoading] = useState7(false);
|
|
531
|
+
const [error, setError] = useState7(null);
|
|
532
|
+
const request = useCallback8(async () => {
|
|
533
|
+
if (!isReady || !craft) {
|
|
534
|
+
console.warn("Craft not ready");
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
setLoading(true);
|
|
538
|
+
try {
|
|
539
|
+
const result = await craft.requestPermission(permission);
|
|
540
|
+
setGranted(result.granted);
|
|
541
|
+
} catch (err) {
|
|
542
|
+
setError(err);
|
|
543
|
+
} finally {
|
|
544
|
+
setLoading(false);
|
|
545
|
+
}
|
|
546
|
+
}, [craft, isReady, permission]);
|
|
547
|
+
return { granted, request, loading, error };
|
|
548
|
+
}
|
|
549
|
+
function useCraftEvent(event, callback) {
|
|
550
|
+
const { craft, isReady } = useCraft2();
|
|
551
|
+
const callbackRef = useRef2(callback);
|
|
552
|
+
useEffect4(() => {
|
|
553
|
+
callbackRef.current = callback;
|
|
554
|
+
}, [callback]);
|
|
555
|
+
useEffect4(() => {
|
|
556
|
+
if (!isReady || !craft) return;
|
|
557
|
+
const handler = (...args) => callbackRef.current(...args);
|
|
558
|
+
craft.on(event, handler);
|
|
559
|
+
return () => {
|
|
560
|
+
craft.off(event, handler);
|
|
561
|
+
};
|
|
562
|
+
}, [craft, isReady, event]);
|
|
563
|
+
}
|
|
564
|
+
function useCraftInvoke() {
|
|
565
|
+
const { craft, isReady } = useCraft2();
|
|
566
|
+
const invoke = useCallback8(
|
|
567
|
+
async (method, params) => {
|
|
568
|
+
if (!isReady || !craft) {
|
|
569
|
+
throw new Error("Craft not ready");
|
|
570
|
+
}
|
|
571
|
+
return await craft.invoke(method, params);
|
|
572
|
+
},
|
|
573
|
+
[craft, isReady]
|
|
574
|
+
);
|
|
575
|
+
return { invoke, isReady };
|
|
576
|
+
}
|
|
577
|
+
function useIsMobile() {
|
|
578
|
+
const { platform } = usePlatform();
|
|
579
|
+
return useMemo(() => {
|
|
580
|
+
if (!platform) return false;
|
|
581
|
+
return platform.platform === "ios" || platform.platform === "android";
|
|
582
|
+
}, [platform]);
|
|
583
|
+
}
|
|
584
|
+
function useIsDesktop() {
|
|
585
|
+
const { platform } = usePlatform();
|
|
586
|
+
return useMemo(() => {
|
|
587
|
+
if (!platform) return false;
|
|
588
|
+
return platform.platform === "macos" || platform.platform === "windows" || platform.platform === "linux";
|
|
589
|
+
}, [platform]);
|
|
590
|
+
}
|
|
591
|
+
export {
|
|
592
|
+
useCraft2 as useCraft,
|
|
593
|
+
useCraftEvent,
|
|
594
|
+
useCraftInvoke,
|
|
595
|
+
useDatabase,
|
|
596
|
+
useDeviceInfo,
|
|
597
|
+
useFileSystem,
|
|
598
|
+
useHaptic,
|
|
599
|
+
useHttp,
|
|
600
|
+
useIsDesktop,
|
|
601
|
+
useIsMobile,
|
|
602
|
+
useNotification,
|
|
603
|
+
usePermission,
|
|
604
|
+
usePlatform,
|
|
605
|
+
useToast,
|
|
606
|
+
useTray,
|
|
607
|
+
useWindow
|
|
608
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@craft-native/react",
|
|
3
|
+
"version": "0.0.72",
|
|
4
|
+
"homepage": "https://github.com/craft-native/craft/tree/main/packages/react#readme",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/craft-native/craft.git",
|
|
8
|
+
"directory": "packages/react"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/craft-native/craft/issues"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"description": "React bindings for Craft framework",
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"module": "dist/index.mjs",
|
|
19
|
+
"types": "dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"import": "./dist/index.mjs",
|
|
23
|
+
"require": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly",
|
|
29
|
+
"dev": "tsup src/index.ts --format cjs,esm --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"craft",
|
|
33
|
+
"react",
|
|
34
|
+
"hooks",
|
|
35
|
+
"native",
|
|
36
|
+
"mobile",
|
|
37
|
+
"desktop"
|
|
38
|
+
],
|
|
39
|
+
"author": "Craft Team",
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"react": ">=16.8.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/react": "^18.2.0",
|
|
46
|
+
"react": "^18.2.0",
|
|
47
|
+
"tsup": "^8.0.0",
|
|
48
|
+
"typescript": "^7.0.2"
|
|
49
|
+
}
|
|
50
|
+
}
|