create-fluxstack 1.20.1 → 1.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,70 +0,0 @@
1
- import { useMemo } from 'react'
2
- import { Live, useLiveChunkedUpload } from '@fluxstack/live-react'
3
- import type { LiveChunkedUploadOptions } from '@fluxstack/live-react'
4
- import type { FileUploadCompleteResponse } from '@fluxstack/live'
5
- import { LiveUpload } from '@server/live/LiveUpload'
6
-
7
- export interface UseLiveUploadOptions {
8
- live?: {
9
- room?: string
10
- userId?: string
11
- autoMount?: boolean
12
- debug?: boolean
13
- }
14
- upload?: LiveChunkedUploadOptions
15
- onProgress?: (progress: number, bytesUploaded: number, totalBytes: number) => void
16
- onComplete?: (response: FileUploadCompleteResponse) => void
17
- onError?: (error: string) => void
18
- }
19
-
20
- export function useLiveUpload(options: UseLiveUploadOptions = {}) {
21
- const { live: liveOptions, upload: uploadOptions, onProgress, onComplete, onError } = options
22
-
23
- const live = Live.use(LiveUpload, {
24
- initialState: LiveUpload.defaultState,
25
- ...liveOptions
26
- })
27
-
28
- const mergedUploadOptions = useMemo<LiveChunkedUploadOptions>(() => {
29
- return {
30
- allowedTypes: [],
31
- maxFileSize: 500 * 1024 * 1024,
32
- adaptiveChunking: true,
33
- fileUrlResolver: (fileUrl) => fileUrl.startsWith('/uploads/') ? `/api${fileUrl}` : fileUrl,
34
- onProgress,
35
- onComplete,
36
- onError,
37
- ...uploadOptions
38
- }
39
- }, [onProgress, onComplete, onError, uploadOptions])
40
-
41
- const upload = useLiveChunkedUpload(live, mergedUploadOptions)
42
-
43
- const startUpload = useMemo(() => {
44
- return async (file: File) => {
45
- if (!live.$connected || !live.$componentId) {
46
- const msg = 'WebSocket nao conectado. Tente novamente.'
47
- onError?.(msg)
48
- await live.failUpload({ error: msg })
49
- return
50
- }
51
- await upload.uploadFile(file)
52
- }
53
- }, [live, upload, onError])
54
-
55
- return {
56
- live,
57
- state: live.$state,
58
- status: live.$state.status,
59
- connected: live.$connected,
60
- componentId: live.$componentId,
61
- uploading: upload.uploading,
62
- progress: upload.progress,
63
- bytesUploaded: upload.bytesUploaded,
64
- totalBytes: upload.totalBytes,
65
- error: live.$state.error,
66
- startUpload,
67
- cancelUpload: upload.cancelUpload,
68
- reset: upload.reset
69
- }
70
- }