|
1 | 1 | # Advanced usage |
2 | 2 |
|
3 | | -This page collects low-level helpers and optional maintenance tools. These are intentionally |
4 | | -kept out of the main README so new users can follow a single, Uppy-first path. |
5 | | - |
6 | | -## Low-level tus helpers (advanced) |
7 | | - |
8 | | -If you need a custom uploader (no Uppy), the legacy tus helpers are still available: |
9 | | - |
10 | | -```tsx |
11 | | -import { |
12 | | - uploadWithTransloaditTus, |
13 | | - useTransloaditTusUpload, |
14 | | - uploadFilesWithTransloaditTus, |
15 | | -} from "@transloadit/convex/react"; |
16 | | -import { api } from "../convex/_generated/api"; |
17 | | - |
18 | | -function TusUpload() { |
19 | | - const { upload, isUploading, progress } = useTransloaditTusUpload( |
20 | | - api.transloadit.createAssembly, |
21 | | - ); |
22 | | - |
23 | | - const handleUpload = async (file: File) => { |
24 | | - await upload(file, { |
25 | | - templateId: "template_id_here", |
26 | | - onAssemblyCreated: (assembly) => console.log(assembly.assemblyId), |
27 | | - }); |
28 | | - }; |
29 | | - |
30 | | - return ( |
31 | | - <div> |
32 | | - <input type="file" onChange={(e) => handleUpload(e.target.files![0])} /> |
33 | | - {isUploading && <p>Uploading: {progress}%</p>} |
34 | | - </div> |
35 | | - ); |
36 | | -} |
37 | | -``` |
38 | | - |
39 | | -Imperative helper (e.g. non-React): |
40 | | - |
41 | | -```ts |
42 | | -import { useAction } from "convex/react"; |
43 | | - |
44 | | -const createAssembly = useAction(api.transloadit.createAssembly); |
45 | | - |
46 | | -await uploadWithTransloaditTus( |
47 | | - createAssembly, |
48 | | - file, |
49 | | - { templateId: "template_id_here" }, |
50 | | - { onStateChange: (state) => console.log(state) }, |
51 | | -); |
52 | | -``` |
53 | | - |
54 | | -Multi-file uploads with concurrency + cancellation: |
55 | | - |
56 | | -```ts |
57 | | -import { uploadFilesWithTransloaditTus } from "@transloadit/convex/react"; |
58 | | - |
59 | | -const controller = uploadFilesWithTransloaditTus(createAssembly, files, { |
60 | | - concurrency: 3, |
61 | | - onFileProgress: (file, progress) => console.log(file.name, progress), |
62 | | - onOverallProgress: (progress) => console.log("overall", progress), |
63 | | -}); |
64 | | - |
65 | | -// Optional: cancel in-flight uploads |
66 | | -// controller.cancel(); |
67 | | - |
68 | | -const result = await controller.promise; |
69 | | -console.log(result.files); |
70 | | -``` |
71 | | - |
72 | | -## Reactive status/results helpers |
73 | | - |
74 | | -```tsx |
75 | | -import { useAssemblyStatus, useTransloaditFiles } from "@transloadit/convex/react"; |
76 | | -import { api } from "../convex/_generated/api"; |
77 | | - |
78 | | -function AssemblyStatus({ assemblyId }: { assemblyId: string }) { |
79 | | - const status = useAssemblyStatus(api.transloadit.getAssemblyStatus, assemblyId); |
80 | | - const results = useTransloaditFiles(api.transloadit.listResults, { |
81 | | - assemblyId, |
82 | | - }); |
83 | | - |
84 | | - if (!status) return null; |
85 | | - return ( |
86 | | - <div> |
87 | | - <p>Status: {status.ok}</p> |
88 | | - <p>Results: {results?.length ?? 0}</p> |
89 | | - </div> |
90 | | - ); |
91 | | -} |
92 | | -``` |
93 | | - |
94 | | -Polling fallback (no webhooks): |
95 | | - |
96 | | -```tsx |
97 | | -import { useAssemblyStatusWithPolling } from "@transloadit/convex/react"; |
98 | | -import { api } from "../convex/_generated/api"; |
99 | | - |
100 | | -const status = useAssemblyStatusWithPolling( |
101 | | - api.transloadit.getAssemblyStatus, |
102 | | - api.transloadit.refreshAssembly, |
103 | | - assemblyId, |
104 | | - { pollIntervalMs: 5000, stopOnTerminal: true }, |
105 | | -); |
106 | | -``` |
| 3 | +This page collects optional helpers that build on the Uppy-first integration path. |
107 | 4 |
|
108 | 5 | ## Typed helpers (raw payload parsing) |
109 | 6 |
|
@@ -145,14 +42,20 @@ type ResizeResult = ResultForRobot<"/image/resize">; |
145 | 42 | type EncodeResult = ResultForRobot<"/video/encode">; |
146 | 43 | ``` |
147 | 44 |
|
148 | | -Uppy/Tus wiring: |
| 45 | +Polling fallback (no webhooks): |
149 | 46 |
|
150 | 47 | ```ts |
151 | | -import { buildTusUploadConfig } from "@transloadit/convex"; |
152 | | - |
153 | | -const { endpoint, metadata } = buildTusUploadConfig(assembly.data, file, { |
154 | | - fieldName: "file", |
| 48 | +import { pollAssembly, isAssemblyTerminal } from "@transloadit/convex"; |
| 49 | + |
| 50 | +const controller = pollAssembly({ |
| 51 | + intervalMs: 5000, |
| 52 | + refresh: async () => { |
| 53 | + await refreshAssembly({ assemblyId }); |
| 54 | + }, |
| 55 | + isTerminal: () => isAssemblyTerminal(status), |
155 | 56 | }); |
| 57 | + |
| 58 | +// controller.stop(); |
156 | 59 | ``` |
157 | 60 |
|
158 | 61 | ## Optional demo template tooling |
|
0 commit comments