@startsimpli/hooks 0.4.16 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startsimpli/hooks",
3
- "version": "0.4.16",
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.23"
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.23"
49
+ "@startsimpli/api": "0.5.28"
50
50
  },
51
51
  "scripts": {
52
52
  "build": "tsup",
@@ -2,7 +2,15 @@ import { describe, it, expect, vi } from 'vitest'
2
2
  import { renderHook, act, waitFor } from '@testing-library/react'
3
3
  import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
4
4
  import React from 'react'
5
- import { useCreateSecret, useDeleteSecret, useUpdateSecret } from '../useVault'
5
+ import {
6
+ useAssignEnvironmentProject,
7
+ useCreateSecret,
8
+ useDeleteProject,
9
+ useDeleteSecret,
10
+ useLinkSharedKey,
11
+ useRotateSharedKey,
12
+ useUpdateSecret,
13
+ } from '../useVault'
6
14
  import type { VaultApi } from '@startsimpli/api'
7
15
 
8
16
  /** startsim-drl: secret mutations must invalidate the env LIST (carries
@@ -105,3 +113,76 @@ describe('secret mutations also invalidate environment queries (startsim-drl)',
105
113
  )
106
114
  })
107
115
  })
116
+
117
+ describe('projects + global keys hooks (startsim-oftw, startsim-1m4g)', () => {
118
+ it('useAssignEnvironmentProject patches the env and refreshes env + project lists', async () => {
119
+ const qc = mkClient()
120
+ const seen = trackInvalidations(qc)
121
+ const api = fakeApi({ updateEnvironment: vi.fn().mockResolvedValue({ slug: 'e1', project: 'alpha' }) })
122
+ const { result } = renderHook(() => useAssignEnvironmentProject(api), { wrapper: wrap(qc) })
123
+
124
+ await act(async () => {
125
+ result.current.mutate({ slug: 'e1', project: 'alpha' })
126
+ })
127
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
128
+
129
+ expect(api.updateEnvironment).toHaveBeenCalledWith('e1', { project: 'alpha' })
130
+ expect(seen).toEqual(
131
+ expect.arrayContaining([['vault', 'environments'], ['vault', 'projects']]),
132
+ )
133
+ })
134
+
135
+ it('useDeleteProject refreshes both projects and environments (orphaned to Ungrouped)', async () => {
136
+ const qc = mkClient()
137
+ const seen = trackInvalidations(qc)
138
+ const api = fakeApi({ deleteProject: vi.fn().mockResolvedValue(undefined) })
139
+ const { result } = renderHook(() => useDeleteProject(api), { wrapper: wrap(qc) })
140
+
141
+ await act(async () => {
142
+ result.current.mutate('alpha')
143
+ })
144
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
145
+
146
+ expect(seen).toEqual(
147
+ expect.arrayContaining([['vault', 'projects'], ['vault', 'environments']]),
148
+ )
149
+ })
150
+
151
+ it('useRotateSharedKey invalidates shared-secrets, links and secrets so linked envs refresh', async () => {
152
+ const qc = mkClient()
153
+ const seen = trackInvalidations(qc)
154
+ const api = fakeApi({ rotateSharedSecret: vi.fn().mockResolvedValue({ id: 'ss1' }) })
155
+ const { result } = renderHook(() => useRotateSharedKey(api), { wrapper: wrap(qc) })
156
+
157
+ await act(async () => {
158
+ result.current.mutate({ id: 'ss1', value: 'sk-new' })
159
+ })
160
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
161
+
162
+ expect(api.rotateSharedSecret).toHaveBeenCalledWith('ss1', 'sk-new')
163
+ expect(seen).toEqual(
164
+ expect.arrayContaining([
165
+ ['vault', 'shared-secrets'],
166
+ ['vault', 'links'],
167
+ ['vault', 'secrets'],
168
+ ]),
169
+ )
170
+ })
171
+
172
+ it('useLinkSharedKey links and refreshes the env links + shared-secret counts', async () => {
173
+ const qc = mkClient()
174
+ const seen = trackInvalidations(qc)
175
+ const api = fakeApi({ linkSharedSecret: vi.fn().mockResolvedValue({ id: 'l1' }) })
176
+ const { result } = renderHook(() => useLinkSharedKey(api, 'prod'), { wrapper: wrap(qc) })
177
+
178
+ await act(async () => {
179
+ result.current.mutate('ss1')
180
+ })
181
+ await waitFor(() => expect(result.current.isSuccess).toBe(true))
182
+
183
+ expect(api.linkSharedSecret).toHaveBeenCalledWith('prod', 'ss1')
184
+ expect(seen).toEqual(
185
+ expect.arrayContaining([['vault', 'links', 'prod'], ['vault', 'shared-secrets']]),
186
+ )
187
+ })
188
+ })
package/src/index.ts CHANGED
@@ -70,7 +70,13 @@ export { useAsyncOptions } from './useAsyncOptions'
70
70
  export type { UseAsyncOptionsResult } from './useAsyncOptions'
71
71
 
72
72
  // Vault — environments, secrets, access keys, audit (startsim-d30.3.2)
73
+ // + projects + global/shared keys (startsim-oftw, startsim-1m4g)
73
74
  export {
75
+ useProjects,
76
+ useCreateProject,
77
+ useUpdateProject,
78
+ useDeleteProject,
79
+ useAssignEnvironmentProject,
74
80
  useEnvironments,
75
81
  useEnvironment,
76
82
  useCreateEnvironment,
@@ -81,6 +87,14 @@ export {
81
87
  useUpdateSecret,
82
88
  useDeleteSecret,
83
89
  useRevealSecret,
90
+ useSharedKeys,
91
+ useCreateSharedKey,
92
+ useUpdateSharedKey,
93
+ useDeleteSharedKey,
94
+ useRotateSharedKey,
95
+ useEnvironmentLinks,
96
+ useLinkSharedKey,
97
+ useUnlinkSharedKey,
84
98
  useAccessKeys,
85
99
  useCreateAccessKey,
86
100
  useDeleteAccessKey,
@@ -88,3 +102,5 @@ export {
88
102
  } from './useVault'
89
103
  export * from './usePresentations'
90
104
  export * from './useWorkflows'
105
+ export * from './useDocuments'
106
+ export * from './useAttachments'
@@ -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
+ }
package/src/useVault.ts CHANGED
@@ -4,7 +4,9 @@ import type {
4
4
  VaultApi,
5
5
  VaultEnvironmentInput,
6
6
  VaultListParams,
7
+ VaultProjectInput,
7
8
  VaultSecretInput,
9
+ VaultSharedSecretInput,
8
10
  } from '@startsimpli/api'
9
11
 
10
12
  /** TanStack Query hooks over VaultApi (startsim-d30.3.2).
@@ -15,8 +17,11 @@ import type {
15
17
  */
16
18
 
17
19
  const ENVIRONMENTS_KEY = ['vault', 'environments'] as const
20
+ const PROJECTS_KEY = ['vault', 'projects'] as const
21
+ const SHARED_SECRETS_KEY = ['vault', 'shared-secrets'] as const
18
22
  const environmentKey = (slug: string) => ['vault', 'environment', slug]
19
23
  const secretsKey = (slug: string) => ['vault', 'secrets', slug]
24
+ const linksKey = (slug: string) => ['vault', 'links', slug]
20
25
  const accessKeysKey = (slug: string) => ['vault', 'access-keys', slug]
21
26
 
22
27
  /** Secret mutations alter the env's `secret_count`, so refresh the list +
@@ -28,6 +33,57 @@ function invalidateSecretQueries(qc: ReturnType<typeof useQueryClient>, slug: st
28
33
  qc.invalidateQueries({ queryKey: environmentKey(slug) })
29
34
  }
30
35
 
36
+ // --- Projects ---
37
+ export function useProjects(api: VaultApi, params?: VaultListParams) {
38
+ return useQuery({
39
+ queryKey: ['vault', 'projects', params],
40
+ queryFn: () => api.listProjects(params),
41
+ })
42
+ }
43
+
44
+ export function useCreateProject(api: VaultApi) {
45
+ const qc = useQueryClient()
46
+ return useMutation({
47
+ mutationFn: (data: VaultProjectInput) => api.createProject(data),
48
+ onSuccess: () => qc.invalidateQueries({ queryKey: PROJECTS_KEY }),
49
+ })
50
+ }
51
+
52
+ export function useUpdateProject(api: VaultApi) {
53
+ const qc = useQueryClient()
54
+ return useMutation({
55
+ mutationFn: ({ slug, data }: { slug: string; data: Partial<VaultProjectInput> }) =>
56
+ api.updateProject(slug, data),
57
+ onSuccess: () => qc.invalidateQueries({ queryKey: PROJECTS_KEY }),
58
+ })
59
+ }
60
+
61
+ export function useDeleteProject(api: VaultApi) {
62
+ const qc = useQueryClient()
63
+ return useMutation({
64
+ mutationFn: (slug: string) => api.deleteProject(slug),
65
+ onSuccess: () => {
66
+ // Deleting a project orphans its envs to Ungrouped → refresh both lists.
67
+ qc.invalidateQueries({ queryKey: PROJECTS_KEY })
68
+ qc.invalidateQueries({ queryKey: ENVIRONMENTS_KEY })
69
+ },
70
+ })
71
+ }
72
+
73
+ /** Assign (or clear) an environment's project. Thin wrapper over
74
+ * updateEnvironment so callers read as intent, not a generic patch. */
75
+ export function useAssignEnvironmentProject(api: VaultApi) {
76
+ const qc = useQueryClient()
77
+ return useMutation({
78
+ mutationFn: ({ slug, project }: { slug: string; project: string | null }) =>
79
+ api.updateEnvironment(slug, { project }),
80
+ onSuccess: () => {
81
+ qc.invalidateQueries({ queryKey: ENVIRONMENTS_KEY })
82
+ qc.invalidateQueries({ queryKey: PROJECTS_KEY })
83
+ },
84
+ })
85
+ }
86
+
31
87
  // --- Environments ---
32
88
  export function useEnvironments(api: VaultApi, params?: VaultListParams) {
33
89
  return useQuery({
@@ -132,6 +188,95 @@ export function useDeleteAccessKey(api: VaultApi, slug: string) {
132
188
  })
133
189
  }
134
190
 
191
+ // --- Shared secrets (global keys) ---
192
+ export function useSharedKeys(api: VaultApi, params?: VaultListParams) {
193
+ return useQuery({
194
+ queryKey: ['vault', 'shared-secrets', params],
195
+ queryFn: () => api.listSharedSecrets(params),
196
+ })
197
+ }
198
+
199
+ export function useCreateSharedKey(api: VaultApi) {
200
+ const qc = useQueryClient()
201
+ return useMutation({
202
+ mutationFn: (data: VaultSharedSecretInput) => api.createSharedSecret(data),
203
+ onSuccess: () => {
204
+ // Creating with a default_project can auto-link envs → refresh broadly.
205
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
206
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
207
+ },
208
+ })
209
+ }
210
+
211
+ export function useUpdateSharedKey(api: VaultApi) {
212
+ const qc = useQueryClient()
213
+ return useMutation({
214
+ mutationFn: ({ id, data }: { id: string; data: Partial<VaultSharedSecretInput> }) =>
215
+ api.updateSharedSecret(id, data),
216
+ onSuccess: () => {
217
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
218
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
219
+ },
220
+ })
221
+ }
222
+
223
+ export function useDeleteSharedKey(api: VaultApi) {
224
+ const qc = useQueryClient()
225
+ return useMutation({
226
+ mutationFn: (id: string) => api.deleteSharedSecret(id),
227
+ onSuccess: () => {
228
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
229
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
230
+ },
231
+ })
232
+ }
233
+
234
+ /** Rotate a shared key's single source value. Every linked env sees it on its
235
+ * next exchange — so invalidate shared-secret + all link/secret views. */
236
+ export function useRotateSharedKey(api: VaultApi) {
237
+ const qc = useQueryClient()
238
+ return useMutation({
239
+ mutationFn: ({ id, value }: { id: string; value: string }) =>
240
+ api.rotateSharedSecret(id, value),
241
+ onSuccess: () => {
242
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
243
+ qc.invalidateQueries({ queryKey: ['vault', 'links'] })
244
+ qc.invalidateQueries({ queryKey: ['vault', 'secrets'] })
245
+ },
246
+ })
247
+ }
248
+
249
+ // --- Secret links (per-environment) ---
250
+ export function useEnvironmentLinks(api: VaultApi, slug: string, params?: VaultListParams) {
251
+ return useQuery({
252
+ queryKey: ['vault', 'links', slug, params],
253
+ queryFn: () => api.listLinks(slug, params),
254
+ enabled: !!slug,
255
+ })
256
+ }
257
+
258
+ export function useLinkSharedKey(api: VaultApi, slug: string) {
259
+ const qc = useQueryClient()
260
+ return useMutation({
261
+ mutationFn: (sharedSecretId: string) => api.linkSharedSecret(slug, sharedSecretId),
262
+ onSuccess: () => {
263
+ qc.invalidateQueries({ queryKey: linksKey(slug) })
264
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY }) // linkedEnvCount changed
265
+ },
266
+ })
267
+ }
268
+
269
+ export function useUnlinkSharedKey(api: VaultApi, slug: string) {
270
+ const qc = useQueryClient()
271
+ return useMutation({
272
+ mutationFn: (linkId: string) => api.unlinkSharedSecret(slug, linkId),
273
+ onSuccess: () => {
274
+ qc.invalidateQueries({ queryKey: linksKey(slug) })
275
+ qc.invalidateQueries({ queryKey: SHARED_SECRETS_KEY })
276
+ },
277
+ })
278
+ }
279
+
135
280
  // --- Audit ---
136
281
  export function useAuditLog(api: VaultApi, slug: string, params?: VaultListParams) {
137
282
  return useQuery({