@fabrika/annotations 0.9.0-dev.11 → 0.9.0-dev.13

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": "@fabrika/annotations",
3
- "version": "0.9.0-dev.11",
3
+ "version": "0.9.0-dev.13",
4
4
  "type": "module",
5
5
  "description": "React annotation state, text/block/region locators, editor and feedback surface.",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  }
44
44
  },
45
45
  "scripts": {
46
- "typecheck": "tsc --noEmit",
46
+ "typecheck": "tsrs --build tsconfig.test.json",
47
47
  "test": "bun test"
48
48
  },
49
49
  "peerDependencies": {
@@ -1,7 +1,7 @@
1
1
  import { type AnnotationHost, useAnnotationHost } from '@fabrika/annotations/runtime'
2
2
  import React, { useCallback, useEffect, useRef, useState } from 'react'
3
- import { localUploadResponseSchema, sharedUploadResponseSchema } from './schemas'
4
- import { autoResizeTextarea, hostAcceptsUploads } from './utils'
3
+ import { localUploadResponseSchema, sharedUploadResponseSchema, uploadErrorResponseSchema } from './schemas'
4
+ import { ATTACHABLE_IMAGE_REFUSED, ATTACHABLE_IMAGE_TYPES, autoResizeTextarea, hostAcceptsUploads } from './utils'
5
5
 
6
6
  interface AnnotationEditorProps {
7
7
  note: string
@@ -38,20 +38,25 @@ export function imageToUrl(path: string): string {
38
38
  return `/api/uploads/${filename}`
39
39
  }
40
40
 
41
- async function uploadImage(host: AnnotationHost, file: File): Promise<string | null> {
41
+ type UploadResult = { path: string } | { error: string }
42
+
43
+ async function uploadImage(host: AnnotationHost, file: File): Promise<UploadResult> {
42
44
  const endpoint = host.uploadUrl
43
- if (!endpoint) return null
45
+ if (!endpoint) return { error: 'Uploads are not available here' }
44
46
  const formData = new FormData()
45
47
  formData.append('image', file)
46
48
  try {
47
49
  const resp = await fetch(endpoint(), { method: 'POST', body: formData })
48
50
  const data: unknown = await resp.json()
51
+ if (!resp.ok) {
52
+ const refused = uploadErrorResponseSchema.safeParse(data)
53
+ return { error: refused.success ? refused.data.error : `Upload failed (HTTP ${resp.status})` }
54
+ }
49
55
  // Local daemon returns { path }; worker returns { url }. Normalise to
50
56
  // a single string that `imageToUrl` can resolve.
51
- if (host.isShared) return sharedUploadResponseSchema.parse(data).url || null
52
- return localUploadResponseSchema.parse(data).path || null
57
+ return { path: host.isShared ? sharedUploadResponseSchema.parse(data).url : localUploadResponseSchema.parse(data).path }
53
58
  } catch {
54
- return null
59
+ return { error: 'Upload failed' }
55
60
  }
56
61
  }
57
62
 
@@ -81,6 +86,7 @@ export function AnnotationEditor({
81
86
  const canAttach = !readOnly && hostAcceptsUploads(host)
82
87
  const [isDragging, setIsDragging] = useState(false)
83
88
  const [isUploading, setIsUploading] = useState(false)
89
+ const [uploadError, setUploadError] = useState<string | null>(null)
84
90
  const [isFocused, setIsFocused] = useState(false)
85
91
  const dragCounter = useRef(0)
86
92
  const internalRef = useRef<HTMLTextAreaElement | null>(null)
@@ -106,11 +112,16 @@ export function AnnotationEditor({
106
112
  }, [autoFocus])
107
113
 
108
114
  const doUpload = useCallback(async (file: File) => {
109
- if (!file.type.startsWith('image/')) return
115
+ if (!ATTACHABLE_IMAGE_TYPES.includes(file.type)) {
116
+ setUploadError(ATTACHABLE_IMAGE_REFUSED)
117
+ return
118
+ }
119
+ setUploadError(null)
110
120
  setIsUploading(true)
111
121
  try {
112
- const path = await uploadImage(host, file)
113
- if (path) onAddImage(path)
122
+ const result = await uploadImage(host, file)
123
+ if ('path' in result) onAddImage(result.path)
124
+ else setUploadError(result.error)
114
125
  } finally {
115
126
  setIsUploading(false)
116
127
  }
@@ -211,7 +222,7 @@ export function AnnotationEditor({
211
222
  <input
212
223
  ref={fileInputRef}
213
224
  type="file"
214
- accept="image/*"
225
+ accept={ATTACHABLE_IMAGE_TYPES.join(',')}
215
226
  multiple
216
227
  style={{ display: 'none' }}
217
228
  onChange={(e) => {
@@ -291,6 +302,20 @@ export function AnnotationEditor({
291
302
  </div>
292
303
  )}
293
304
 
305
+ {uploadError && (
306
+ <div
307
+ role="alert"
308
+ style={{
309
+ fontSize: '11px',
310
+ color: 'var(--color-accent-red)',
311
+ fontFamily: "'Inter', sans-serif",
312
+ marginTop: '4px',
313
+ }}
314
+ >
315
+ {uploadError}
316
+ </div>
317
+ )}
318
+
294
319
  {isDragging && (
295
320
  <div
296
321
  style={{
package/src/schemas.ts CHANGED
@@ -37,3 +37,4 @@ export const annotationStateSchema = z.object({
37
37
 
38
38
  export const localUploadResponseSchema = z.object({ path: z.string() })
39
39
  export const sharedUploadResponseSchema = z.object({ url: z.string() })
40
+ export const uploadErrorResponseSchema = z.object({ error: z.string() })
package/src/utils.ts CHANGED
@@ -21,6 +21,11 @@ export function hostAcceptsUploads(host: { uploadUrl: (() => string) | null }):
21
21
  return host.uploadUrl !== null
22
22
  }
23
23
 
24
+ /** The image types the upload endpoints accept; they refuse every other file. */
25
+ export const ATTACHABLE_IMAGE_TYPES = ['image/png', 'image/jpeg', 'image/gif', 'image/webp']
26
+
27
+ export const ATTACHABLE_IMAGE_REFUSED = 'Only PNG, JPEG, GIF and WebP images can be attached'
28
+
24
29
  /** Generate a unique annotation ID */
25
30
  export function generateAnnotationId(): string {
26
31
  return `ann-${Date.now()}-${Math.random().toString(36).slice(2, 6)}`
@@ -976,3 +976,10 @@ export declare const sharedUploadResponseSchema: z.ZodObject<{
976
976
  }, {
977
977
  url: string;
978
978
  }>;
979
+ export declare const uploadErrorResponseSchema: z.ZodObject<{
980
+ error: z.ZodString;
981
+ }, "strip", z.ZodTypeAny, {
982
+ error: string;
983
+ }, {
984
+ error: string;
985
+ }>;
@@ -18,6 +18,9 @@ export declare function fileAnnotationPath(ann: {
18
18
  export declare function hostAcceptsUploads(host: {
19
19
  uploadUrl: (() => string) | null;
20
20
  }): boolean;
21
+ /** The image types the upload endpoints accept; they refuse every other file. */
22
+ export declare const ATTACHABLE_IMAGE_TYPES: string[];
23
+ export declare const ATTACHABLE_IMAGE_REFUSED = "Only PNG, JPEG, GIF and WebP images can be attached";
21
24
  /** Generate a unique annotation ID */
22
25
  export declare function generateAnnotationId(): string;
23
26
  /** Auto-resize a textarea to fit its content */