-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
107 lines (99 loc) · 3.05 KB
/
App.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
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
import { useState } from "react";
import PostForm, { type PostContent } from "./components/PostForm";
import Header from "./components/Header";
declare global {
interface Window {
electron: {
ipcRenderer: {
send: (channel: string, ...args: unknown[]) => void;
on: (
channel: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (data: Record<string, any>) => void
) => void;
once: (
channel: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (data: Record<string, any>) => void
) => void;
invoke: (
channel: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) => any;
};
};
}
}
function App() {
const [isPosting, setIsPosting] = useState(false);
const handlePost = async (
data: PostContent,
callback: (success: boolean) => void
) => {
setIsPosting(true);
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const postContent: Record<string, any> = { ...data };
const files: { base64: string; mimeType: string }[] = [];
for (const index in files) {
const file = files[index];
files.push({
mimeType: file.mimeType,
base64: file.base64,
});
}
postContent.files = files;
window.electron.ipcRenderer.send("post-to-socials", postContent);
window.electron.ipcRenderer.once("post-response", (response) => {
let twitterSucceed = false;
let blueskySucceed = false;
if (data.postToTwitter) {
const twitterId = response.twitter?.value?.data?.id;
if (twitterId) {
alert("Post sent to Twitter.");
twitterSucceed = true;
} else {
alert(
"Post to Twitter failed: " +
(response.twitter?.reason?.toString() ?? "")
);
}
}
if (data.postToBluesky) {
const blueskyURI = response.bluesky?.value?.uri;
if (blueskyURI) {
alert("Post sent to Bluesky.");
blueskySucceed = true;
} else {
alert(
"Post to Bluesky failed: " +
(response.bluesky?.reason?.toString() ?? "")
);
}
}
setIsPosting(false);
if (twitterSucceed && blueskySucceed) {
callback(true);
}
});
window.electron.ipcRenderer.on("post-error", (error) => {
alert("Post failed: " + String(error));
setIsPosting(false);
callback(false);
});
} catch (error) {
alert("Initiate posting failed: " + String(error));
setIsPosting(false);
callback(false);
}
};
return (
<div className="min-h-screen bg-gradient-to-br from-blue-100 to-purple-100">
<Header />
<main className="container mx-auto px-4 py-8">
<PostForm onPost={handlePost} isPosting={isPosting} />
</main>
</div>
);
}
export default App;