Repository navigation
Expand file tree
/
Copy pathindex.tsx
More file actions
239 lines (218 loc) · 8.97 KB
/
Copy pathindex.tsx
File metadata and controls
239 lines (218 loc) · 8.97 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// App 頁面:設定帳號(只存在本機 Keychain)、查看今日狀態、手動簽到或全部執行、預覽小工具。
import {
Button,
HStack,
List,
Navigation,
NavigationStack,
Script,
SecureField,
Section,
Spacer,
Text,
TextField,
VStack,
Widget,
useEffect,
useState,
} from "scripting"
import type { DayState, Extra } from "./src/baha"
import {
clearAll,
getCredentials,
getSavedAccount,
getState,
messageOf,
runCheck,
saveCredentials,
taipeiDate,
totp,
totpRemaining,
} from "./src/baha"
import type { Tone } from "./src/extras"
import { describeExtra, runAll, todayExtras } from "./src/extras"
const GREEN = "#2EB872"
const ORANGE = "#FF8A00"
const RED = "#FF5A5F"
const TONE: Record<Tone, string> = { ok: GREEN, muted: "secondaryLabel", warn: ORANGE, bad: RED }
function ExtraRow({ title, extra, emptyText }: { title: string; extra?: Extra; emptyText?: string }) {
const { text, tone } = describeExtra(extra, emptyText)
return (
<VStack alignment="leading" spacing={2}>
<Row label={title} value={text} color={TONE[tone]} />
{extra?.detail ? <Text font="caption" foregroundStyle="secondaryLabel">{extra.detail}</Text> : null}
</VStack>
)
}
function Row({ label, value, color }: { label: string; value: string; color?: string }) {
return (
<HStack>
<Text>{label}</Text>
<Spacer />
<Text fontWeight="semibold" foregroundStyle={color ?? "secondaryLabel"}>{value || "—"}</Text>
</HStack>
)
}
function timeLabel(ts: number): string {
if (!ts) return "—"
const d = new Date(ts)
const pad = (n: number) => String(n).padStart(2, "0")
return `${d.getMonth() + 1}/${d.getDate()} ${pad(d.getHours())}:${pad(d.getMinutes())}`
}
function statusRow(state: DayState | null): { text: string; color: string } {
if (!state || state.date !== taipeiDate()) return { text: "尚未檢查", color: "secondaryLabel" }
if (state.signed) return { text: "已簽到", color: GREEN }
if (state.error) return { text: "失敗", color: RED }
return { text: "尚未簽到", color: ORANGE }
}
function App() {
const dismiss = Navigation.useDismiss()
const initial = getSavedAccount()
const [account, setAccount] = useState(initial)
const [uid, setUid] = useState(initial.uid)
const [pwd, setPwd] = useState("")
const [secret, setSecret] = useState("")
const [state, setState] = useState<DayState | null>(getState())
const [busy, setBusy] = useState("")
const [notice, setNotice] = useState("")
const [now, setNow] = useState(Date.now())
// 每秒更新驗證碼預覽
// 每秒更新驗證碼預覽與廣告等待狀態
useEffect(() => {
if (!account.hasTotp && busy !== "all") return
const id = setInterval(() => setNow(Date.now()), 1000)
return () => clearInterval(id)
}, [account.hasTotp, busy])
const configured = account.uid !== "" && account.hasPassword
async function runEverything() {
if (busy) return
setBusy("all")
setNotice("")
try {
const next = await runAll({ onProgress: (s) => { setState(s); Widget.reloadAll() } })
setState(next)
Widget.reloadAll()
} finally {
setBusy("")
}
}
async function run(label: string, signIn: boolean) {
if (busy) return
setBusy(label)
setNotice("")
try {
const next = await runCheck(signIn)
setState(next)
Widget.reloadAll()
} finally {
setBusy("")
}
}
function save() {
try {
saveCredentials({ uid, pwd: pwd || undefined, totp: secret.trim() ? secret : undefined })
setPwd("")
setSecret("")
setAccount(getSavedAccount())
setState(getState())
setNotice("已儲存在本機 Keychain")
Widget.reloadAll()
} catch (error) {
setNotice(messageOf(error))
}
}
function removeTotp() {
try {
saveCredentials({ uid: account.uid, totp: "" })
setAccount(getSavedAccount())
setNotice("已移除兩步驟驗證金鑰")
} catch (error) {
setNotice(messageOf(error))
}
}
async function clearEverything() {
const ok = await Dialog.confirm({ title: "清除所有資料?", message: "會刪除本機儲存的帳號、密碼、兩步驟驗證金鑰與登入狀態。" })
if (!ok) return
clearAll()
setUid("")
setPwd("")
setSecret("")
setAccount(getSavedAccount())
setState(null)
setNotice("已清除")
Widget.reloadAll()
}
const status = statusRow(state)
const extras = todayExtras(state, now)
const creds = account.hasTotp ? getCredentials() : null
const code = creds?.totp ? totp(creds.totp, now) : ""
return (
<NavigationStack>
<List
navigationTitle="Baha Check"
navigationBarTitleDisplayMode="inline"
toolbar={{ cancellationAction: <Button title="完成" action={dismiss} /> }}
>
<Section header={<Text>今日狀態</Text>} footer={notice ? <Text>{notice}</Text> : undefined}>
<Row label="今日" value={status.text} color={status.color} />
{state && state.date === taipeiDate() && state.signed ? <Row label="連續簽到" value={`${state.days} 天`} /> : null}
{state?.error && !state.signed ? <Text font="caption" foregroundStyle={RED}>{state.error}</Text> : null}
{state?.nickname ? <Row label="帳號" value={state.nickname} /> : null}
<Row label="最後檢查" value={state ? timeLabel(state.checkedAt) : "—"} />
<Button
title={busy === "signin" ? "簽到中…" : "立即簽到"}
systemImage="checkmark.circle"
disabled={!configured || !!busy}
action={() => run("signin", true)}
/>
<Button
title={busy === "check" ? "檢查中…" : "只檢查狀態"}
systemImage="arrow.clockwise"
disabled={!configured || !!busy}
action={() => run("check", false)}
/>
</Section>
<Section
header={<Text>公會/動畫瘋/廣告</Text>}
footer={<Text>這三項會在小工具每次更新時自動執行(只在中尺寸小工具顯示):公會與答題不需簽到,廣告雙倍在主站自動簽到後開始(下次更新再領獎勵)。失敗或答案未公布時約 30 分鐘後自動重試。更新時間由 iOS 安排,可能有延遲;想立刻完成可按「全部執行」(廣告需等約 30 秒)。動畫瘋答案取自每日公布答案的小屋文章,不保證正確。</Text>}
>
<ExtraRow title="公會簽到" extra={extras.guild} />
<ExtraRow title="動畫瘋答題" extra={extras.quiz} />
<ExtraRow title="廣告雙倍" extra={extras.ads}
emptyText={state?.signed && state.date === taipeiDate() ? "等待執行" : "簽到後自動領"} />
<Button
title={busy === "all" ? "執行中…(廣告約 30 秒)" : "全部執行"}
systemImage="bolt"
disabled={!configured || !!busy}
action={runEverything}
/>
</Section>
<Section
header={<Text>巴哈姆特帳號</Text>}
footer={<Text>帳號、密碼與兩步驟驗證金鑰只存在這支 iPhone 的 Keychain,不會上傳或寫進 GitHub。密碼與金鑰欄位留空代表沿用已儲存的值。</Text>}
>
<TextField title="帳號" prompt="巴哈姆特帳號" value={uid} onChanged={setUid}
autocorrectionDisabled textInputAutocapitalization="never" />
<SecureField title="密碼" prompt={account.hasPassword ? "已儲存(留空沿用)" : "密碼"} value={pwd} onChanged={setPwd} />
<SecureField title="兩步驟驗證金鑰" prompt={account.hasTotp ? "已儲存(留空沿用)" : "選填,16 碼金鑰或 otpauth:// 網址"}
value={secret} onChanged={setSecret} />
<Button title="儲存" systemImage="lock" disabled={!uid.trim()} action={save} />
{code ? <Row label="目前驗證碼" value={`${code.slice(0, 3)} ${code.slice(3)}(${totpRemaining(now)} 秒)`} /> : null}
{account.hasTotp ? <Button title="移除兩步驟驗證金鑰" role="destructive" action={removeTotp} /> : null}
</Section>
<Section header={<Text>小工具</Text>} footer={<Text>主畫面加入 Scripting 小工具並選擇本腳本。小工具每次更新時會自動簽到並執行其他項目;小型只顯示主站簽到,中型另外顯示公會、答題、廣告。自動簽到失敗或想立刻完成時,點小工具即可。</Text>}>
<Button title="預覽小型(主站簽到)" systemImage="square" action={() => Widget.preview({ family: "systemSmall" })} />
<Button title="預覽中型(含公會、答題、廣告)" systemImage="rectangle" action={() => Widget.preview({ family: "systemMedium" })} />
</Section>
<Section>
<Button title="清除所有本機資料" role="destructive" action={clearEverything} />
</Section>
</List>
</NavigationStack>
)
}
async function main() {
await Navigation.present({ element: <App /> })
Script.exit()
}
main()