forked from schl3ck/scripting-app-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
478 lines (458 loc) · 12.1 KB
/
index.tsx
File metadata and controls
478 lines (458 loc) · 12.1 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import {
Button,
ContentUnavailableView,
EmptyView,
HStack,
Image,
List,
Navigation,
NavigationLink,
NavigationStack,
Path,
ProgressView,
Script,
Section,
Spacer,
Text,
VStack,
VirtualNode,
ZStack,
useCallback,
useEffect,
useMemo,
useState,
} from "scripting"
import { CrawlWidgetSizes } from "./Widget/crawlWidgetSizes"
import { TestMultiSelectionsPickerView } from "./MultiSelectionsPicker/testMultiSelectionsPicker"
import { ScriptUpdaterDescription } from "./ScriptUpdater/scriptUpdaterDescription"
import {
VersionData,
StorageData,
downloadUpdate,
installDownloadedScript,
cleanFiles,
updateAvailable,
getVersionCache,
Changelog,
UpdateCheckInterval,
} from "./ScriptUpdater/scriptUpdater"
const versionsUrl = "https://github.com/schl3ck/scripting-app-lib/releases"
const currentVersion = "1.4.1"
function Tests() {
return (
<Section title="Tests and Examples">
<NavigationLink
title="Multi Selections Picker View"
destination={<TestMultiSelectionsPickerView />}
/>
<NavigationLink
title="Widget Sizes"
destination={<CrawlWidgetSizes />}
/>
<NavigationLink
title="Script Updater"
destination={<ScriptUpdaterDescription />}
/>
</Section>
)
}
function MainView() {
const [from, setFrom] = useState("")
const [to, setTo] = useState("")
const dismiss = Navigation.useDismiss()
const [linkCreated, setLinkCreated] = useState(false)
const parentFolder = Path.normalize(Path.join(Script.directory, ".."))
const [updatedToVersion, setUpdatedToVersion] = useState<string>()
return (
<NavigationStack>
<List
navigationTitle={updatedToVersion ? "" : "Link a lib"}
// navigationBarTitleDisplayMode={"large"}
toolbar={{
cancellationAction: (
<Button action={dismiss}>
<Text>Close</Text>
</Button>
),
}}
overlay={
linkCreated ? (
<TemporaryPopover>
<VStack
font={"title"}
spacing={5}
>
<Image systemName="checkmark" />
<Text>Done</Text>
</VStack>
</TemporaryPopover>
) : updatedToVersion ? (
<UpdateFinished version={updatedToVersion} />
) : (
<EmptyView />
)
}
>
<Section
footer={
<Text>Select a library module and a destination folder to create a symbolic link.</Text>
}
>
<NavigationLink
destination={
<Folder
key={"libDestination"}
path={Script.directory}
select="file"
onSelection={(p) => {
setFrom(p)
}}
tip={"Select a file to link"}
/>
}
>
<HStack>
<Text>Select a Lib File</Text>
<Spacer />
<Text
foregroundStyle={"secondaryLabel"}
font={"callout"}
>{from.replace(Script.directory + "/", "")}</Text>
</HStack>
</NavigationLink>
<NavigationLink
destination={
<Folder
key={"targetDestination"}
path={parentFolder}
select="folder"
topLevel
onSelection={(p) => {
setTo(p)
}}
/>
}
>
<HStack>
<Text>Select a Target Folder</Text>
<Spacer />
<Text
foregroundStyle={"secondaryLabel"}
font={"callout"}
>{to.replace(parentFolder + "/", "")}</Text>
</HStack>
</NavigationLink>
</Section>
<Section>
<Button
multilineTextAlignment={"center"}
action={() => {
FileManager.createLinkSync(Path.join(to, Path.basename(from)), from)
setLinkCreated(true)
setTimeout(() => setLinkCreated(false), 1000)
}}
disabled={!from || !to}
>
<Text>Create Link</Text>
</Button>
</Section>
<Tests />
<UpdateSection onUpdated={setUpdatedToVersion} />
</List>
</NavigationStack>
)
}
function TemporaryPopover({ children }: { children: VirtualNode }) {
return (
<ZStack
frame={{
alignment: "center",
minHeight: 50,
minWidth: 50,
}}
padding={20}
background={"thinMaterial"}
clipShape={{
type: "rect",
cornerRadius: 15,
style: "continuous",
}}
>
{children}
</ZStack>
)
}
function Folder({
path,
onSelection,
select,
topLevel = false,
tip,
}: {
path: string
onSelection: (path: string) => void
select: "folder" | "file"
topLevel?: boolean
tip?: string
}) {
const dismiss = Navigation.useDismiss()
const [content, setContent] = useState<string[]>([])
const [loaded, setLoaded] = useState(false)
const load = useCallback(async () => {
setContent((await FileManager.readDirectory(path)).map((i) => Path.join(path, i)))
setLoaded(true)
}, [])
useEffect(() => {
load()
}, [])
const { folders, files } = useMemo(() => {
const folders = []
const files = []
for (const item of content) {
if (FileManager.isDirectorySync(item)) {
folders.push(item)
} else {
files.push(item)
}
}
folders.sort((a, b) => a.localeCompare(b))
files.sort((a, b) => a.localeCompare(b))
return {
folders,
files,
}
}, [content])
return (
<List
navigationTitle={Path.basename(path)}
toolbar={{
bottomBar: select === "folder" && !topLevel ?
<HStack>
<Spacer />
<Button
action={() => {
dismiss()
onSelection(path)
}}
>
<HStack>
<Image systemName={"link"} />
<Text>Link lib file here</Text>
</HStack>
</Button>
<Spacer />
</HStack>
: undefined,
topBarTrailing: <Button
systemImage="folder.badge.plus"
title={"New Folder"}
action={async () => {
const name = await Dialog.prompt({
title: "New Folder",
cancelLabel: "Cancel",
confirmLabel: "Create",
})
if (name) {
const folder = Path.join(path, name)
FileManager.createDirectorySync(folder)
setContent([folder, ...content])
}
}}
/>,
}}
overlay={
content.length === 0 || !loaded ? (
<ContentUnavailableView
title={loaded ? "No entries" : "Loading"}
systemImage={loaded ? "folder" : "progress.indicator"}
/>
) : (
<EmptyView />
)
}
contentMargins={
tip != null
? {
edges: 'top',
insets: 0,
}
: undefined
}
>
{tip != null
? <Section footer={
<Text>{tip}</Text>
}>
<EmptyView />
</Section>
: null}
{folders.map((f) => (
<NavigationLink
destination={
<Folder
path={f}
tip={tip}
onSelection={(p) => {
dismiss()
onSelection(p)
}}
select={select}
/>
}
>
<HStack>
<Image
systemName="folder.fill"
foregroundStyle={"accentColor"}
/>
<Text>{Path.basename(f)}</Text>
</HStack>
</NavigationLink>
))}
{files.map((f) => (
<HStack
contentShape={"rect"}
onTapGesture={() => {
dismiss()
onSelection(f)
}}
disabled={select === "folder"}
>
<Image
systemName="doc.fill"
foregroundStyle={"accentColor"}
/>
<Text>{Path.basename(f)}</Text>
<Spacer />
</HStack>
))}
</List>
)
}
function UpdateSection({
onUpdated,
}: {
onUpdated: (version: string) => void,
}) {
const [error, setError] = useState<string>()
const [showError, setShowError] = useState(false)
const [checkingForUpdate, setCheckingForUpdate] = useState(false)
const [updating, setUpdating] = useState(false)
const [newVersions, setNewVersions] = useState<VersionData[]>(() => [])
const [versionCache, setVersionCache] = useState<StorageData | null>()
const update = useCallback(async () => {
setUpdating(true)
await downloadUpdate(newVersions[newVersions.length - 1].url)
await installDownloadedScript()
await cleanFiles()
setUpdating(false)
onUpdated(newVersions[newVersions.length - 1].version)
}, [newVersions])
const checkForUpdate = useCallback(async (interval: UpdateCheckInterval) => {
setCheckingForUpdate(true)
try {
setNewVersions(await updateAvailable(versionsUrl, interval, currentVersion))
} catch (err) {
const e = err as any
setError("" + err)
console.error({
name: e.name,
message: e.message,
cause: e.cause,
fileName: e.fileName,
lineNumber: e.lineNumber,
columnNumber: e.columnNumber,
stack: e.stack,
})
setShowError(true)
}
setVersionCache(getVersionCache())
setCheckingForUpdate(false)
}, [])
useEffect(() => {
setVersionCache(getVersionCache())
checkForUpdate("daily")
}, [])
return (
<Section>
<Button
action={() => checkForUpdate("every time")}
disabled={checkingForUpdate || updating}
alert={{
title: "There was an error",
message: <Text>{error ?? ""}</Text>,
actions: <EmptyView />,
isPresented: showError,
onChanged: setShowError,
}}
>
<HStack>
<Text>{checkingForUpdate ? "Checking..." : "Check for an update"}</Text>
<Spacer />
{checkingForUpdate ? (
<ProgressView progressViewStyle="circular" />
) : error ? (
<Image
systemName="exclamationmark.triangle.fill"
foregroundStyle="systemYellow"
/>
) : (
<VStack
foregroundStyle="secondaryLabel"
alignment={"trailing"}
>
<Text
font={"caption"}
>
{versionCache ? new Date(versionCache.lastChecked).toLocaleString() : "never"}
</Text>
<Text
font={"caption2"}
>Last checked</Text>
</VStack>
)}
</HStack>
</Button>
<NavigationLink
title="Changelog"
destination={
<Changelog
versions={newVersions.length > 0 ? newVersions : versionCache?.versions ?? []}
/>
}
/>
{newVersions.length > 0 ? (
<Button action={update}>
<HStack>
<Text>
{updating
? "Updating..."
: "Update to " + newVersions[newVersions.length - 1].version}
</Text>
<Spacer />
{updating ? <ProgressView progressViewStyle="circular" /> : <EmptyView />}
</HStack>
</Button>
) : (
<EmptyView />
)}
</Section>
)
}
function UpdateFinished({ version }: { version: string }) {
return (
<ContentUnavailableView
systemImage="sparkles"
title={"Update to " + version + " installed!"}
description="Please close this script, rebuild all scripts (in the settings of Scripting) and start this script again to use the new version."
background="secondarySystemBackground"
/>
)
}
async function run() {
await Navigation.present(
<MainView />
)
Script.exit()
}
run()