@constructive-io/sheets 0.3.0 → 0.4.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.
@@ -2,12 +2,12 @@
2
2
  import {
3
3
  createSheetsExecute,
4
4
  createSheetsUpload
5
- } from "./chunk-H4JJTMP5.js";
5
+ } from "./chunk-SDKHWE7G.js";
6
6
  import {
7
7
  useSheetsContext,
8
8
  useSheetsStore,
9
9
  useSheetsStoreApi
10
- } from "./chunk-4PGPVMZB.js";
10
+ } from "./chunk-FG2LRZP5.js";
11
11
  import {
12
12
  BOOLEAN_TYPES,
13
13
  DATE_TIME_TYPES,
@@ -756,7 +756,7 @@ function transformToPostGraphileVariables(table, options = {}) {
756
756
  variables.orderBy = options.orderBy.filter(({ field }) => availableFields.has(field)).map(({ field, direction }) => toOrderByEnumValue(field, direction));
757
757
  }
758
758
  if (options.where) {
759
- variables.filter = options.where;
759
+ variables.where = options.where;
760
760
  }
761
761
  return variables;
762
762
  }
@@ -2388,7 +2388,7 @@ import { Suspense as Suspense4 } from "react";
2388
2388
 
2389
2389
  // src/grid/editors/image-editor.tsx
2390
2390
  import { useCallback as useCallback3, useContext as useContext2, useEffect as useEffect4, useMemo as useMemo8, useRef as useRef5, useState as useState5 } from "react";
2391
- import { AlertCircleIcon, ImageIcon, ImageOff, Loader2 as Loader22, UploadIcon, XIcon as XIcon2 } from "lucide-react";
2391
+ import { AlertCircleIcon, FileIcon, ImageIcon, ImageOff, Loader2 as Loader22, UploadIcon, XIcon as XIcon2 } from "lucide-react";
2392
2392
  import { AnimatePresence, motion } from "motion/react";
2393
2393
  import { toast } from "@constructive-io/ui/toast";
2394
2394
 
@@ -2520,6 +2520,20 @@ function RemoveButton({ onClick, label }) {
2520
2520
  }
2521
2521
  ) });
2522
2522
  }
2523
+ function FilePreview({ name, size, mime }) {
2524
+ const hasMime = !!mime && mime !== "unknown";
2525
+ return /* @__PURE__ */ jsxs3("div", { className: "flex size-full flex-col items-center justify-center gap-2 p-4 text-center", children: [
2526
+ /* @__PURE__ */ jsx8("div", { className: "bg-muted/60 flex size-12 shrink-0 items-center justify-center rounded-lg", "aria-hidden": "true", children: /* @__PURE__ */ jsx8(FileIcon, { className: "text-muted-foreground size-6" }) }),
2527
+ /* @__PURE__ */ jsxs3("div", { className: "min-w-0 max-w-full", children: [
2528
+ /* @__PURE__ */ jsx8("p", { className: "truncate text-sm font-medium", title: name, children: name || "File" }),
2529
+ (size || hasMime) && /* @__PURE__ */ jsxs3("p", { className: "text-muted-foreground mt-0.5 text-xs", children: [
2530
+ size ? formatFileSize(size) : null,
2531
+ size && hasMime ? " \u2022 " : null,
2532
+ hasMime ? /* @__PURE__ */ jsx8("span", { className: "uppercase", children: mime }) : null
2533
+ ] })
2534
+ ] })
2535
+ ] });
2536
+ }
2523
2537
  var ImageEditor = ({
2524
2538
  value,
2525
2539
  onFinishedEditing,
@@ -2529,8 +2543,11 @@ var ImageEditor = ({
2529
2543
  onSaveComplete,
2530
2544
  isDraftRow = false,
2531
2545
  onSubmitDraft,
2532
- onDraftUploadComplete
2546
+ onDraftUploadComplete,
2547
+ acceptAnyFile = false
2533
2548
  }) => {
2549
+ const noun = acceptAnyFile ? "file" : "image";
2550
+ const Noun = acceptAnyFile ? "File" : "Image";
2534
2551
  const [uploadState, setUploadState] = useState5("idle");
2535
2552
  const { config } = useSheetsContext();
2536
2553
  const storeApi = useSheetsStoreApi();
@@ -2553,10 +2570,12 @@ var ImageEditor = ({
2553
2570
  const isUploading = uploadState === "uploading";
2554
2571
  const isBusy = uploadState !== "idle";
2555
2572
  const imageUrl = useMemo8(() => getImageUrl(imageValue), [imageValue]);
2556
- const imageStatus = useImageStatus(imageUrl || void 0);
2557
- const imageLoadFailed = imageUrl != null && imageStatus === "error";
2558
- const hasImage = imageUrl != null && !imageLoadFailed;
2559
2573
  const imageMetadata = useMemo8(() => getImageMetadata(imageValue), [imageValue]);
2574
+ const storedMime = imageMetadata?.mime;
2575
+ const storedIsImage = !acceptAnyFile || !!storedMime && storedMime.startsWith("image/");
2576
+ const imageStatus = useImageStatus(storedIsImage ? imageUrl || void 0 : void 0);
2577
+ const imageLoadFailed = storedIsImage && imageUrl != null && imageStatus === "error";
2578
+ const hasImage = imageUrl != null && !imageLoadFailed && storedIsImage;
2560
2579
  const revokePreviewUrl = useCallback3(() => {
2561
2580
  if (previewUrlRef.current) {
2562
2581
  URL.revokeObjectURL(previewUrlRef.current);
@@ -2576,8 +2595,8 @@ var ImageEditor = ({
2576
2595
  setImageValue(normalized);
2577
2596
  onSaveComplete?.({ imageData: normalized });
2578
2597
  onFinishedEditing();
2579
- toast.success({ message: "Image uploaded successfully" });
2580
- }, [resetFileState, onSaveComplete, onFinishedEditing]);
2598
+ toast.success({ message: `${Noun} uploaded successfully` });
2599
+ }, [resetFileState, onSaveComplete, onFinishedEditing, Noun]);
2581
2600
  const handleUploadError = useCallback3((error) => {
2582
2601
  setUploadState("idle");
2583
2602
  sheetsLogger().error("[image-editor] upload failed:", error);
@@ -2597,20 +2616,22 @@ var ImageEditor = ({
2597
2616
  const handleFileSelection = useCallback3((acceptedFiles) => {
2598
2617
  if (acceptedFiles.length === 0) return;
2599
2618
  const file = acceptedFiles[0];
2600
- if (!file.type.startsWith("image/")) {
2601
- toast.error({ message: "Please select an image file" });
2602
- return;
2603
- }
2604
- if (file.size > 10 * 1024 * 1024) {
2605
- toast.error({ message: "File size must be less than 10MB" });
2606
- return;
2619
+ if (!acceptAnyFile) {
2620
+ if (!file.type.startsWith("image/")) {
2621
+ toast.error({ message: "Please select an image file" });
2622
+ return;
2623
+ }
2624
+ if (file.size > 10 * 1024 * 1024) {
2625
+ toast.error({ message: "File size must be less than 10MB" });
2626
+ return;
2627
+ }
2607
2628
  }
2608
2629
  revokePreviewUrl();
2609
2630
  setSelectedFile(file);
2610
2631
  const url = URL.createObjectURL(file);
2611
2632
  previewUrlRef.current = url;
2612
2633
  setPreviewUrl(url);
2613
- }, [revokePreviewUrl]);
2634
+ }, [revokePreviewUrl, acceptAnyFile]);
2614
2635
  const handleDragEnter = useCallback3((e) => {
2615
2636
  e.preventDefault();
2616
2637
  e.stopPropagation();
@@ -2645,7 +2666,7 @@ var ImageEditor = ({
2645
2666
  const committedId = committedRecordIdRef.current ?? recordId;
2646
2667
  if (committedRecordIdRef.current != null) {
2647
2668
  try {
2648
- toast.info({ message: "Uploading image..." });
2669
+ toast.info({ message: `Uploading ${noun}...` });
2649
2670
  setUploadState("uploading");
2650
2671
  const uploadResult = await executeFieldUpload(
2651
2672
  tableName,
@@ -2661,7 +2682,7 @@ var ImageEditor = ({
2661
2682
  resetFileState();
2662
2683
  setImageValue(normalized);
2663
2684
  onDraftUploadComplete?.();
2664
- toast.success({ message: "Image uploaded successfully" });
2685
+ toast.success({ message: `${Noun} uploaded successfully` });
2665
2686
  onFinishedEditing();
2666
2687
  } catch (error) {
2667
2688
  onDraftUploadComplete?.();
@@ -2681,7 +2702,7 @@ var ImageEditor = ({
2681
2702
  const result = await onSubmitDraft();
2682
2703
  if (!result.createdRow?.id) throw new Error("Failed to create row");
2683
2704
  committedRecordIdRef.current = result.createdRow.id;
2684
- toast.info({ message: "Uploading image..." });
2705
+ toast.info({ message: `Uploading ${noun}...` });
2685
2706
  setUploadState("uploading");
2686
2707
  const uploadResult = await executeFieldUpload(
2687
2708
  tableName,
@@ -2697,7 +2718,7 @@ var ImageEditor = ({
2697
2718
  resetFileState();
2698
2719
  setImageValue(normalized);
2699
2720
  onDraftUploadComplete?.();
2700
- toast.success({ message: "Row created and image uploaded" });
2721
+ toast.success({ message: `Row created and ${noun} uploaded` });
2701
2722
  onFinishedEditing();
2702
2723
  } catch (error) {
2703
2724
  onDraftUploadComplete?.();
@@ -2712,7 +2733,7 @@ var ImageEditor = ({
2712
2733
  }
2713
2734
  setUploadState("uploading");
2714
2735
  uploadMutation.mutate(selectedFile);
2715
- }, [selectedFile, uploadMutation, tableName, fieldName, recordId, isDraftRow, onSubmitDraft, config.endpoint, config.onAuthError, getToken, resetFileState, onDraftUploadComplete, onFinishedEditing]);
2736
+ }, [selectedFile, uploadMutation, tableName, fieldName, recordId, isDraftRow, onSubmitDraft, config.endpoint, config.onAuthError, getToken, resetFileState, onDraftUploadComplete, onFinishedEditing, noun, Noun]);
2716
2737
  const handleSave = useCallback3(() => {
2717
2738
  if (!imageValue) {
2718
2739
  onFinishedEditing(null);
@@ -2737,12 +2758,13 @@ var ImageEditor = ({
2737
2758
  handleSave();
2738
2759
  }
2739
2760
  }, [handleSave]);
2740
- const dropzoneKey = isBusy ? "busy" : previewUrl ? "preview" : hasImage ? "loaded" : imageLoadFailed ? "broken" : "empty";
2761
+ const hasStoredFile = acceptAnyFile && !!imageUrl && !storedIsImage;
2762
+ const dropzoneKey = isBusy ? "busy" : previewUrl ? "preview" : hasImage ? "loaded" : hasStoredFile ? "file" : imageLoadFailed ? "broken" : "empty";
2741
2763
  const renderDropzoneContent = () => {
2742
2764
  if (isBusy) {
2743
2765
  return /* @__PURE__ */ jsxs3("div", { "data-state": uploadState, className: "text-muted-foreground flex w-full flex-col items-center justify-center p-6", children: [
2744
2766
  /* @__PURE__ */ jsx8(Loader22, { className: "mb-2 size-6 animate-spin" }),
2745
- /* @__PURE__ */ jsx8("p", { className: "text-sm", children: isSubmittingDraft ? "Creating row..." : "Uploading image..." }),
2767
+ /* @__PURE__ */ jsx8("p", { className: "text-sm", children: isSubmittingDraft ? "Creating row..." : `Uploading ${noun}...` }),
2746
2768
  isUploading && uploadProgress && /* @__PURE__ */ jsxs3("div", { className: "mt-3 w-full max-w-xs", children: [
2747
2769
  /* @__PURE__ */ jsx8("div", { className: "bg-muted h-1.5 overflow-hidden rounded-full", children: /* @__PURE__ */ jsx8(
2748
2770
  "div",
@@ -2759,8 +2781,9 @@ var ImageEditor = ({
2759
2781
  ] });
2760
2782
  }
2761
2783
  if (previewUrl) {
2784
+ const selectedIsImage = !acceptAnyFile || (selectedFile?.type.startsWith("image/") ?? false);
2762
2785
  return /* @__PURE__ */ jsxs3("div", { className: "relative w-full", style: { height: "13rem" }, children: [
2763
- /* @__PURE__ */ jsx8("img", { src: previewUrl, alt: selectedFile?.name || "Uploaded image", className: "size-full object-contain p-2" }),
2786
+ selectedIsImage ? /* @__PURE__ */ jsx8("img", { src: previewUrl, alt: selectedFile?.name || "Selected file", className: "size-full object-contain p-2" }) : /* @__PURE__ */ jsx8(FilePreview, { name: selectedFile?.name, size: selectedFile?.size, mime: selectedFile?.type }),
2764
2787
  /* @__PURE__ */ jsx8(RemoveButton, { onClick: removeFile, label: "Remove selected file" })
2765
2788
  ] });
2766
2789
  }
@@ -2779,6 +2802,12 @@ var ImageEditor = ({
2779
2802
  /* @__PURE__ */ jsx8(RemoveButton, { onClick: handleRemoveImage, label: "Remove image" })
2780
2803
  ] });
2781
2804
  }
2805
+ if (hasStoredFile) {
2806
+ return /* @__PURE__ */ jsxs3("div", { className: "relative w-full", style: { height: "13rem" }, children: [
2807
+ /* @__PURE__ */ jsx8(FilePreview, { name: imageMetadata?.filename, size: imageMetadata?.size, mime: imageMetadata?.mime }),
2808
+ /* @__PURE__ */ jsx8(RemoveButton, { onClick: handleRemoveImage, label: "Remove file" })
2809
+ ] });
2810
+ }
2782
2811
  if (imageLoadFailed) {
2783
2812
  return /* @__PURE__ */ jsxs3("div", { className: "flex w-full flex-col items-center justify-center px-4 py-6 text-center", children: [
2784
2813
  /* @__PURE__ */ jsx8(
@@ -2832,8 +2861,12 @@ var ImageEditor = ({
2832
2861
  }
2833
2862
  return /* @__PURE__ */ jsxs3("div", { className: "flex w-full flex-col items-center justify-center px-4 py-6 text-center", children: [
2834
2863
  /* @__PURE__ */ jsx8("div", { className: "bg-muted/60 mb-3 flex size-10 shrink-0 items-center justify-center rounded-full", "aria-hidden": "true", children: /* @__PURE__ */ jsx8(UploadIcon, { className: "text-muted-foreground size-4" }) }),
2835
- /* @__PURE__ */ jsx8("p", { className: "text-sm font-medium", children: "Drop image here or browse" }),
2836
- /* @__PURE__ */ jsx8("p", { className: "text-muted-foreground mt-1 text-xs", children: "SVG, PNG, JPG or GIF (max. 10MB)" }),
2864
+ /* @__PURE__ */ jsxs3("p", { className: "text-sm font-medium", children: [
2865
+ "Drop ",
2866
+ noun,
2867
+ " here or browse"
2868
+ ] }),
2869
+ /* @__PURE__ */ jsx8("p", { className: "text-muted-foreground mt-1 text-xs", children: acceptAnyFile ? "Any file type" : "SVG, PNG, JPG or GIF (max. 10MB)" }),
2837
2870
  /* @__PURE__ */ jsxs3(
2838
2871
  Button3,
2839
2872
  {
@@ -2847,7 +2880,8 @@ var ImageEditor = ({
2847
2880
  disabled: isBusy,
2848
2881
  children: [
2849
2882
  /* @__PURE__ */ jsx8(UploadIcon, { className: "-ms-0.5 opacity-60", "aria-hidden": "true" }),
2850
- "Select image"
2883
+ "Select ",
2884
+ noun
2851
2885
  ]
2852
2886
  }
2853
2887
  )
@@ -2862,17 +2896,17 @@ var ImageEditor = ({
2862
2896
  style: overlayMaxHeight > 0 ? { maxHeight: overlayMaxHeight } : void 0,
2863
2897
  children: /* @__PURE__ */ jsxs3("div", { onKeyDown: handleEditorKeyDown, className: "flex min-h-0 flex-1 flex-col", children: [
2864
2898
  /* @__PURE__ */ jsxs3("div", { className: "flex shrink-0 items-center gap-2 border-b px-6 py-3", children: [
2865
- /* @__PURE__ */ jsx8(ImageIcon, { className: "text-muted-foreground size-4" }),
2866
- /* @__PURE__ */ jsx8("h3", { className: "text-sm font-medium", children: imageUrl ? "Edit Image" : "Add Image" })
2899
+ acceptAnyFile ? /* @__PURE__ */ jsx8(FileIcon, { className: "text-muted-foreground size-4" }) : /* @__PURE__ */ jsx8(ImageIcon, { className: "text-muted-foreground size-4" }),
2900
+ /* @__PURE__ */ jsx8("h3", { className: "text-sm font-medium", children: imageUrl ? `Edit ${Noun}` : `Add ${Noun}` })
2867
2901
  ] }),
2868
2902
  /* @__PURE__ */ jsx8(
2869
2903
  "input",
2870
2904
  {
2871
2905
  ref: fileInputRef,
2872
2906
  type: "file",
2873
- accept: "image/*",
2907
+ accept: acceptAnyFile ? void 0 : "image/*",
2874
2908
  className: "hidden",
2875
- "aria-label": "Upload image file",
2909
+ "aria-label": `Upload ${noun}`,
2876
2910
  tabIndex: -1,
2877
2911
  onChange: (e) => {
2878
2912
  const files = Array.from(e.target.files || []);
@@ -2946,6 +2980,7 @@ import { jsx as jsx9 } from "react/jsx-runtime";
2946
2980
  var DRAFT_PREFIX = "draft:";
2947
2981
  function ImageEditorDom({
2948
2982
  value,
2983
+ cell,
2949
2984
  colKey,
2950
2985
  rowId,
2951
2986
  tableName,
@@ -2955,6 +2990,8 @@ function ImageEditorDom({
2955
2990
  onInvalidateData
2956
2991
  }) {
2957
2992
  const isDraftRow = rowId.startsWith(DRAFT_PREFIX);
2993
+ const cellType = cell.meta?.cellType;
2994
+ const acceptAnyFile = cellType != null && cellType !== "image";
2958
2995
  const handleSaveComplete = onCommitPatch ? (saveData) => onCommitPatch({ [colKey]: saveData.imageData }) : void 0;
2959
2996
  const handleSubmitDraft = isDraftRow && onSubmitDraftRow ? async () => {
2960
2997
  const createdRow = await onSubmitDraftRow(rowId);
@@ -2964,6 +3001,7 @@ function ImageEditorDom({
2964
3001
  ImageEditor,
2965
3002
  {
2966
3003
  value,
3004
+ acceptAnyFile,
2967
3005
  onFinishedEditing: () => onCancel(),
2968
3006
  tableName,
2969
3007
  fieldName: colKey,
@@ -4480,6 +4518,7 @@ export {
4480
4518
  useSheetsAdapter,
4481
4519
  useSheetsMeta,
4482
4520
  createSheetsCell,
4521
+ getImageMetadata,
4483
4522
  OverlayMeasureContext,
4484
4523
  computeOverlayGeometry,
4485
4524
  OverlayViewportGuard,
@@ -4491,4 +4530,4 @@ export {
4491
4530
  DOM_EDITOR_REGISTRY,
4492
4531
  resolveNativeEditor
4493
4532
  };
4494
- //# sourceMappingURL=chunk-TU7ATZ4X.js.map
4533
+ //# sourceMappingURL=chunk-BWKB52JB.js.map