generated from vivid-lapin/ts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.tsx
208 lines (202 loc) · 6.1 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
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
import $ from "@recoiljs/refine"
import axios from "axios"
import rax from "axios-retry"
import clsx from "clsx"
import React, { useEffect, useState } from "react"
import { atom, useRecoilValue, useRecoilState } from "recoil"
import { syncEffect } from "recoil-sync"
import { GyazoSetting } from "./types"
import { InitPlugin } from "../@types/plugin"
import tailwind from "../tailwind.scss"
const _id = "io.github.ci7lus.miraktest-plugins.gyazo"
const prefix = "plugins.ci7lus.gyazo"
const meta = {
id: _id,
name: "Gyazo",
author: "ci7lus",
version: "0.1.5",
description: "スクリーンショットをGyazoにアップロードするプラグインです。",
authorUrl: "https://github.com/ci7lus",
url: "https://github.com/ci7lus/miraktest-plugins/tree/master/src/miraktest-gyazo",
}
rax(axios, {
retryDelay: () => 1000,
retryCondition: (res) => res.isAxiosError,
})
const main: InitPlugin = {
renderer: ({ atoms, constants }) => {
const settingRefine = $.object({
token: $.voidable($.string()),
collectionId: $.voidable($.string()),
})
const settingAtom = atom<GyazoSetting>({
key: `${prefix}.setting`,
default: {},
effects: [
syncEffect({
storeKey: constants?.recoil?.sharedKey,
refine: settingRefine,
}),
syncEffect({
storeKey: constants?.recoil?.storedKey,
refine: settingRefine,
}),
],
})
const ssA = atom<string>({
key: `${prefix}.objurl`,
default: "",
effects: [
syncEffect({
storeKey: constants?.recoil?.sharedKey,
refine: $.string(),
}),
],
})
return {
...meta,
exposedAtoms: [],
sharedAtoms: [
{
type: "atom",
atom: settingAtom,
},
{
type: "atom",
atom: ssA,
},
],
storedAtoms: [
{
type: "atom",
atom: settingAtom,
},
],
setup() {
return
},
components: [
{
id: `${prefix}.uploader`,
position: "onBackground",
component: () => {
const setting = useRecoilValue(settingAtom)
const playingContent = useRecoilValue(
atoms.contentPlayerPlayingContentAtom
)
const url = useRecoilValue(atoms.contentPlayerScreenshotUrlSelector)
useEffect(() => {
const token = setting.token
if (!token || !url) {
return
}
;(async () => {
const bin = await fetch(url)
const form = new FormData()
form.append("access_token", token)
const title = playingContent?.program?.name
if (title) {
form.append("desc", title)
}
const desc = playingContent?.service?.name
if (desc) {
form.append("title", desc)
}
const blob = await bin.blob()
form.append("imagedata", blob, url.split("/").pop())
if (setting.collectionId) {
form.append("collection_id", setting.collectionId)
}
axios
.post("https://upload.gyazo.com/api/upload", form)
.catch((e) => console.error(e))
})()
}, [url])
return <></>
},
},
{
id: `${prefix}.settings`,
position: "onSetting",
label: meta.name,
component: () => {
const [setting, setSetting] = useRecoilState(settingAtom)
const [token, setToken] = useState(setting.token)
const [collectionId, setCollectionId] = useState(
setting.collectionId
)
return (
<>
<style>{tailwind}</style>
<form
className="m-4"
onSubmit={(e) => {
e.preventDefault()
setSetting({
token: token || undefined,
collectionId: collectionId || undefined,
})
}}
>
<label className={clsx("mb-2", "block")}>
<span>Gyazo Access Token</span>
<input
type="text"
placeholder="****"
className={clsx(
"block",
"mt-2",
"form-input",
"rounded-md",
"w-full",
"text-gray-900"
)}
value={token || ""}
onChange={(e) => setToken(e.target.value)}
/>
</label>
<label className={clsx("mb-2", "block")}>
<span>Collection Id</span>
<input
type="text"
placeholder="****"
className={clsx(
"block",
"mt-2",
"form-input",
"rounded-md",
"w-full",
"text-gray-900"
)}
value={collectionId || ""}
onChange={(e) => setCollectionId(e.target.value)}
/>
</label>
<button
type="submit"
className={clsx(
"bg-gray-100",
"text-gray-800",
"p-2",
"px-2",
"my-4",
"rounded-md",
"cursor-pointer"
)}
>
保存
</button>
</form>
</>
)
},
},
],
destroy() {
return
},
windows: {},
}
},
}
export default main