@startsimpli/hooks 0.4.18 → 0.4.19
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.
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/useAttachments.ts +51 -0
- package/src/useDocuments.ts +81 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@startsimpli/hooks",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.19",
|
|
4
4
|
"description": "Shared React hooks for StartSimpli apps",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@tanstack/react-query": ">=5.0.0",
|
|
24
24
|
"react": ">=18.0.0",
|
|
25
25
|
"react-native": ">=0.74.0",
|
|
26
|
-
"@startsimpli/api": "0.5.
|
|
26
|
+
"@startsimpli/api": "0.5.28"
|
|
27
27
|
},
|
|
28
28
|
"peerDependenciesMeta": {
|
|
29
29
|
"@tanstack/react-query": {
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"tsup": "^8.5.1",
|
|
47
47
|
"typescript": "^6.0.3",
|
|
48
48
|
"vitest": "^4.1.5",
|
|
49
|
-
"@startsimpli/api": "0.5.
|
|
49
|
+
"@startsimpli/api": "0.5.28"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsup",
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
import type { AttachmentsApi, UploadAttachmentOptions } from '@startsimpli/api'
|
|
3
|
+
|
|
4
|
+
/** TanStack Query hooks over AttachmentsApi (the document-scoped
|
|
5
|
+
* `api/v1/documents/{id}/attachments/` backend). Each hook takes the
|
|
6
|
+
* AttachmentsApi instance (e.g. `api.attachments`) + the owning document id,
|
|
7
|
+
* matching the useDocuments / usePresentations convention.
|
|
8
|
+
*
|
|
9
|
+
* Upload is a multipart mutation that exposes per-upload progress via the
|
|
10
|
+
* client's `onProgress` callback; on success the owning message/session's
|
|
11
|
+
* attachment list is invalidated so chips refresh.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const attachmentsKey = (id: string) => ['attachments', 'list', id] as const
|
|
15
|
+
|
|
16
|
+
function isRealId(id: string | undefined): id is string {
|
|
17
|
+
return !!id && id !== 'new'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** List attachments for a message/session id. */
|
|
21
|
+
export function useAttachments(api: AttachmentsApi, id: string) {
|
|
22
|
+
return useQuery({
|
|
23
|
+
queryKey: attachmentsKey(id),
|
|
24
|
+
queryFn: () => api.listAttachments(id),
|
|
25
|
+
enabled: isRealId(id),
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Upload a file to a message/session (multipart). Pass `onProgress` per upload
|
|
30
|
+
* to drive a chip progress bar. Invalidates the owning id's attachment list. */
|
|
31
|
+
export function useUploadAttachment(api: AttachmentsApi, id: string) {
|
|
32
|
+
const qc = useQueryClient()
|
|
33
|
+
return useMutation({
|
|
34
|
+
mutationFn: ({ file, ...options }: { file: File | Blob } & UploadAttachmentOptions) =>
|
|
35
|
+
api.uploadAttachment(id, file, options),
|
|
36
|
+
onSuccess: () => {
|
|
37
|
+
qc.invalidateQueries({ queryKey: attachmentsKey(id) })
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Delete an attachment by id; invalidates the owning id's attachment list. */
|
|
43
|
+
export function useDeleteAttachment(api: AttachmentsApi, id: string) {
|
|
44
|
+
const qc = useQueryClient()
|
|
45
|
+
return useMutation({
|
|
46
|
+
mutationFn: (attachmentId: string) => api.deleteAttachment(id, attachmentId),
|
|
47
|
+
onSuccess: () => {
|
|
48
|
+
qc.invalidateQueries({ queryKey: attachmentsKey(id) })
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
import type {
|
|
3
|
+
DocumentsApi,
|
|
4
|
+
DocumentListParams,
|
|
5
|
+
DocumentStatus,
|
|
6
|
+
RegenerateSectionInput,
|
|
7
|
+
} from '@startsimpli/api'
|
|
8
|
+
|
|
9
|
+
/** TanStack Query hooks over DocumentsApi (the live `api/v1/documents/` backend).
|
|
10
|
+
*
|
|
11
|
+
* Each hook takes the DocumentsApi instance (e.g. `api.documents`), matching the
|
|
12
|
+
* usePresentations / useWorkflows convention. Compile + section regeneration are
|
|
13
|
+
* async on the backend, so the document detail and the lightweight status query
|
|
14
|
+
* are invalidated together and the status query self-polls while in flight.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const DOCUMENTS_KEY = ['documents', 'list'] as const
|
|
18
|
+
const documentKey = (id: string) => ['documents', 'detail', id]
|
|
19
|
+
const documentStatusKey = (id: string) => ['documents', 'status', id]
|
|
20
|
+
|
|
21
|
+
const POLL_MS = 1500
|
|
22
|
+
|
|
23
|
+
/** Treat an empty / 'new' (unsaved draft) id as "no document yet" — don't fetch. */
|
|
24
|
+
function isRealId(id: string | undefined): id is string {
|
|
25
|
+
return !!id && id !== 'new'
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Refetch interval for the status query: poll while work is in flight. */
|
|
29
|
+
export function documentStatusRefetchInterval(status: DocumentStatus | undefined): number | false {
|
|
30
|
+
return status === 'generating' || status === 'draft' ? POLL_MS : false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function invalidateDocument(qc: ReturnType<typeof useQueryClient>, id: string) {
|
|
34
|
+
qc.invalidateQueries({ queryKey: documentKey(id) })
|
|
35
|
+
qc.invalidateQueries({ queryKey: documentStatusKey(id) })
|
|
36
|
+
qc.invalidateQueries({ queryKey: DOCUMENTS_KEY })
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function useDocuments(api: DocumentsApi, params?: DocumentListParams) {
|
|
40
|
+
return useQuery({
|
|
41
|
+
queryKey: ['documents', 'list', params],
|
|
42
|
+
queryFn: () => api.listDocuments(params),
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useDocument(api: DocumentsApi, id: string) {
|
|
47
|
+
return useQuery({
|
|
48
|
+
queryKey: documentKey(id),
|
|
49
|
+
queryFn: () => api.getDocument(id),
|
|
50
|
+
enabled: isRealId(id),
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Self-polling document + per-section status; stops when ready/error. */
|
|
55
|
+
export function useDocumentStatus(api: DocumentsApi, id: string, enabled = true) {
|
|
56
|
+
return useQuery({
|
|
57
|
+
queryKey: documentStatusKey(id),
|
|
58
|
+
queryFn: () => api.getDocumentStatus(id),
|
|
59
|
+
enabled: enabled && isRealId(id),
|
|
60
|
+
refetchInterval: (query) => documentStatusRefetchInterval(query.state.data?.status),
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Build the deck and export PPTX + PDF; invalidates detail + status. */
|
|
65
|
+
export function useCompileDocument(api: DocumentsApi, id: string) {
|
|
66
|
+
const qc = useQueryClient()
|
|
67
|
+
return useMutation({
|
|
68
|
+
mutationFn: () => api.compileDocument(id),
|
|
69
|
+
onSuccess: () => invalidateDocument(qc, id),
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Regenerate one section by order; invalidates detail + status. */
|
|
74
|
+
export function useRegenerateSection(api: DocumentsApi, id: string) {
|
|
75
|
+
const qc = useQueryClient()
|
|
76
|
+
return useMutation({
|
|
77
|
+
mutationFn: ({ order, ...data }: { order: number } & RegenerateSectionInput) =>
|
|
78
|
+
api.regenerateSection(id, order, data),
|
|
79
|
+
onSuccess: () => invalidateDocument(qc, id),
|
|
80
|
+
})
|
|
81
|
+
}
|