Skip to content

armgit / ui

Public Imported from GitHub
0
TypeScript

Improve Human Review grid editing performance

Andrew Luof15f49ddddc8
diff --git a/apps/v4/app/layout.tsx b/apps/v4/app/layout.tsx
index 38f3c99..fa046d6 100644
--- a/apps/v4/app/layout.tsx
+++ b/apps/v4/app/layout.tsx
@@ -57,7 +57,7 @@ export const metadata: Metadata = {
shortcut: [{ url: "/extend.svg", type: "image/svg+xml" }],
apple: "/apple-touch-icon.png",
},
- manifest: `${siteConfig.url}/site.webmanifest`,
+ manifest: "/site.webmanifest",
}
export default function RootLayout({
diff --git a/apps/v4/components/human-review-docs.tsx b/apps/v4/components/human-review-docs.tsx
index 685a109..2229edb 100644
--- a/apps/v4/components/human-review-docs.tsx
+++ b/apps/v4/components/human-review-docs.tsx
@@ -5,13 +5,15 @@ import {
DataEditor,
emptyGridSelection,
GridCellKind,
- type DrawCellCallback,
type EditableGridCell,
type GridCell,
type GridColumn,
type GridSelection,
type Item,
+ type NumberCell,
+ type ProvideEditorComponent,
type Rectangle,
+ type TextCell,
type Theme,
} from "@glideapps/glide-data-grid"
import {
@@ -561,6 +563,202 @@ function areGridSelectionsEqual(left: GridSelection, right: GridSelection) {
)
}
+function getGridSelectionRanges(selection: GridSelection | undefined) {
+ const current = selection?.current
+ if (!current) return []
+
+ return [...(current.rangeStack ?? []), current.range]
+}
+
+function areArrayNestedViewsEqual(
+ left: ArrayNestedView[],
+ right: ArrayNestedView[]
+) {
+ if (left === right) return true
+ if (left.length !== right.length) return false
+
+ return left.every((view, index) => {
+ const other = right[index]
+
+ return (
+ view.rowIndex === other?.rowIndex &&
+ view.columnId === other.columnId &&
+ view.title === other.title &&
+ view.schema === other.schema &&
+ Object.is(view.value, other.value)
+ )
+ })
+}
+
+type HumanReviewOverlayCell = TextCell | NumberCell
+
+type HumanReviewTextOverlayEditorProps = React.ComponentProps<
+ ProvideEditorComponent<HumanReviewOverlayCell>
+> & {
+ overlayOpenRef: React.RefObject<boolean>
+ readOnly?: boolean
+}
+
+function HumanReviewTextOverlayEditor({
+ isHighlighted,
+ onFinishedEditing,
+ overlayOpenRef,
+ readOnly = false,
+ validatedSelection,
+ value,
+}: HumanReviewTextOverlayEditorProps) {
+ const inputRef = React.useRef<HTMLTextAreaElement | null>(null)
+ const initialValue =
+ value.kind === GridCellKind.Number ? value.displayData : value.data
+ const latestValueRef = React.useRef(initialValue)
+ const finishedRef = React.useRef(false)
+
+ const finishEditing = React.useCallback(
+ (
+ shouldSave: boolean,
+ movement: readonly [-1 | 0 | 1, -1 | 0 | 1] = [0, 0]
+ ) => {
+ if (finishedRef.current) return
+
+ finishedRef.current = true
+ overlayOpenRef.current = false
+
+ if (!shouldSave || readOnly) {
+ onFinishedEditing(undefined, movement)
+ return
+ }
+
+ if (value.kind === GridCellKind.Number) {
+ const numericValue = Number(latestValueRef.current)
+
+ onFinishedEditing(
+ {
+ ...value,
+ data: Number.isFinite(numericValue) ? numericValue : value.data,
+ displayData: latestValueRef.current,
+ },
+ movement
+ )
+ return
+ }
+
+ onFinishedEditing(
+ {
+ ...value,
+ data: latestValueRef.current,
+ displayData: latestValueRef.current,
+ },
+ movement
+ )
+ },
+ [onFinishedEditing, overlayOpenRef, readOnly, value]
+ )
+
+ React.useEffect(() => {
+ overlayOpenRef.current = true
+
+ const input = inputRef.current
+ if (!input) {
+ return () => {
+ overlayOpenRef.current = false
+ }
+ }
+
+ const handleInput = (event: Event) => {
+ event.stopPropagation()
+ latestValueRef.current = input.value
+ }
+ const stopTextInputPropagation = (event: Event) => {
+ event.stopPropagation()
+ }
+ const handleKeyDown = (event: KeyboardEvent) => {
+ event.stopPropagation()
+
+ if (event.key === "Escape") {
+ event.preventDefault()
+ finishEditing(false)
+ return
+ }
+
+ if (event.key === "Tab") {
+ event.preventDefault()
+ finishEditing(true, [event.shiftKey ? -1 : 1, 0])
+ return
+ }
+
+ if (event.key === "Enter" && !event.shiftKey) {
+ event.preventDefault()
+ finishEditing(true, [0, 1])
+ }
+ }
+
+ input.addEventListener("input", handleInput)
+ input.addEventListener("beforeinput", stopTextInputPropagation, true)
+ input.addEventListener("keydown", handleKeyDown, true)
+
+ if (!readOnly) {
+ input.focus()
+
+ if (validatedSelection !== undefined) {
+ const selectionRange =
+ typeof validatedSelection === "number"
+ ? [validatedSelection, validatedSelection]
+ : validatedSelection
+ input.setSelectionRange(selectionRange[0], selectionRange[1])
+ } else if (isHighlighted) {
+ input.setSelectionRange(0, input.value.length)
+ } else {
+ input.setSelectionRange(input.value.length, input.value.length)
+ }
+ }
+
+ return () => {
+ input.removeEventListener("input", handleInput)
+ input.removeEventListener("beforeinput", stopTextInputPropagation, true)
+ input.removeEventListener("keydown", handleKeyDown, true)
+ overlayOpenRef.current = false
+ }
+ }, [
+ finishEditing,
+ isHighlighted,
+ overlayOpenRef,
+ validatedSelection,
+ readOnly,
+ ])
+
+ React.useEffect(() => {
+ const handlePointerOutside = (event: PointerEvent | MouseEvent) => {
+ const input = inputRef.current
+ const overlayRoot = input?.closest(".gdg-clip-region")
+
+ if (!overlayRoot || overlayRoot.contains(event.target as Node | null)) {
+ return
+ }
+
+ finishEditing(true)
+ }
+
+ document.addEventListener("pointerdown", handlePointerOutside, true)
+ document.addEventListener("contextmenu", handlePointerOutside, true)
+
+ return () => {
+ document.removeEventListener("pointerdown", handlePointerOutside, true)
+ document.removeEventListener("contextmenu", handlePointerOutside, true)
+ }
+ }, [finishEditing])
+
+ return (
+ <textarea
+ ref={inputRef}
+ className="gdg-input"
+ defaultValue={initialValue}
+ disabled={readOnly}
+ dir="auto"
+ style={{ height: "100%", minHeight: 32, resize: "none", width: "100%" }}
+ />
+ )
+}
+
function getFieldIcon(type: SchemaPropertyType) {
if (type === "number" || type === "integer") return InputNumericIcon
if (type === "boolean") return TextCheckIcon
@@ -820,24 +1018,6 @@ function getNestedArrayValue({
})
}
-function isCellInRange(col: number, row: number, range: Rectangle) {
- return (
- col >= range.x &&
- col < range.x + range.width &&
- row >= range.y &&
- row < range.y + range.height
- )
-}
-
-function getRangeEdges(col: number, row: number, range: Rectangle) {
- return {
- bottom: row === range.y + range.height - 1,
- left: col === range.x,
- right: col === range.x + range.width - 1,
- top: row === range.y,
- }
-}
-
function HumanReviewArrayValueGrid({
activeNestedSide = null,
activeSelectionSide = null,
@@ -886,6 +1066,7 @@ function HumanReviewArrayValueGrid({
>([])
const [localGridSelection, setLocalGridSelection] =
React.useState<GridSelection>(emptyGridSelection)
+ const fastTextOverlayOpenRef = React.useRef(false)
const nestedStack = sharedNestedStack ?? localNestedStack
const syncedGridSelection = sharedGridSelection ?? localGridSelection
const gridSelection =
@@ -894,28 +1075,6 @@ function HumanReviewArrayValueGrid({
selectionDepth !== nestedStackBaseDepth)
? emptyGridSelection
: syncedGridSelection
- const sharedSelectionRange = sharedGridSelection?.current?.range
- const sharedSelectionRangeStack = sharedGridSelection?.current?.rangeStack
- const mirroredSelectionRanges = React.useMemo<readonly Rectangle[]>(() => {
- if (
- !activeSelectionSide ||
- activeSelectionSide === viewSide ||
- selectionDepth !== nestedStackBaseDepth ||
- !sharedSelectionRange ||
- !sharedSelectionRangeStack
- ) {
- return []
- }
-
- return [...sharedSelectionRangeStack, sharedSelectionRange]
- }, [
- activeSelectionSide,
- nestedStackBaseDepth,
- selectionDepth,
- sharedSelectionRange,
- sharedSelectionRangeStack,
- viewSide,
- ])
const visibleNestedStack = nestedStack.slice(nestedStackBaseDepth)
const activeNestedView = visibleNestedStack[0] ?? null
const activeNestedValue = activeNestedView
@@ -938,7 +1097,33 @@ function HumanReviewArrayValueGrid({
const mirroredCellBackground = isDark
? "rgba(167, 139, 250, 0.18)"
: "rgba(237, 233, 254, 0.9)"
- const mirroredCellBorder = isDark ? "rgb(196, 181, 253)" : "rgb(124, 58, 237)"
+ const mirroredHighlightRegions = React.useMemo<
+ React.ComponentProps<typeof DataEditor>["highlightRegions"]
+ >(() => {
+ if (
+ !activeSelectionSide ||
+ activeSelectionSide === viewSide ||
+ selectionDepth !== nestedStackBaseDepth
+ ) {
+ return undefined
+ }
+
+ const ranges = getGridSelectionRanges(sharedGridSelection)
+ if (!ranges.length) return undefined
+
+ return ranges.map((range) => ({
+ color: mirroredCellBackground,
+ range,
+ style: "dashed" as const,
+ }))
+ }, [
+ activeSelectionSide,
+ mirroredCellBackground,
+ nestedStackBaseDepth,
+ selectionDepth,
+ sharedGridSelection,
+ viewSide,
+ ])
const setNestedStack = React.useCallback(
(
@@ -962,6 +1147,13 @@ function HumanReviewArrayValueGrid({
const handleGridSelectionChange = React.useCallback(
(selection: GridSelection) => {
if (onGridSelectionChange) {
+ if (
+ sharedGridSelection &&
+ areGridSelectionsEqual(sharedGridSelection, selection)
+ ) {
+ return
+ }
+
onGridSelectionChange(selection, viewSide, nestedStackBaseDepth)
return
}
@@ -970,7 +1162,7 @@ function HumanReviewArrayValueGrid({
areGridSelectionsEqual(current, selection) ? current : selection
)
},
- [nestedStackBaseDepth, onGridSelectionChange, viewSide]
+ [nestedStackBaseDepth, onGridSelectionChange, sharedGridSelection, viewSide]
)
const columns = React.useMemo<GridColumn[]>(() => {
@@ -1009,9 +1201,6 @@ function HumanReviewArrayValueGrid({
const matchedNestedCell =
activeNestedView?.rowIndex === row &&
activeNestedView.columnId === columnId
- const matchesMirroredSelection = mirroredSelectionRanges.some((range) =>
- isCellInRange(col, row, range)
- )
const nestedCellTheme: Partial<Theme> | undefined = matchedNestedCell
? {
bgCell: isMirroredNestedTarget
@@ -1020,13 +1209,6 @@ function HumanReviewArrayValueGrid({
textDark: blueCellText,
}
: undefined
- const selectionCellTheme: Partial<Theme> | undefined =
- matchesMirroredSelection
- ? {
- bgCell: mirroredCellBackground,
- textDark: isDark ? "rgb(233, 213, 255)" : "rgb(91, 33, 182)",
- }
- : undefined
if (isComplexSchema(cellSchema)) {
return {
@@ -1039,7 +1221,6 @@ function HumanReviewArrayValueGrid({
activationBehaviorOverride: "double-click",
themeOverride: {
textDark: blueCellText,
- ...(selectionCellTheme ?? {}),
...(nestedCellTheme ?? {}),
},
}
@@ -1051,7 +1232,6 @@ function HumanReviewArrayValueGrid({
data: typeof cellValue === "boolean" ? cellValue : false,
allowOverlay: false,
readonly: readOnly,
- ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),
}
}
@@ -1061,8 +1241,7 @@ function HumanReviewArrayValueGrid({
data: typeof cellValue === "number" ? cellValue : undefined,
displayData: typeof cellValue === "number" ? String(cellValue) : "",
allowOverlay: true,
- readonly: readOnly,
- ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),
+ readonly: false,
}
}
@@ -1081,8 +1260,7 @@ function HumanReviewArrayValueGrid({
? ""
: String(cellValue),
allowOverlay: true,
- readonly: readOnly,
- ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),
+ readonly: false,
}
},
[
@@ -1090,10 +1268,8 @@ function HumanReviewArrayValueGrid({
blueCellText,
columns,
isMirroredNestedTarget,
- isDark,
itemSchema,
mirroredCellBackground,
- mirroredSelectionRanges,
readOnly,
rows,
selectedCellBackground,
@@ -1121,58 +1297,43 @@ function HumanReviewArrayValueGrid({
},
[columns, itemSchema, onChange, readOnly, rows, value]
)
-
- const drawCell = React.useCallback<DrawCellCallback>(
- (args, drawContent) => {
- drawContent()
-
- const selectionRange = mirroredSelectionRanges.find((range) =>
- isCellInRange(args.col, args.row, range)
- )
-
- if (!selectionRange) {
- return
- }
-
- const { ctx, rect } = args
- const edges = getRangeEdges(args.col, args.row, selectionRange)
-
- ctx.save()
- ctx.strokeStyle = mirroredCellBorder
- ctx.lineWidth = 1.5
- ctx.setLineDash([4, 3])
-
- if (edges.top) {
- ctx.beginPath()
- ctx.moveTo(rect.x + 1.5, rect.y + 1.5)
- ctx.lineTo(rect.x + rect.width - 1.5, rect.y + 1.5)
- ctx.stroke()
- }
-
- if (edges.right) {
- ctx.beginPath()
- ctx.moveTo(rect.x + rect.width - 1.5, rect.y + 1.5)
- ctx.lineTo(rect.x + rect.width - 1.5, rect.y + rect.height - 1.5)
- ctx.stroke()
+ const provideEditor = React.useCallback<
+ NonNullable<React.ComponentProps<typeof DataEditor>["provideEditor"]>
+ >(
+ (cell) => {
+ if (
+ cell.kind !== GridCellKind.Text &&
+ cell.kind !== GridCellKind.Number
+ ) {
+ return undefined
}
- if (edges.bottom) {
- ctx.beginPath()
- ctx.moveTo(rect.x + 1.5, rect.y + rect.height - 1.5)
- ctx.lineTo(rect.x + rect.width - 1.5, rect.y + rect.height - 1.5)
- ctx.stroke()
- }
+ return {
+ editor: (props) => {
+ if (
+ props.value.kind !== GridCellKind.Text &&
+ props.value.kind !== GridCellKind.Number
+ ) {
+ return null
+ }
- if (edges.left) {
- ctx.beginPath()
- ctx.moveTo(rect.x + 1.5, rect.y + 1.5)
- ctx.lineTo(rect.x + 1.5, rect.y + rect.height - 1.5)
- ctx.stroke()
+ return (
+ <HumanReviewTextOverlayEditor
+ {...(props as React.ComponentProps<
+ ProvideEditorComponent<HumanReviewOverlayCell>
+ >)}
+ overlayOpenRef={fastTextOverlayOpenRef}
+ readOnly={readOnly}
+ />
+ )
+ },
}
-
- ctx.restore()
},
- [mirroredCellBorder, mirroredSelectionRanges]
+ [readOnly]
+ )
+ const handleOutsideClick = React.useCallback(
+ () => !fastTextOverlayOpenRef.current,
+ []
)
const openNestedCell = React.useCallback(
@@ -1259,11 +1420,13 @@ function HumanReviewArrayValueGrid({
rows={rows.length}
getCellContent={getCellContent}
cellActivationBehavior="double-click"
- drawCell={drawCell}
gridSelection={gridSelection}
+ highlightRegions={mirroredHighlightRegions}
onCellEdited={updateCellValue}
onCellActivated={openNestedCell}
onGridSelectionChange={handleGridSelectionChange}
+ provideEditor={provideEditor}
+ isOutsideClick={handleOutsideClick}
rowMarkers="number"
smoothScrollX
smoothScrollY
@@ -1452,10 +1615,17 @@ function HumanReviewFieldCardBase({
React.useState<SyncedArraySelection>(EMPTY_SYNCED_ARRAY_SELECTION)
const updateSyncedArrayNestedView = React.useCallback(
(stack: ArrayNestedView[], side: ArrayReviewSide) => {
- setSyncedArrayNestedView({
- activeSide: stack.length ? side : null,
- stack,
- })
+ const activeSide = stack.length ? side : null
+
+ setSyncedArrayNestedView((current) =>
+ current.activeSide === activeSide &&
+ areArrayNestedViewsEqual(current.stack, stack)
+ ? current
+ : {
+ activeSide,
+ stack,
+ }
+ )
},
[]
)
@@ -2072,8 +2242,7 @@ export function HumanReviewExample() {
}`
const humanReviewSourceCode =
- '"use client"\n\nimport * as React from "react"\nimport {\n DataEditor,\n emptyGridSelection,\n GridCellKind,\n type DrawCellCallback,\n type EditableGridCell,\n type GridCell,\n type GridColumn,\n type GridSelection,\n type Item,\n type Rectangle,\n type Theme,\n} from "@glideapps/glide-data-grid"\nimport {\n ArrowLeft01Icon,\n CancelCircleIcon,\n FileDiffIcon,\n InputNumericIcon,\n InputTextIcon,\n SecondBracketIcon,\n SourceCodeSquareIcon,\n TextCheckIcon,\n Undo02Icon,\n} from "@hugeicons/core-free-icons"\nimport { HugeiconsIcon } from "@hugeicons/react"\nimport { MultiFileDiff, Virtualizer } from "@pierre/diffs/react"\n\nimport { cn } from "@/lib/utils"\nimport { PDFViewer, type PDFViewerHandle } from "@/components/ui/pdf-viewer"\nimport { PdfBlockResizableShell } from "@/components/pdf-block-resizable-shell"\nimport { Button } from "@/registry/new-york-v4/ui/button"\nimport { Input } from "@/registry/new-york-v4/ui/input"\nimport { ScrollArea } from "@/registry/new-york-v4/ui/scroll-area"\nimport {\n Tabs,\n TabsContent,\n TabsList,\n TabsTrigger,\n} from "@/registry/new-york-v4/ui/tabs"\nimport {\n Tooltip,\n TooltipContent,\n TooltipProvider,\n TooltipTrigger,\n} from "@/registry/new-york-v4/ui/tooltip"\n\nimport "@glideapps/glide-data-grid/dist/index.css"\n\nexport type JsonPrimitive = string | number | boolean | null\nexport type JsonValue = JsonPrimitive | JsonObject | JsonArray\nexport type JsonObject = { [key: string]: JsonValue }\nexport type JsonArray = JsonValue[]\nexport type SchemaPropertyType =\n | "string"\n | "number"\n | "integer"\n | "boolean"\n | "object"\n | "array"\n\nexport type ReviewFieldSchema = {\n type: SchemaPropertyType\n title?: string\n description?: string\n enum?: Array<string | number>\n properties?: Record<string, ReviewFieldSchema>\n items?: ReviewFieldSchema\n}\n\nexport type HighlightArea = {\n left: number\n top: number\n width: number\n height: number\n}\n\nexport type ReviewField = {\n key: string\n schema: ReviewFieldSchema\n actual: JsonValue\n expected: JsonValue\n location?: {\n page: number\n area: HighlightArea\n }\n}\n\nconst DEFAULT_ZOOM = 0.75\nconst REVIEW_HIGHLIGHT_STYLE =\n "border-blue-500/70 bg-blue-500/12 shadow-[0_4px_16px_rgb(59_130_246_/_10%)]"\nconst DIFF_VIEWER_THEME = {\n "--diffs-light-bg": "oklch(0.985 0.002 247)",\n "--diffs-dark-bg": "oklch(0.18 0.003 247)",\n "--diffs-light": "oklch(0.22 0.01 247)",\n "--diffs-dark": "oklch(0.92 0.006 247)",\n "--diffs-bg-context-override":\n "light-dark(oklch(0.967 0.003 247), oklch(0.235 0.004 247))",\n "--diffs-bg-context-gutter-override":\n "light-dark(oklch(0.948 0.004 247), oklch(0.205 0.004 247))",\n "--diffs-bg-separator-override":\n "light-dark(oklch(0.94 0.004 247), oklch(0.255 0.005 247))",\n "--diffs-bg-buffer-override":\n "light-dark(oklch(0.955 0.004 247), oklch(0.225 0.004 247))",\n "--diffs-light-addition-color": "oklch(0.54 0.13 158)",\n "--diffs-dark-addition-color": "oklch(0.72 0.13 158)",\n "--diffs-light-deletion-color": "oklch(0.55 0.16 28)",\n "--diffs-dark-deletion-color": "oklch(0.72 0.14 28)",\n "--diffs-bg-addition-override":\n "light-dark(oklch(0.957 0.032 158), oklch(0.255 0.052 158))",\n "--diffs-bg-addition-emphasis-override":\n "light-dark(oklch(0.88 0.06 158), oklch(0.36 0.08 158))",\n "--diffs-bg-deletion-override":\n "light-dark(oklch(0.958 0.03 28), oklch(0.255 0.047 28))",\n "--diffs-bg-deletion-emphasis-override":\n "light-dark(oklch(0.9 0.052 28), oklch(0.36 0.075 28))",\n "--diffs-fg-number-override":\n "light-dark(oklch(0.56 0.018 247), oklch(0.66 0.012 247))",\n "--diffs-font-size": "12px",\n "--diffs-line-height": "20px",\n} as React.CSSProperties\n\nfunction readIsDarkTheme() {\n return (\n typeof document !== "undefined" &&\n document.documentElement.classList.contains("dark")\n )\n}\n\n// A single shared MutationObserver backs every consumer. Each grid previously\n// created its own observer (two, via useHumanReviewGridTheme), so opening a\n// nested array view spun up and tore down several observers at once.\nconst darkThemeListeners = new Set<(isDark: boolean) => void>()\nlet darkThemeObserver: MutationObserver | null = null\nlet sharedIsDarkTheme = false\n\nfunction ensureDarkThemeObserver() {\n if (\n darkThemeObserver ||\n typeof document === "undefined" ||\n typeof MutationObserver === "undefined"\n ) {\n return\n }\n\n sharedIsDarkTheme = readIsDarkTheme()\n darkThemeObserver = new MutationObserver(() => {\n const nextIsDark = readIsDarkTheme()\n if (nextIsDark === sharedIsDarkTheme) return\n\n sharedIsDarkTheme = nextIsDark\n darkThemeListeners.forEach((listener) => listener(nextIsDark))\n })\n darkThemeObserver.observe(document.documentElement, {\n attributes: true,\n attributeFilter: ["class"],\n })\n}\n\nfunction useIsDarkTheme() {\n const [isDark, setIsDark] = React.useState(readIsDarkTheme)\n\n React.useEffect(() => {\n ensureDarkThemeObserver()\n setIsDark(sharedIsDarkTheme)\n darkThemeListeners.add(setIsDark)\n\n return () => {\n darkThemeListeners.delete(setIsDark)\n if (darkThemeListeners.size === 0 && darkThemeObserver) {\n darkThemeObserver.disconnect()\n darkThemeObserver = null\n }\n }\n }, [])\n\n return isDark\n}\n\nfunction useHumanReviewGridTheme() {\n const isDark = useIsDarkTheme()\n\n return React.useMemo<Partial<Theme>>(\n () => ({\n accentColor: isDark ? "rgb(96, 165, 250)" : "rgb(37, 99, 235)",\n accentLight: isDark ? "rgba(29, 78, 216, 0.15)" : "rgb(219, 234, 254)",\n accentFg: "rgb(255, 255, 255)",\n textDark: isDark ? "rgb(229, 229, 229)" : "rgb(23, 23, 23)",\n textMedium: isDark ? "rgb(163, 163, 163)" : "rgb(82, 82, 82)",\n textLight: isDark ? "rgb(115, 115, 115)" : "rgb(163, 163, 163)",\n textBubble: isDark ? "rgb(245, 245, 245)" : "rgb(23, 23, 23)",\n textHeader: isDark ? "rgb(245, 245, 245)" : "rgb(23, 23, 23)",\n textGroupHeader: isDark ? "rgb(163, 163, 163)" : "rgb(82, 82, 82)",\n bgCell: isDark ? "rgb(10, 10, 10)" : "rgb(255, 255, 255)",\n bgCellMedium: isDark ? "rgb(23, 23, 23)" : "rgb(250, 250, 250)",\n bgHeader: isDark ? "rgb(23, 23, 23)" : "rgb(250, 250, 250)",\n bgHeaderHasFocus: isDark ? "rgb(38, 38, 38)" : "rgb(245, 245, 245)",\n bgHeaderHovered: isDark ? "rgb(38, 38, 38)" : "rgb(245, 245, 245)",\n borderColor: isDark ? "rgb(38, 38, 38)" : "rgb(229, 229, 229)",\n horizontalBorderColor: isDark ? "rgb(38, 38, 38)" : "rgb(229, 229, 229)",\n cellHorizontalPadding: 8,\n cellVerticalPadding: 3,\n headerIconSize: 18,\n baseFontStyle: "13px",\n headerFontStyle: "600 13px",\n markerFontStyle: "11px",\n fontFamily: \'-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif\',\n editorFontSize: "13px",\n }),\n [isDark]\n )\n}\n\nconst REVIEW_FIELDS: ReviewField[] = [\n {\n key: "vendor_name",\n schema: {\n type: "string",\n title: "Vendor name",\n description: "Supplier shown on the invoice header.",\n },\n actual: "Acme Supplies LLC",\n expected: "Acme Supply LLC",\n location: {\n page: 1,\n area: { left: 31, top: 30, width: 40, height: 5.8 },\n },\n },\n {\n key: "line_items",\n schema: {\n type: "array",\n title: "Line items",\n description: "Invoice rows with nested allocation details.",\n items: {\n type: "object",\n properties: {\n description: {\n type: "string",\n title: "Description",\n },\n quantity: {\n type: "number",\n title: "Qty",\n },\n unit_price: {\n type: "number",\n title: "Unit price",\n },\n tax_detail: {\n type: "object",\n title: "Tax detail",\n properties: {\n code: {\n type: "string",\n title: "Code",\n },\n jurisdiction: {\n type: "string",\n title: "Jurisdiction",\n },\n },\n },\n allocations: {\n type: "array",\n title: "Allocations",\n items: {\n type: "object",\n properties: {\n department: {\n type: "string",\n title: "Department",\n },\n percent: {\n type: "number",\n title: "Percent",\n },\n },\n },\n },\n },\n },\n },\n actual: [\n {\n description: "Transformer implementation support",\n quantity: 2,\n unit_price: 4200,\n tax_detail: { code: "TX-CA", jurisdiction: "CA" },\n allocations: [\n { department: "Research", percent: 70 },\n { department: "Platform", percent: 20 },\n ],\n },\n {\n description: "Attention model review",\n quantity: 1,\n unit_price: 4080,\n tax_detail: { code: "TX-NY", jurisdiction: "NY" },\n allocations: [{ department: "Research", percent: 100 }],\n },\n ],\n expected: [\n {\n description: "Transformer implementation support",\n quantity: 2,\n unit_price: 4200,\n tax_detail: { code: "TX-CA", jurisdiction: "CA" },\n allocations: [\n { department: "Research", percent: 70 },\n { department: "Platform", percent: 30 },\n ],\n },\n {\n description: "Attention model review",\n quantity: 1,\n unit_price: 4080.75,\n tax_detail: { code: "TX-NY", jurisdiction: "NY" },\n allocations: [{ department: "Research", percent: 100 }],\n },\n ],\n location: {\n page: 1,\n area: { left: 13.5, top: 66, width: 73.5, height: 7.5 },\n },\n },\n {\n key: "total_amount",\n schema: {\n type: "number",\n title: "Total amount",\n description: "Final amount due including tax.",\n },\n actual: 12480,\n expected: 12480.75,\n location: {\n page: 1,\n area: { left: 13.5, top: 66, width: 73.5, height: 7.5 },\n },\n },\n {\n key: "payment_terms",\n schema: {\n type: "string",\n title: "Payment terms",\n enum: ["Due on receipt", "Net 15", "Net 30"],\n },\n actual: "Net 15",\n expected: "Net 30",\n location: {\n page: 1,\n area: { left: 13.5, top: 55.5, width: 73.5, height: 7.5 },\n },\n },\n {\n key: "requires_review",\n schema: {\n type: "boolean",\n title: "Requires review",\n description: "Whether a human should verify this document before export.",\n },\n actual: false,\n expected: true,\n location: {\n page: 2,\n area: { left: 9.5, top: 12, width: 81, height: 11.5 },\n },\n },\n {\n key: "remittance",\n schema: {\n type: "object",\n title: "Remittance details",\n description: "Payment destination used for invoice reconciliation.",\n properties: {\n account_holder: {\n type: "string",\n title: "Account holder",\n },\n routing_number: {\n type: "string",\n title: "Routing number",\n },\n verified: {\n type: "boolean",\n title: "Verified",\n },\n },\n },\n actual: {\n account_holder: "Acme Supplies LLC",\n routing_number: "021000021",\n verified: false,\n },\n expected: {\n account_holder: "Acme Supply LLC",\n routing_number: "021000021",\n verified: true,\n },\n },\n]\n\nfunction valuesFromFields(\n fields: ReviewField[],\n valueKey: "actual" | "expected"\n) {\n return fields.reduce<JsonObject>((values, field) => {\n values[field.key] = field[valueKey]\n return values\n }, {})\n}\n\nfunction formatJson(value: unknown) {\n return JSON.stringify(value, null, 2)\n}\n\nfunction isJsonObject(value: JsonValue): value is JsonObject {\n return typeof value === "object" && value !== null && !Array.isArray(value)\n}\n\nfunction isJsonArray(value: JsonValue): value is JsonArray {\n return Array.isArray(value)\n}\n\nfunction getObjectValue(value: JsonValue, key: string): JsonValue {\n if (!isJsonObject(value)) return null\n return value[key] ?? null\n}\n\nfunction setObjectValue(\n value: JsonValue,\n key: string,\n childValue: JsonValue\n): JsonObject {\n return {\n ...(isJsonObject(value) ? value : {}),\n [key]: childValue,\n }\n}\n\nfunction getArrayValue(value: JsonValue): JsonArray {\n return isJsonArray(value) ? value : []\n}\n\nfunction setArrayItemValue(\n value: JsonValue,\n index: number,\n childValue: JsonValue\n): JsonArray {\n const nextValue = getArrayValue(value).slice()\n nextValue[index] = childValue\n return nextValue\n}\n\nfunction getPrimitiveValue(value: JsonValue): JsonPrimitive {\n return isJsonObject(value) || isJsonArray(value) ? null : value\n}\n\nfunction jsonValuesEqual(left: JsonValue, right: JsonValue) {\n return formatJson(left) === formatJson(right)\n}\n\nfunction countReviewFields(fields: ReviewField[]): number {\n return fields.reduce((count, field) => {\n if (field.schema.type !== "object") return count + 1\n\n const properties = field.schema.properties ?? {}\n const childFields = Object.entries(properties).map(\n ([key, schema]): ReviewField => ({\n key: `${field.key}.${key}`,\n schema,\n actual: getObjectValue(field.actual, key),\n expected: getObjectValue(field.expected, key),\n })\n )\n\n return count + Math.max(countReviewFields(childFields), 1)\n }, 0)\n}\n\nfunction findReviewField(\n fields: ReviewField[],\n fieldKey: string | undefined\n): ReviewField | undefined {\n if (!fieldKey) return undefined\n\n for (const field of fields) {\n if (field.key === fieldKey) return field\n\n if (field.schema.type === "object") {\n const childFields = Object.entries(field.schema.properties ?? {}).map(\n ([key, schema]): ReviewField => ({\n key: `${field.key}.${key}`,\n schema,\n actual: getObjectValue(field.actual, key),\n expected: getObjectValue(field.expected, key),\n })\n )\n const childField = findReviewField(childFields, fieldKey)\n if (childField) return childField\n }\n }\n\n return undefined\n}\n\nfunction formatValue(value: JsonValue) {\n if (value === null) return "NULL"\n if (isJsonObject(value) || isJsonArray(value)) return formatJson(value)\n if (typeof value === "boolean") return value ? "true" : "false"\n return String(value)\n}\n\nfunction areGridRangesEqual(\n left: Readonly<Rectangle> | undefined,\n right: Readonly<Rectangle> | undefined\n) {\n return (\n left === right ||\n (left !== undefined &&\n right !== undefined &&\n left.x === right.x &&\n left.y === right.y &&\n left.width === right.width &&\n left.height === right.height)\n )\n}\n\nfunction areGridRangeStacksEqual(\n left: readonly Readonly<Rectangle>[] | undefined,\n right: readonly Readonly<Rectangle>[] | undefined\n) {\n if (left === right) return true\n if (!left || !right || left.length !== right.length) return false\n\n return left.every((range, index) => areGridRangesEqual(range, right[index]))\n}\n\nfunction areGridSelectionsEqual(left: GridSelection, right: GridSelection) {\n const leftCurrent = left.current\n const rightCurrent = right.current\n\n return (\n leftCurrent?.cell[0] === rightCurrent?.cell[0] &&\n leftCurrent?.cell[1] === rightCurrent?.cell[1] &&\n areGridRangesEqual(leftCurrent?.range, rightCurrent?.range) &&\n areGridRangeStacksEqual(\n leftCurrent?.rangeStack,\n rightCurrent?.rangeStack\n ) &&\n left.columns.equals(right.columns) &&\n left.rows.equals(right.rows)\n )\n}\n\nfunction getFieldIcon(type: SchemaPropertyType) {\n if (type === "number" || type === "integer") return InputNumericIcon\n if (type === "boolean") return TextCheckIcon\n if (type === "array") return SecondBracketIcon\n if (type === "object") return SourceCodeSquareIcon\n return InputTextIcon\n}\n\nfunction HumanReviewValueInput({\n readOnly = false,\n schema,\n value,\n onChange,\n}: {\n readOnly?: boolean\n schema: ReviewFieldSchema\n value: JsonPrimitive\n onChange: (value: JsonPrimitive) => void\n}) {\n if (schema.enum?.length) {\n return (\n <span className="relative inline-flex w-full rounded-lg border border-input bg-background text-sm text-foreground shadow-xs/5 dark:bg-input/32">\n <select\n disabled={readOnly}\n value={value === null ? "" : String(value)}\n onChange={(event) => onChange(event.target.value)}\n className="h-8.5 w-full appearance-none rounded-[inherit] bg-transparent px-3 text-sm outline-none sm:h-7.5"\n >\n {schema.enum.map((option) => (\n <option key={String(option)} value={String(option)}>\n {String(option)}\n </option>\n ))}\n </select>\n </span>\n )\n }\n\n if (schema.type === "number" || schema.type === "integer") {\n return (\n <Input\n nativeInput\n readOnly={readOnly}\n type="number"\n value={value === null ? "" : String(value)}\n onChange={(event) => {\n const nextValue = event.currentTarget.value\n onChange(nextValue === "" ? null : Number(nextValue))\n }}\n />\n )\n }\n\n if (schema.type === "boolean") {\n return (\n <div className="grid grid-cols-2 gap-1 rounded-lg bg-muted p-0.5">\n {[true, false].map((option) => (\n <Button\n key={String(option)}\n type="button"\n size="sm"\n variant={value === option ? "outline" : "ghost"}\n className={cn(\n "h-7 shadow-none",\n value === option && "bg-background dark:bg-input"\n )}\n disabled={readOnly}\n onClick={() => onChange(option)}\n >\n {option ? "True" : "False"}\n </Button>\n ))}\n </div>\n )\n }\n\n return (\n <Input\n nativeInput\n readOnly={readOnly}\n value={value === null ? "" : String(value)}\n onChange={(event) => onChange(event.currentTarget.value)}\n />\n )\n}\n\nfunction getArrayItemSchema(schema: ReviewFieldSchema): ReviewFieldSchema {\n return schema.items ?? { type: "string" }\n}\n\nfunction isComplexSchema(schema: ReviewFieldSchema) {\n return schema.type === "object" || schema.type === "array"\n}\n\nfunction summarizeComplexValue(value: JsonValue) {\n if (isJsonArray(value)) return `${value.length} items`\n if (isJsonObject(value)) return `${Object.keys(value).length} fields`\n return formatValue(value)\n}\n\nfunction getCellValueForArrayColumn(\n rowValue: JsonValue,\n itemSchema: ReviewFieldSchema,\n columnId: string\n) {\n if (itemSchema.type === "object") {\n return getObjectValue(rowValue, columnId)\n }\n\n return rowValue\n}\n\nfunction getCellSchemaForArrayColumn(\n itemSchema: ReviewFieldSchema,\n columnId: string\n) {\n if (itemSchema.type === "object") {\n return itemSchema.properties?.[columnId] ?? { type: "string" }\n }\n\n return itemSchema\n}\n\nfunction applyPrimitiveEdit(\n schema: ReviewFieldSchema,\n value: EditableGridCell\n): JsonValue | undefined {\n if (schema.type === "boolean" && value.kind === GridCellKind.Boolean) {\n return value.data\n }\n\n if (\n (schema.type === "number" || schema.type === "integer") &&\n value.kind === GridCellKind.Number\n ) {\n return value.data ?? null\n }\n\n if (value.kind === GridCellKind.Text) {\n if (schema.type === "number" || schema.type === "integer") {\n return value.data.trim() === "" ? null : Number(value.data)\n }\n\n return value.data\n }\n\n return undefined\n}\n\ntype ArrayNestedView = {\n rowIndex: number\n columnId: string\n title: string\n schema: ReviewFieldSchema\n value: JsonValue\n}\n\ntype ArrayReviewSide = "actual" | "expected"\n\ntype SyncedArrayNestedView = {\n activeSide: ArrayReviewSide | null\n stack: ArrayNestedView[]\n}\n\ntype SyncedArraySelection = {\n activeSide: ArrayReviewSide | null\n depth: number\n gridSelection: GridSelection\n}\n\nexport type HumanReviewTheme = "light" | "dark"\n\nconst EMPTY_SYNCED_ARRAY_NESTED_VIEW: SyncedArrayNestedView = {\n activeSide: null,\n stack: [],\n}\n\nconst EMPTY_SYNCED_ARRAY_SELECTION: SyncedArraySelection = {\n activeSide: null,\n depth: 0,\n gridSelection: emptyGridSelection,\n}\n\nfunction setNestedArrayValue({\n value,\n schema,\n nestedStack,\n nextNestedValue,\n}: {\n value: JsonValue\n schema: ReviewFieldSchema\n nestedStack: ArrayNestedView[]\n nextNestedValue: JsonValue\n}): JsonValue {\n const [currentView, ...remainingViews] = nestedStack\n\n if (!currentView) return nextNestedValue\n\n const itemSchema = getArrayItemSchema(schema)\n const rowValue = getArrayValue(value)[currentView.rowIndex] ?? null\n const cellSchema = getCellSchemaForArrayColumn(\n itemSchema,\n currentView.columnId\n )\n const currentCellValue = getCellValueForArrayColumn(\n rowValue,\n itemSchema,\n currentView.columnId\n )\n const nextCellValue: JsonValue = remainingViews.length\n ? setNestedArrayValue({\n value: currentCellValue,\n schema: cellSchema,\n nestedStack: remainingViews,\n nextNestedValue,\n })\n : nextNestedValue\n const nextRowValue: JsonValue =\n itemSchema.type === "object"\n ? setObjectValue(rowValue, currentView.columnId, nextCellValue)\n : nextCellValue\n\n return setArrayItemValue(value, currentView.rowIndex, nextRowValue)\n}\n\nfunction getNestedArrayValue({\n value,\n schema,\n nestedStack,\n}: {\n value: JsonValue\n schema: ReviewFieldSchema\n nestedStack: ArrayNestedView[]\n}): JsonValue {\n const [currentView, ...remainingViews] = nestedStack\n\n if (!currentView) return value\n\n const itemSchema = getArrayItemSchema(schema)\n const rowValue = getArrayValue(value)[currentView.rowIndex] ?? null\n const cellSchema = getCellSchemaForArrayColumn(\n itemSchema,\n currentView.columnId\n )\n const cellValue = getCellValueForArrayColumn(\n rowValue,\n itemSchema,\n currentView.columnId\n )\n\n if (!remainingViews.length) return cellValue\n\n return getNestedArrayValue({\n value: cellValue,\n schema: cellSchema,\n nestedStack: remainingViews,\n })\n}\n\nfunction isCellInRange(col: number, row: number, range: Rectangle) {\n return (\n col >= range.x &&\n col < range.x + range.width &&\n row >= range.y &&\n row < range.y + range.height\n )\n}\n\nfunction getRangeEdges(col: number, row: number, range: Rectangle) {\n return {\n bottom: row === range.y + range.height - 1,\n left: col === range.x,\n right: col === range.x + range.width - 1,\n top: row === range.y,\n }\n}\n\nfunction HumanReviewArrayValueGrid({\n activeNestedSide = null,\n activeSelectionSide = null,\n label,\n nestedStackBaseDepth = 0,\n readOnly = false,\n schema,\n selectionDepth = 0,\n sharedGridSelection,\n sharedNestedStack,\n value,\n viewSide = "expected",\n onChange,\n onGridSelectionChange,\n onNestedStackChange,\n}: {\n activeNestedSide?: ArrayReviewSide | null\n activeSelectionSide?: ArrayReviewSide | null\n label: string\n nestedStackBaseDepth?: number\n readOnly?: boolean\n schema: ReviewFieldSchema\n selectionDepth?: number\n sharedGridSelection?: GridSelection\n sharedNestedStack?: ArrayNestedView[]\n value: JsonValue\n viewSide?: ArrayReviewSide\n onChange?: (value: JsonValue) => void\n onGridSelectionChange?: (\n selection: GridSelection,\n side: ArrayReviewSide,\n depth: number\n ) => void\n onNestedStackChange?: (\n stack: ArrayNestedView[],\n side: ArrayReviewSide\n ) => void\n}) {\n const rows = getArrayValue(value)\n const itemSchema = getArrayItemSchema(schema)\n const gridTheme = useHumanReviewGridTheme()\n const isDark = useIsDarkTheme()\n const [localNestedStack, setLocalNestedStack] = React.useState<\n ArrayNestedView[]\n >([])\n const [localGridSelection, setLocalGridSelection] =\n React.useState<GridSelection>(emptyGridSelection)\n const nestedStack = sharedNestedStack ?? localNestedStack\n const syncedGridSelection = sharedGridSelection ?? localGridSelection\n const gridSelection =\n sharedGridSelection &&\n (activeSelectionSide !== viewSide ||\n selectionDepth !== nestedStackBaseDepth)\n ? emptyGridSelection\n : syncedGridSelection\n const sharedSelectionRange = sharedGridSelection?.current?.range\n const sharedSelectionRangeStack = sharedGridSelection?.current?.rangeStack\n const mirroredSelectionRanges = React.useMemo<readonly Rectangle[]>(() => {\n if (\n !activeSelectionSide ||\n activeSelectionSide === viewSide ||\n selectionDepth !== nestedStackBaseDepth ||\n !sharedSelectionRange ||\n !sharedSelectionRangeStack\n ) {\n return []\n }\n\n return [...sharedSelectionRangeStack, sharedSelectionRange]\n }, [\n activeSelectionSide,\n nestedStackBaseDepth,\n selectionDepth,\n sharedSelectionRange,\n sharedSelectionRangeStack,\n viewSide,\n ])\n const visibleNestedStack = nestedStack.slice(nestedStackBaseDepth)\n const activeNestedView = visibleNestedStack[0] ?? null\n const activeNestedValue = activeNestedView\n ? getNestedArrayValue({\n value,\n schema,\n nestedStack: [activeNestedView],\n })\n : null\n const isActiveNestedSource =\n Boolean(activeNestedView) && activeNestedSide === viewSide\n const isMirroredNestedTarget =\n Boolean(activeNestedView) &&\n activeNestedSide !== null &&\n activeNestedSide !== viewSide\n const blueCellText = isDark ? "rgb(147, 197, 253)" : "rgb(37, 99, 235)"\n const selectedCellBackground = isDark\n ? "rgba(37, 99, 235, 0.2)"\n : "rgba(219, 234, 254, 0.85)"\n const mirroredCellBackground = isDark\n ? "rgba(167, 139, 250, 0.18)"\n : "rgba(237, 233, 254, 0.9)"\n const mirroredCellBorder = isDark ? "rgb(196, 181, 253)" : "rgb(124, 58, 237)"\n\n const setNestedStack = React.useCallback(\n (\n updater:\n | ArrayNestedView[]\n | ((current: ArrayNestedView[]) => ArrayNestedView[])\n ) => {\n if (onNestedStackChange) {\n const nextStack =\n typeof updater === "function" ? updater(nestedStack) : updater\n\n onNestedStackChange(nextStack, viewSide)\n return\n }\n\n setLocalNestedStack(updater)\n },\n [nestedStack, onNestedStackChange, viewSide]\n )\n\n const handleGridSelectionChange = React.useCallback(\n (selection: GridSelection) => {\n if (onGridSelectionChange) {\n onGridSelectionChange(selection, viewSide, nestedStackBaseDepth)\n return\n }\n\n setLocalGridSelection((current) =>\n areGridSelectionsEqual(current, selection) ? current : selection\n )\n },\n [nestedStackBaseDepth, onGridSelectionChange, viewSide]\n )\n\n const columns = React.useMemo<GridColumn[]>(() => {\n if (itemSchema.type === "object") {\n const propertyEntries = Object.entries(itemSchema.properties ?? {})\n\n if (propertyEntries.length) {\n return propertyEntries.map(([key, propertySchema]) => ({\n id: key,\n title: propertySchema.title ?? key,\n width: isComplexSchema(propertySchema) ? 148 : 132,\n }))\n }\n }\n\n return [\n {\n id: "value",\n title: itemSchema.title ?? "Value",\n width: isComplexSchema(itemSchema) ? 148 : 180,\n },\n ]\n }, [itemSchema])\n\n const getCellContent = React.useCallback(\n ([col, row]: Item): GridCell => {\n const column = columns[col]\n const columnId = String(column?.id ?? "value")\n const rowValue = rows[row] ?? null\n const cellSchema = getCellSchemaForArrayColumn(itemSchema, columnId)\n const cellValue = getCellValueForArrayColumn(\n rowValue,\n itemSchema,\n columnId\n )\n const matchedNestedCell =\n activeNestedView?.rowIndex === row &&\n activeNestedView.columnId === columnId\n const matchesMirroredSelection = mirroredSelectionRanges.some((range) =>\n isCellInRange(col, row, range)\n )\n const nestedCellTheme: Partial<Theme> | undefined = matchedNestedCell\n ? {\n bgCell: isMirroredNestedTarget\n ? mirroredCellBackground\n : selectedCellBackground,\n textDark: blueCellText,\n }\n : undefined\n const selectionCellTheme: Partial<Theme> | undefined =\n matchesMirroredSelection\n ? {\n bgCell: mirroredCellBackground,\n textDark: isDark ? "rgb(233, 213, 255)" : "rgb(91, 33, 182)",\n }\n : undefined\n\n if (isComplexSchema(cellSchema)) {\n return {\n kind: GridCellKind.Text,\n data: summarizeComplexValue(cellValue),\n displayData: summarizeComplexValue(cellValue),\n allowOverlay: false,\n readonly: true,\n cursor: "pointer",\n activationBehaviorOverride: "double-click",\n themeOverride: {\n textDark: blueCellText,\n ...(selectionCellTheme ?? {}),\n ...(nestedCellTheme ?? {}),\n },\n }\n }\n\n if (cellSchema.type === "boolean") {\n return {\n kind: GridCellKind.Boolean,\n data: typeof cellValue === "boolean" ? cellValue : false,\n allowOverlay: false,\n readonly: readOnly,\n ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),\n }\n }\n\n if (cellSchema.type === "number" || cellSchema.type === "integer") {\n return {\n kind: GridCellKind.Number,\n data: typeof cellValue === "number" ? cellValue : undefined,\n displayData: typeof cellValue === "number" ? String(cellValue) : "",\n allowOverlay: true,\n readonly: readOnly,\n ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),\n }\n }\n\n return {\n kind: GridCellKind.Text,\n data:\n cellValue === null ||\n isJsonObject(cellValue) ||\n isJsonArray(cellValue)\n ? ""\n : String(cellValue),\n displayData:\n cellValue === null ||\n isJsonObject(cellValue) ||\n isJsonArray(cellValue)\n ? ""\n : String(cellValue),\n allowOverlay: true,\n readonly: readOnly,\n ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),\n }\n },\n [\n activeNestedView,\n blueCellText,\n columns,\n isMirroredNestedTarget,\n isDark,\n itemSchema,\n mirroredCellBackground,\n mirroredSelectionRanges,\n readOnly,\n rows,\n selectedCellBackground,\n ]\n )\n\n const updateCellValue = React.useCallback(\n ([col, row]: Item, nextCell: EditableGridCell) => {\n if (readOnly || !onChange) return\n\n const column = columns[col]\n const columnId = String(column?.id ?? "value")\n const rowValue = rows[row] ?? null\n const cellSchema = getCellSchemaForArrayColumn(itemSchema, columnId)\n const nextValue = applyPrimitiveEdit(cellSchema, nextCell)\n\n if (nextValue === undefined) return\n\n const nextRowValue =\n itemSchema.type === "object"\n ? setObjectValue(rowValue, columnId, nextValue)\n : nextValue\n\n onChange(setArrayItemValue(value, row, nextRowValue))\n },\n [columns, itemSchema, onChange, readOnly, rows, value]\n )\n\n const drawCell = React.useCallback<DrawCellCallback>(\n (args, drawContent) => {\n drawContent()\n\n const selectionRange = mirroredSelectionRanges.find((range) =>\n isCellInRange(args.col, args.row, range)\n )\n\n if (!selectionRange) {\n return\n }\n\n const { ctx, rect } = args\n const edges = getRangeEdges(args.col, args.row, selectionRange)\n\n ctx.save()\n ctx.strokeStyle = mirroredCellBorder\n ctx.lineWidth = 1.5\n ctx.setLineDash([4, 3])\n\n if (edges.top) {\n ctx.beginPath()\n ctx.moveTo(rect.x + 1.5, rect.y + 1.5)\n ctx.lineTo(rect.x + rect.width - 1.5, rect.y + 1.5)\n ctx.stroke()\n }\n\n if (edges.right) {\n ctx.beginPath()\n ctx.moveTo(rect.x + rect.width - 1.5, rect.y + 1.5)\n ctx.lineTo(rect.x + rect.width - 1.5, rect.y + rect.height - 1.5)\n ctx.stroke()\n }\n\n if (edges.bottom) {\n ctx.beginPath()\n ctx.moveTo(rect.x + 1.5, rect.y + rect.height - 1.5)\n ctx.lineTo(rect.x + rect.width - 1.5, rect.y + rect.height - 1.5)\n ctx.stroke()\n }\n\n if (edges.left) {\n ctx.beginPath()\n ctx.moveTo(rect.x + 1.5, rect.y + 1.5)\n ctx.lineTo(rect.x + 1.5, rect.y + rect.height - 1.5)\n ctx.stroke()\n }\n\n ctx.restore()\n },\n [mirroredCellBorder, mirroredSelectionRanges]\n )\n\n const openNestedCell = React.useCallback(\n ([col, row]: Item) => {\n const column = columns[col]\n const columnId = String(column?.id ?? "value")\n const rowValue = rows[row] ?? null\n const cellSchema = getCellSchemaForArrayColumn(itemSchema, columnId)\n\n if (!isComplexSchema(cellSchema)) return\n\n setNestedStack((current) => {\n const nextView = {\n rowIndex: row,\n columnId,\n title: `${column?.title ?? columnId} / row ${row + 1}`,\n schema: cellSchema,\n value: getCellValueForArrayColumn(rowValue, itemSchema, columnId),\n }\n const currentView = current[nestedStackBaseDepth]\n\n if (\n currentView?.rowIndex === nextView.rowIndex &&\n currentView.columnId === nextView.columnId\n ) {\n return current\n }\n\n return [...current.slice(0, nestedStackBaseDepth), nextView]\n })\n },\n [columns, itemSchema, nestedStackBaseDepth, rows, setNestedStack]\n )\n\n const updateNestedValue = React.useCallback(\n (nextNestedValue: JsonValue) => {\n if (!activeNestedView || readOnly || !onChange) return\n\n onChange(\n setNestedArrayValue({\n value,\n schema,\n nestedStack: visibleNestedStack.slice(0, 1),\n nextNestedValue,\n })\n )\n setNestedStack((current) =>\n current.map((view, index) =>\n index === nestedStackBaseDepth\n ? { ...view, value: nextNestedValue }\n : view\n )\n )\n },\n [\n activeNestedView,\n nestedStackBaseDepth,\n onChange,\n readOnly,\n schema,\n setNestedStack,\n value,\n visibleNestedStack,\n ]\n )\n\n return (\n <div\n className={cn(\n "relative overflow-hidden rounded-md border bg-background transition-[border-color,background-color,box-shadow] focus-within:border-blue-500/50 focus-within:shadow-[0_0_0_1px_rgb(59_130_246_/_8%)] hover:border-blue-500/50",\n isActiveNestedSource &&\n "border-blue-500/60 bg-blue-500/5 shadow-[0_0_0_1px_rgb(59_130_246_/_10%)]"\n )}\n >\n <div>\n <div className="flex h-8 items-center justify-between gap-2 border-b px-2 text-[11px] font-medium text-muted-foreground">\n <span>{label}</span>\n <span>{rows.length} rows</span>\n </div>\n <div className="h-[220px]">\n <DataEditor\n columns={columns}\n rows={rows.length}\n getCellContent={getCellContent}\n cellActivationBehavior="double-click"\n drawCell={drawCell}\n gridSelection={gridSelection}\n onCellEdited={updateCellValue}\n onCellActivated={openNestedCell}\n onGridSelectionChange={handleGridSelectionChange}\n rowMarkers="number"\n smoothScrollX\n smoothScrollY\n theme={gridTheme}\n width="100%"\n height="100%"\n rowHeight={32}\n headerHeight={34}\n />\n </div>\n </div>\n {activeNestedView ? (\n <div className="absolute inset-0 z-10 flex flex-col bg-background">\n <div className="flex h-8 items-center gap-2 border-b px-2">\n <Button\n type="button"\n variant="ghost"\n size="icon-sm"\n className="size-6 text-muted-foreground"\n onClick={() =>\n setNestedStack((current) =>\n current.slice(\n 0,\n Math.max(nestedStackBaseDepth, current.length - 1)\n )\n )\n }\n aria-label="Back to parent array"\n >\n <HugeiconsIcon icon={ArrowLeft01Icon} className="size-3.5" />\n </Button>\n <div className="min-w-0 flex-1 truncate text-xs font-medium text-foreground">\n {activeNestedView.title}\n </div>\n </div>\n <div className="min-h-0 flex-1 overflow-auto p-2">\n {activeNestedView.schema.type === "array" ? (\n <HumanReviewArrayValueGrid\n label={\n activeNestedView.schema.title ?? activeNestedView.columnId\n }\n nestedStackBaseDepth={nestedStackBaseDepth + 1}\n readOnly={readOnly}\n schema={activeNestedView.schema}\n sharedGridSelection={sharedGridSelection}\n sharedNestedStack={sharedNestedStack}\n value={activeNestedValue}\n viewSide={viewSide}\n activeNestedSide={activeNestedSide}\n activeSelectionSide={activeSelectionSide}\n selectionDepth={selectionDepth}\n onChange={updateNestedValue}\n onGridSelectionChange={onGridSelectionChange}\n onNestedStackChange={onNestedStackChange}\n />\n ) : (\n <HumanReviewObjectValueEditor\n schema={activeNestedView.schema}\n value={activeNestedValue}\n originalValue={activeNestedValue}\n readOnly={readOnly}\n onChange={updateNestedValue}\n />\n )}\n </div>\n </div>\n ) : null}\n </div>\n )\n}\n\nfunction HumanReviewObjectValueEditor({\n schema,\n value,\n originalValue,\n readOnly = false,\n onChange,\n}: {\n schema: ReviewFieldSchema\n value: JsonValue\n originalValue: JsonValue\n readOnly?: boolean\n onChange: (value: JsonValue) => void\n}) {\n const propertyEntries = Object.entries(schema.properties ?? {})\n\n if (!propertyEntries.length) {\n return (\n <div className="rounded-md bg-background px-2 py-1.5 text-sm text-muted-foreground">\n No properties\n </div>\n )\n }\n\n return (\n <div className="space-y-2">\n {propertyEntries.map(([propertyKey, propertySchema]) => (\n <HumanReviewFieldCard\n key={propertyKey}\n field={{\n key: propertyKey,\n schema: propertySchema,\n actual: getObjectValue(originalValue, propertyKey),\n expected: getObjectValue(originalValue, propertyKey),\n }}\n value={getObjectValue(value, propertyKey)}\n originalValue={getObjectValue(originalValue, propertyKey)}\n readOnly={readOnly}\n onChange={(childValue) =>\n !readOnly &&\n onChange(setObjectValue(value, propertyKey, childValue))\n }\n onUndo={() =>\n !readOnly &&\n onChange(\n setObjectValue(\n value,\n propertyKey,\n getObjectValue(originalValue, propertyKey)\n )\n )\n }\n onSetNull={() =>\n !readOnly && onChange(setObjectValue(value, propertyKey, null))\n }\n />\n ))}\n </div>\n )\n}\n\ntype HumanReviewFieldCardProps = {\n field: ReviewField\n value: JsonValue\n originalValue: JsonValue\n active?: boolean\n activeFieldKey?: string\n readOnly?: boolean\n onChange: (value: JsonValue) => void\n onFieldFocus?: (field: ReviewField) => void\n onUndo: () => void\n onSetNull: () => void\n}\n\nfunction areHumanReviewFieldCardPropsEqual(\n previous: HumanReviewFieldCardProps,\n next: HumanReviewFieldCardProps\n) {\n return (\n previous.field === next.field &&\n Object.is(previous.value, next.value) &&\n Object.is(previous.originalValue, next.originalValue) &&\n previous.active === next.active &&\n previous.activeFieldKey === next.activeFieldKey &&\n previous.readOnly === next.readOnly &&\n previous.onFieldFocus === next.onFieldFocus\n )\n}\n\nconst HumanReviewFieldCard = React.memo(\n HumanReviewFieldCardBase,\n areHumanReviewFieldCardPropsEqual\n)\n\nfunction HumanReviewFieldCardBase({\n field,\n value,\n originalValue,\n active,\n activeFieldKey,\n readOnly = false,\n onChange,\n onFieldFocus,\n onUndo,\n onSetNull,\n}: HumanReviewFieldCardProps) {\n const modified = !jsonValuesEqual(value, originalValue)\n const Icon = getFieldIcon(field.schema.type)\n const propertyEntries = Object.entries(field.schema.properties ?? {})\n const [syncedArrayNestedView, setSyncedArrayNestedView] =\n React.useState<SyncedArrayNestedView>(EMPTY_SYNCED_ARRAY_NESTED_VIEW)\n const [syncedArraySelection, setSyncedArraySelection] =\n React.useState<SyncedArraySelection>(EMPTY_SYNCED_ARRAY_SELECTION)\n const updateSyncedArrayNestedView = React.useCallback(\n (stack: ArrayNestedView[], side: ArrayReviewSide) => {\n setSyncedArrayNestedView({\n activeSide: stack.length ? side : null,\n stack,\n })\n },\n []\n )\n const updateSyncedArraySelection = React.useCallback(\n (gridSelection: GridSelection, side: ArrayReviewSide, depth: number) => {\n const activeSide = gridSelection.current ? side : null\n\n setSyncedArraySelection((current) =>\n current.activeSide === activeSide &&\n current.depth === depth &&\n areGridSelectionsEqual(current.gridSelection, gridSelection)\n ? current\n : {\n activeSide,\n depth,\n gridSelection,\n }\n )\n },\n []\n )\n\n return (\n <div\n tabIndex={0}\n onFocusCapture={() => onFieldFocus?.(field)}\n onMouseEnter={() => onFieldFocus?.(field)}\n className={cn(\n "rounded-lg border bg-background p-3 transition-[border-color,background-color,box-shadow] focus-within:border-blue-500/50 focus-within:bg-blue-500/5 hover:border-blue-500/50 hover:bg-blue-500/5 focus-visible:ring-2 focus-visible:ring-blue-500/20 focus-visible:outline-none",\n active &&\n "border-blue-500/60 bg-blue-500/5 shadow-[0_0_0_1px_rgb(59_130_246_/_8%)]"\n )}\n >\n <div className="mb-3 flex min-h-8 items-start justify-between gap-3">\n <div className="min-w-0">\n <div className="flex min-w-0 items-center gap-2">\n <div className="min-w-0">\n <div className="truncate text-sm font-medium">\n {field.schema.title ?? field.key}\n </div>\n </div>\n <span\n className={cn(\n "size-2 shrink-0 rounded-full bg-amber-400 transition-opacity",\n !modified && "opacity-0"\n )}\n />\n </div>\n <div className="truncate text-xs text-muted-foreground">\n {field.key}\n </div>\n </div>\n <div className="flex shrink-0 items-center gap-1">\n {!readOnly && modified ? (\n <Tooltip>\n <TooltipTrigger asChild>\n <Button\n type="button"\n variant="ghost"\n size="icon-sm"\n className="text-muted-foreground"\n onClick={onUndo}\n aria-label={`Undo ${field.key}`}\n >\n <HugeiconsIcon icon={Undo02Icon} className="size-4" />\n </Button>\n </TooltipTrigger>\n <TooltipContent>Revert changes</TooltipContent>\n </Tooltip>\n ) : null}\n {!readOnly ? (\n <Tooltip>\n <TooltipTrigger asChild>\n <Button\n type="button"\n variant="ghost"\n size="icon-sm"\n className="text-muted-foreground"\n onClick={onSetNull}\n aria-label={`Set ${field.key} to null`}\n >\n <HugeiconsIcon icon={CancelCircleIcon} className="size-4" />\n </Button>\n </TooltipTrigger>\n <TooltipContent>Set to NULL</TooltipContent>\n </Tooltip>\n ) : null}\n <div className="flex h-6 items-center gap-1 rounded-md border bg-muted/50 px-1.5 text-xs text-muted-foreground">\n <HugeiconsIcon icon={Icon} className="size-3.5" />\n {field.schema.type}\n </div>\n </div>\n </div>\n {field.schema.type === "object" ? (\n <div className="rounded-md border bg-muted/25 p-2">\n <div className="mb-2 flex items-center justify-between gap-3 text-[11px] font-medium text-muted-foreground">\n <span>Properties</span>\n <span>{propertyEntries.length} fields</span>\n </div>\n <div className="space-y-2">\n {propertyEntries.length ? (\n propertyEntries.map(([propertyKey, schema]) => {\n const childField: ReviewField = {\n key: `${field.key}.${propertyKey}`,\n schema,\n actual: getObjectValue(field.actual, propertyKey),\n expected: getObjectValue(originalValue, propertyKey),\n }\n\n return (\n <HumanReviewFieldCard\n key={childField.key}\n field={childField}\n value={getObjectValue(value, propertyKey)}\n originalValue={childField.expected}\n active={childField.key === activeFieldKey}\n activeFieldKey={activeFieldKey}\n readOnly={readOnly}\n onChange={(childValue) =>\n onChange(setObjectValue(value, propertyKey, childValue))\n }\n onFieldFocus={onFieldFocus}\n onUndo={() =>\n onChange(\n setObjectValue(value, propertyKey, childField.expected)\n )\n }\n onSetNull={() =>\n onChange(setObjectValue(value, propertyKey, null))\n }\n />\n )\n })\n ) : (\n <div className="rounded-md bg-background px-2 py-1.5 text-sm text-muted-foreground">\n No properties\n </div>\n )}\n </div>\n </div>\n ) : field.schema.type === "array" ? (\n <div className="grid gap-2">\n <HumanReviewArrayValueGrid\n activeNestedSide={syncedArrayNestedView.activeSide}\n activeSelectionSide={syncedArraySelection.activeSide}\n label="Actual"\n readOnly\n schema={field.schema}\n selectionDepth={syncedArraySelection.depth}\n sharedGridSelection={syncedArraySelection.gridSelection}\n sharedNestedStack={syncedArrayNestedView.stack}\n value={field.actual}\n viewSide="actual"\n onGridSelectionChange={updateSyncedArraySelection}\n onNestedStackChange={updateSyncedArrayNestedView}\n />\n <HumanReviewArrayValueGrid\n activeNestedSide={syncedArrayNestedView.activeSide}\n activeSelectionSide={syncedArraySelection.activeSide}\n label="Expected"\n readOnly={readOnly}\n schema={field.schema}\n selectionDepth={syncedArraySelection.depth}\n sharedGridSelection={syncedArraySelection.gridSelection}\n sharedNestedStack={syncedArrayNestedView.stack}\n value={value}\n viewSide="expected"\n onChange={onChange}\n onGridSelectionChange={updateSyncedArraySelection}\n onNestedStackChange={updateSyncedArrayNestedView}\n />\n </div>\n ) : (\n <div className="grid gap-2 sm:grid-cols-2">\n <div className="rounded-md border bg-muted/30 p-2">\n {field.schema.description ? (\n <p className="mb-2 text-xs text-muted-foreground">\n {field.schema.description}\n </p>\n ) : null}\n <div className="mb-1 text-[11px] font-medium text-muted-foreground">\n Actual\n </div>\n <div className="min-h-7 rounded-md bg-background px-2 py-1.5 text-sm">\n {formatValue(field.actual)}\n </div>\n </div>\n <div className="rounded-md border bg-muted/30 p-2">\n <div className="mb-1 text-[11px] font-medium text-muted-foreground">\n Expected\n </div>\n <HumanReviewValueInput\n readOnly={readOnly}\n schema={field.schema}\n value={getPrimitiveValue(value)}\n onChange={onChange}\n />\n </div>\n </div>\n )}\n </div>\n )\n}\n\nfunction HumanReviewHighlight({ field }: { field: ReviewField }) {\n const area = field.location?.area\n\n if (!area) return null\n\n return (\n <div\n className={cn(\n "pointer-events-none absolute z-10 rounded-[3px] border",\n REVIEW_HIGHLIGHT_STYLE\n )}\n style={{\n left: `${area.left}%`,\n top: `${area.top}%`,\n width: `${area.width}%`,\n height: `${area.height}%`,\n }}\n />\n )\n}\n\nexport function JsonDiffView({\n actual,\n expected,\n theme = "light",\n}: {\n actual: JsonObject\n expected: JsonObject\n theme?: HumanReviewTheme\n}) {\n const oldFile = React.useMemo(\n () => ({\n name: "actual.json",\n contents: formatJson(actual),\n lang: "json",\n }),\n [actual]\n )\n const newFile = React.useMemo(\n () => ({\n name: "expected.json",\n contents: formatJson(expected),\n lang: "json",\n }),\n [expected]\n )\n\n return (\n <Virtualizer\n className="h-full overflow-auto rounded-b-xl bg-surface/60"\n contentClassName="min-w-full"\n >\n <div className="human-review-diff h-full text-xs">\n <MultiFileDiff\n className="block min-w-full"\n style={DIFF_VIEWER_THEME}\n oldFile={oldFile}\n newFile={newFile}\n options={{\n diffStyle: "split",\n diffIndicators: "bars",\n hunkSeparators: "line-info-basic",\n overflow: "wrap",\n themeType: theme,\n theme: {\n light: "pierre-light",\n dark: "pierre-dark",\n },\n }}\n />\n </div>\n </Virtualizer>\n )\n}\n\nexport function HumanReviewPanel({\n fields = REVIEW_FIELDS,\n activeFieldKey,\n className,\n onFieldFocus,\n theme = "light",\n}: {\n fields?: ReviewField[]\n activeFieldKey?: string\n className?: string\n onFieldFocus?: (field: ReviewField) => void\n theme?: HumanReviewTheme\n} = {}) {\n const [activeTab, setActiveTab] = React.useState("form")\n const actualValues = React.useMemo(\n () => valuesFromFields(fields, "actual"),\n [fields]\n )\n const initialExpectedValues = React.useMemo(\n () => valuesFromFields(fields, "expected"),\n [fields]\n )\n const [expected, setExpected] = React.useState<JsonObject>(\n initialExpectedValues\n )\n\n React.useEffect(() => {\n setExpected(initialExpectedValues)\n }, [initialExpectedValues])\n\n const updateValue = React.useCallback((key: string, value: JsonValue) => {\n setExpected((current) =>\n Object.is(current[key], value) ? current : { ...current, [key]: value }\n )\n }, [])\n const fieldCount = React.useMemo(() => countReviewFields(fields), [fields])\n\n return (\n <TooltipProvider delay={200}>\n <Tabs\n value={activeTab}\n onValueChange={setActiveTab}\n className={cn("flex h-[560px] flex-col gap-0 bg-background", className)}\n >\n <div className="flex min-h-12 items-center justify-between gap-3 border-b px-3">\n <TabsList className="h-8 sm:h-7">\n <TabsTrigger value="form" className="h-7 sm:h-6">\n <HugeiconsIcon icon={TextCheckIcon} className="size-4" />\n Form\n </TabsTrigger>\n <TabsTrigger value="json" className="h-7 sm:h-6">\n <HugeiconsIcon icon={SourceCodeSquareIcon} className="size-4" />\n JSON\n </TabsTrigger>\n </TabsList>\n <div className="flex h-8 items-center gap-1 rounded-md border bg-muted/40 px-2 text-xs text-muted-foreground sm:h-7">\n <HugeiconsIcon icon={FileDiffIcon} className="size-3.5" />\n {fieldCount} fields\n </div>\n </div>\n <TabsContent value="form" keepMounted className="min-h-0 flex-1">\n <ScrollArea className="h-full" scrollFade>\n <div className="space-y-3 p-3">\n {fields.map((field) => (\n <HumanReviewFieldCard\n key={field.key}\n field={field}\n value={expected[field.key] ?? null}\n originalValue={field.expected}\n active={\n field.key === activeFieldKey ||\n activeFieldKey?.startsWith(`${field.key}.`)\n }\n activeFieldKey={activeFieldKey}\n onChange={(value) => updateValue(field.key, value)}\n onFieldFocus={onFieldFocus}\n onUndo={() => updateValue(field.key, field.expected)}\n onSetNull={() => updateValue(field.key, null)}\n />\n ))}\n </div>\n </ScrollArea>\n </TabsContent>\n <TabsContent value="json" keepMounted className="min-h-0 flex-1">\n <JsonDiffView\n actual={actualValues}\n expected={expected}\n theme={theme}\n />\n </TabsContent>\n </Tabs>\n </TooltipProvider>\n )\n}\n\nexport function HumanReviewBlock({\n file,\n fields = REVIEW_FIELDS,\n className,\n theme,\n}: {\n file?: string\n fields?: ReviewField[]\n className?: string\n theme?: HumanReviewTheme\n}) {\n const [activeFieldKey, setActiveFieldKey] = React.useState(fields[0]?.key)\n const viewerRef = React.useRef<PDFViewerHandle>(null)\n const activeField = findReviewField(fields, activeFieldKey) ?? fields[0]\n\n React.useEffect(() => {\n if (activeFieldKey || !fields[0]) return\n setActiveFieldKey(fields[0].key)\n }, [activeFieldKey, fields])\n\n const focusField = React.useCallback((field: ReviewField) => {\n if (field.key === activeFieldKey) return\n\n setActiveFieldKey(field.key)\n\n if (field.location) {\n viewerRef.current?.scrollToPageArea(\n field.location.page,\n field.location.area\n )\n }\n }, [activeFieldKey])\n\n return (\n <PdfBlockResizableShell\n autoSaveId="pdf-block-human-review"\n className={className}\n rightDefaultSize={42}\n rightMaxSize={60}\n rightMinSize={30}\n left={\n <PDFViewer\n ref={viewerRef}\n file={file}\n defaultZoom={DEFAULT_ZOOM}\n renderPageOverlay={({ pageNumber }) =>\n activeField?.location?.page === pageNumber ? (\n <HumanReviewHighlight field={activeField} />\n ) : null\n }\n />\n }\n right={\n <aside className="min-h-0 bg-background">\n <HumanReviewPanel\n fields={fields}\n activeFieldKey={activeField?.key}\n className="h-full min-h-0"\n theme={theme}\n onFieldFocus={focusField}\n />\n </aside>\n }\n />\n )\n}\n'
-
+ '"use client"\n\nimport * as React from "react"\nimport {\n DataEditor,\n emptyGridSelection,\n GridCellKind,\n type EditableGridCell,\n type GridCell,\n type GridColumn,\n type GridSelection,\n type Item,\n type NumberCell,\n type ProvideEditorComponent,\n type Rectangle,\n type TextCell,\n type Theme,\n} from "@glideapps/glide-data-grid"\nimport {\n ArrowLeft01Icon,\n CancelCircleIcon,\n FileDiffIcon,\n InputNumericIcon,\n InputTextIcon,\n SecondBracketIcon,\n SourceCodeSquareIcon,\n TextCheckIcon,\n Undo02Icon,\n} from "@hugeicons/core-free-icons"\nimport { HugeiconsIcon } from "@hugeicons/react"\nimport { MultiFileDiff, Virtualizer } from "@pierre/diffs/react"\n\nimport { cn } from "@/lib/utils"\nimport { PDFViewer, type PDFViewerHandle } from "@/components/ui/pdf-viewer"\nimport { PdfBlockResizableShell } from "@/components/pdf-block-resizable-shell"\nimport { Button } from "@/registry/new-york-v4/ui/button"\nimport { Input } from "@/registry/new-york-v4/ui/input"\nimport { ScrollArea } from "@/registry/new-york-v4/ui/scroll-area"\nimport {\n Tabs,\n TabsContent,\n TabsList,\n TabsTrigger,\n} from "@/registry/new-york-v4/ui/tabs"\nimport {\n Tooltip,\n TooltipContent,\n TooltipProvider,\n TooltipTrigger,\n} from "@/registry/new-york-v4/ui/tooltip"\n\nimport "@glideapps/glide-data-grid/dist/index.css"\n\nexport type JsonPrimitive = string | number | boolean | null\nexport type JsonValue = JsonPrimitive | JsonObject | JsonArray\nexport type JsonObject = { [key: string]: JsonValue }\nexport type JsonArray = JsonValue[]\nexport type SchemaPropertyType =\n | "string"\n | "number"\n | "integer"\n | "boolean"\n | "object"\n | "array"\n\nexport type ReviewFieldSchema = {\n type: SchemaPropertyType\n title?: string\n description?: string\n enum?: Array<string | number>\n properties?: Record<string, ReviewFieldSchema>\n items?: ReviewFieldSchema\n}\n\nexport type HighlightArea = {\n left: number\n top: number\n width: number\n height: number\n}\n\nexport type ReviewField = {\n key: string\n schema: ReviewFieldSchema\n actual: JsonValue\n expected: JsonValue\n location?: {\n page: number\n area: HighlightArea\n }\n}\n\nconst DEFAULT_ZOOM = 0.75\nconst REVIEW_HIGHLIGHT_STYLE =\n "border-blue-500/70 bg-blue-500/12 shadow-[0_4px_16px_rgb(59_130_246_/_10%)]"\nconst DIFF_VIEWER_THEME = {\n "--diffs-light-bg": "oklch(0.985 0.002 247)",\n "--diffs-dark-bg": "oklch(0.18 0.003 247)",\n "--diffs-light": "oklch(0.22 0.01 247)",\n "--diffs-dark": "oklch(0.92 0.006 247)",\n "--diffs-bg-context-override":\n "light-dark(oklch(0.967 0.003 247), oklch(0.235 0.004 247))",\n "--diffs-bg-context-gutter-override":\n "light-dark(oklch(0.948 0.004 247), oklch(0.205 0.004 247))",\n "--diffs-bg-separator-override":\n "light-dark(oklch(0.94 0.004 247), oklch(0.255 0.005 247))",\n "--diffs-bg-buffer-override":\n "light-dark(oklch(0.955 0.004 247), oklch(0.225 0.004 247))",\n "--diffs-light-addition-color": "oklch(0.54 0.13 158)",\n "--diffs-dark-addition-color": "oklch(0.72 0.13 158)",\n "--diffs-light-deletion-color": "oklch(0.55 0.16 28)",\n "--diffs-dark-deletion-color": "oklch(0.72 0.14 28)",\n "--diffs-bg-addition-override":\n "light-dark(oklch(0.957 0.032 158), oklch(0.255 0.052 158))",\n "--diffs-bg-addition-emphasis-override":\n "light-dark(oklch(0.88 0.06 158), oklch(0.36 0.08 158))",\n "--diffs-bg-deletion-override":\n "light-dark(oklch(0.958 0.03 28), oklch(0.255 0.047 28))",\n "--diffs-bg-deletion-emphasis-override":\n "light-dark(oklch(0.9 0.052 28), oklch(0.36 0.075 28))",\n "--diffs-fg-number-override":\n "light-dark(oklch(0.56 0.018 247), oklch(0.66 0.012 247))",\n "--diffs-font-size": "12px",\n "--diffs-line-height": "20px",\n} as React.CSSProperties\n\nfunction readIsDarkTheme() {\n return (\n typeof document !== "undefined" &&\n document.documentElement.classList.contains("dark")\n )\n}\n\n// A single shared MutationObserver backs every consumer. Each grid previously\n// created its own observer (two, via useHumanReviewGridTheme), so opening a\n// nested array view spun up and tore down several observers at once.\nconst darkThemeListeners = new Set<(isDark: boolean) => void>()\nlet darkThemeObserver: MutationObserver | null = null\nlet sharedIsDarkTheme = false\n\nfunction ensureDarkThemeObserver() {\n if (\n darkThemeObserver ||\n typeof document === "undefined" ||\n typeof MutationObserver === "undefined"\n ) {\n return\n }\n\n sharedIsDarkTheme = readIsDarkTheme()\n darkThemeObserver = new MutationObserver(() => {\n const nextIsDark = readIsDarkTheme()\n if (nextIsDark === sharedIsDarkTheme) return\n\n sharedIsDarkTheme = nextIsDark\n darkThemeListeners.forEach((listener) => listener(nextIsDark))\n })\n darkThemeObserver.observe(document.documentElement, {\n attributes: true,\n attributeFilter: ["class"],\n })\n}\n\nfunction useIsDarkTheme() {\n const [isDark, setIsDark] = React.useState(readIsDarkTheme)\n\n React.useEffect(() => {\n ensureDarkThemeObserver()\n setIsDark(sharedIsDarkTheme)\n darkThemeListeners.add(setIsDark)\n\n return () => {\n darkThemeListeners.delete(setIsDark)\n if (darkThemeListeners.size === 0 && darkThemeObserver) {\n darkThemeObserver.disconnect()\n darkThemeObserver = null\n }\n }\n }, [])\n\n return isDark\n}\n\nfunction useHumanReviewGridTheme() {\n const isDark = useIsDarkTheme()\n\n return React.useMemo<Partial<Theme>>(\n () => ({\n accentColor: isDark ? "rgb(96, 165, 250)" : "rgb(37, 99, 235)",\n accentLight: isDark ? "rgba(29, 78, 216, 0.15)" : "rgb(219, 234, 254)",\n accentFg: "rgb(255, 255, 255)",\n textDark: isDark ? "rgb(229, 229, 229)" : "rgb(23, 23, 23)",\n textMedium: isDark ? "rgb(163, 163, 163)" : "rgb(82, 82, 82)",\n textLight: isDark ? "rgb(115, 115, 115)" : "rgb(163, 163, 163)",\n textBubble: isDark ? "rgb(245, 245, 245)" : "rgb(23, 23, 23)",\n textHeader: isDark ? "rgb(245, 245, 245)" : "rgb(23, 23, 23)",\n textGroupHeader: isDark ? "rgb(163, 163, 163)" : "rgb(82, 82, 82)",\n bgCell: isDark ? "rgb(10, 10, 10)" : "rgb(255, 255, 255)",\n bgCellMedium: isDark ? "rgb(23, 23, 23)" : "rgb(250, 250, 250)",\n bgHeader: isDark ? "rgb(23, 23, 23)" : "rgb(250, 250, 250)",\n bgHeaderHasFocus: isDark ? "rgb(38, 38, 38)" : "rgb(245, 245, 245)",\n bgHeaderHovered: isDark ? "rgb(38, 38, 38)" : "rgb(245, 245, 245)",\n borderColor: isDark ? "rgb(38, 38, 38)" : "rgb(229, 229, 229)",\n horizontalBorderColor: isDark ? "rgb(38, 38, 38)" : "rgb(229, 229, 229)",\n cellHorizontalPadding: 8,\n cellVerticalPadding: 3,\n headerIconSize: 18,\n baseFontStyle: "13px",\n headerFontStyle: "600 13px",\n markerFontStyle: "11px",\n fontFamily: \'-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif\',\n editorFontSize: "13px",\n }),\n [isDark]\n )\n}\n\nconst REVIEW_FIELDS: ReviewField[] = [\n {\n key: "vendor_name",\n schema: {\n type: "string",\n title: "Vendor name",\n description: "Supplier shown on the invoice header.",\n },\n actual: "Acme Supplies LLC",\n expected: "Acme Supply LLC",\n location: {\n page: 1,\n area: { left: 31, top: 30, width: 40, height: 5.8 },\n },\n },\n {\n key: "line_items",\n schema: {\n type: "array",\n title: "Line items",\n description: "Invoice rows with nested allocation details.",\n items: {\n type: "object",\n properties: {\n description: {\n type: "string",\n title: "Description",\n },\n quantity: {\n type: "number",\n title: "Qty",\n },\n unit_price: {\n type: "number",\n title: "Unit price",\n },\n tax_detail: {\n type: "object",\n title: "Tax detail",\n properties: {\n code: {\n type: "string",\n title: "Code",\n },\n jurisdiction: {\n type: "string",\n title: "Jurisdiction",\n },\n },\n },\n allocations: {\n type: "array",\n title: "Allocations",\n items: {\n type: "object",\n properties: {\n department: {\n type: "string",\n title: "Department",\n },\n percent: {\n type: "number",\n title: "Percent",\n },\n },\n },\n },\n },\n },\n },\n actual: [\n {\n description: "Transformer implementation support",\n quantity: 2,\n unit_price: 4200,\n tax_detail: { code: "TX-CA", jurisdiction: "CA" },\n allocations: [\n { department: "Research", percent: 70 },\n { department: "Platform", percent: 20 },\n ],\n },\n {\n description: "Attention model review",\n quantity: 1,\n unit_price: 4080,\n tax_detail: { code: "TX-NY", jurisdiction: "NY" },\n allocations: [{ department: "Research", percent: 100 }],\n },\n ],\n expected: [\n {\n description: "Transformer implementation support",\n quantity: 2,\n unit_price: 4200,\n tax_detail: { code: "TX-CA", jurisdiction: "CA" },\n allocations: [\n { department: "Research", percent: 70 },\n { department: "Platform", percent: 30 },\n ],\n },\n {\n description: "Attention model review",\n quantity: 1,\n unit_price: 4080.75,\n tax_detail: { code: "TX-NY", jurisdiction: "NY" },\n allocations: [{ department: "Research", percent: 100 }],\n },\n ],\n location: {\n page: 1,\n area: { left: 13.5, top: 66, width: 73.5, height: 7.5 },\n },\n },\n {\n key: "total_amount",\n schema: {\n type: "number",\n title: "Total amount",\n description: "Final amount due including tax.",\n },\n actual: 12480,\n expected: 12480.75,\n location: {\n page: 1,\n area: { left: 13.5, top: 66, width: 73.5, height: 7.5 },\n },\n },\n {\n key: "payment_terms",\n schema: {\n type: "string",\n title: "Payment terms",\n enum: ["Due on receipt", "Net 15", "Net 30"],\n },\n actual: "Net 15",\n expected: "Net 30",\n location: {\n page: 1,\n area: { left: 13.5, top: 55.5, width: 73.5, height: 7.5 },\n },\n },\n {\n key: "requires_review",\n schema: {\n type: "boolean",\n title: "Requires review",\n description: "Whether a human should verify this document before export.",\n },\n actual: false,\n expected: true,\n location: {\n page: 2,\n area: { left: 9.5, top: 12, width: 81, height: 11.5 },\n },\n },\n {\n key: "remittance",\n schema: {\n type: "object",\n title: "Remittance details",\n description: "Payment destination used for invoice reconciliation.",\n properties: {\n account_holder: {\n type: "string",\n title: "Account holder",\n },\n routing_number: {\n type: "string",\n title: "Routing number",\n },\n verified: {\n type: "boolean",\n title: "Verified",\n },\n },\n },\n actual: {\n account_holder: "Acme Supplies LLC",\n routing_number: "021000021",\n verified: false,\n },\n expected: {\n account_holder: "Acme Supply LLC",\n routing_number: "021000021",\n verified: true,\n },\n },\n]\n\nfunction valuesFromFields(\n fields: ReviewField[],\n valueKey: "actual" | "expected"\n) {\n return fields.reduce<JsonObject>((values, field) => {\n values[field.key] = field[valueKey]\n return values\n }, {})\n}\n\nfunction formatJson(value: unknown) {\n return JSON.stringify(value, null, 2)\n}\n\nfunction isJsonObject(value: JsonValue): value is JsonObject {\n return typeof value === "object" && value !== null && !Array.isArray(value)\n}\n\nfunction isJsonArray(value: JsonValue): value is JsonArray {\n return Array.isArray(value)\n}\n\nfunction getObjectValue(value: JsonValue, key: string): JsonValue {\n if (!isJsonObject(value)) return null\n return value[key] ?? null\n}\n\nfunction setObjectValue(\n value: JsonValue,\n key: string,\n childValue: JsonValue\n): JsonObject {\n return {\n ...(isJsonObject(value) ? value : {}),\n [key]: childValue,\n }\n}\n\nfunction getArrayValue(value: JsonValue): JsonArray {\n return isJsonArray(value) ? value : []\n}\n\nfunction setArrayItemValue(\n value: JsonValue,\n index: number,\n childValue: JsonValue\n): JsonArray {\n const nextValue = getArrayValue(value).slice()\n nextValue[index] = childValue\n return nextValue\n}\n\nfunction getPrimitiveValue(value: JsonValue): JsonPrimitive {\n return isJsonObject(value) || isJsonArray(value) ? null : value\n}\n\nfunction jsonValuesEqual(left: JsonValue, right: JsonValue) {\n return formatJson(left) === formatJson(right)\n}\n\nfunction countReviewFields(fields: ReviewField[]): number {\n return fields.reduce((count, field) => {\n if (field.schema.type !== "object") return count + 1\n\n const properties = field.schema.properties ?? {}\n const childFields = Object.entries(properties).map(\n ([key, schema]): ReviewField => ({\n key: `${field.key}.${key}`,\n schema,\n actual: getObjectValue(field.actual, key),\n expected: getObjectValue(field.expected, key),\n })\n )\n\n return count + Math.max(countReviewFields(childFields), 1)\n }, 0)\n}\n\nfunction findReviewField(\n fields: ReviewField[],\n fieldKey: string | undefined\n): ReviewField | undefined {\n if (!fieldKey) return undefined\n\n for (const field of fields) {\n if (field.key === fieldKey) return field\n\n if (field.schema.type === "object") {\n const childFields = Object.entries(field.schema.properties ?? {}).map(\n ([key, schema]): ReviewField => ({\n key: `${field.key}.${key}`,\n schema,\n actual: getObjectValue(field.actual, key),\n expected: getObjectValue(field.expected, key),\n })\n )\n const childField = findReviewField(childFields, fieldKey)\n if (childField) return childField\n }\n }\n\n return undefined\n}\n\nfunction formatValue(value: JsonValue) {\n if (value === null) return "NULL"\n if (isJsonObject(value) || isJsonArray(value)) return formatJson(value)\n if (typeof value === "boolean") return value ? "true" : "false"\n return String(value)\n}\n\nfunction areGridRangesEqual(\n left: Readonly<Rectangle> | undefined,\n right: Readonly<Rectangle> | undefined\n) {\n return (\n left === right ||\n (left !== undefined &&\n right !== undefined &&\n left.x === right.x &&\n left.y === right.y &&\n left.width === right.width &&\n left.height === right.height)\n )\n}\n\nfunction areGridRangeStacksEqual(\n left: readonly Readonly<Rectangle>[] | undefined,\n right: readonly Readonly<Rectangle>[] | undefined\n) {\n if (left === right) return true\n if (!left || !right || left.length !== right.length) return false\n\n return left.every((range, index) => areGridRangesEqual(range, right[index]))\n}\n\nfunction areGridSelectionsEqual(left: GridSelection, right: GridSelection) {\n const leftCurrent = left.current\n const rightCurrent = right.current\n\n return (\n leftCurrent?.cell[0] === rightCurrent?.cell[0] &&\n leftCurrent?.cell[1] === rightCurrent?.cell[1] &&\n areGridRangesEqual(leftCurrent?.range, rightCurrent?.range) &&\n areGridRangeStacksEqual(\n leftCurrent?.rangeStack,\n rightCurrent?.rangeStack\n ) &&\n left.columns.equals(right.columns) &&\n left.rows.equals(right.rows)\n )\n}\n\nfunction getGridSelectionRanges(selection: GridSelection | undefined) {\n const current = selection?.current\n if (!current) return []\n\n return [...(current.rangeStack ?? []), current.range]\n}\n\nfunction areArrayNestedViewsEqual(\n left: ArrayNestedView[],\n right: ArrayNestedView[]\n) {\n if (left === right) return true\n if (left.length !== right.length) return false\n\n return left.every((view, index) => {\n const other = right[index]\n\n return (\n view.rowIndex === other?.rowIndex &&\n view.columnId === other.columnId &&\n view.title === other.title &&\n view.schema === other.schema &&\n Object.is(view.value, other.value)\n )\n })\n}\n\ntype HumanReviewOverlayCell = TextCell | NumberCell\n\ntype HumanReviewTextOverlayEditorProps = React.ComponentProps<\n ProvideEditorComponent<HumanReviewOverlayCell>\n> & {\n overlayOpenRef: React.RefObject<boolean>\n readOnly?: boolean\n}\n\nfunction HumanReviewTextOverlayEditor({\n isHighlighted,\n onFinishedEditing,\n overlayOpenRef,\n readOnly = false,\n validatedSelection,\n value,\n}: HumanReviewTextOverlayEditorProps) {\n const inputRef = React.useRef<HTMLTextAreaElement | null>(null)\n const initialValue =\n value.kind === GridCellKind.Number ? value.displayData : value.data\n const latestValueRef = React.useRef(initialValue)\n const finishedRef = React.useRef(false)\n\n const finishEditing = React.useCallback(\n (\n shouldSave: boolean,\n movement: readonly [-1 | 0 | 1, -1 | 0 | 1] = [0, 0]\n ) => {\n if (finishedRef.current) return\n\n finishedRef.current = true\n overlayOpenRef.current = false\n\n if (!shouldSave || readOnly) {\n onFinishedEditing(undefined, movement)\n return\n }\n\n if (value.kind === GridCellKind.Number) {\n const numericValue = Number(latestValueRef.current)\n\n onFinishedEditing(\n {\n ...value,\n data: Number.isFinite(numericValue) ? numericValue : value.data,\n displayData: latestValueRef.current,\n },\n movement\n )\n return\n }\n\n onFinishedEditing(\n {\n ...value,\n data: latestValueRef.current,\n displayData: latestValueRef.current,\n },\n movement\n )\n },\n [onFinishedEditing, overlayOpenRef, readOnly, value]\n )\n\n React.useEffect(() => {\n overlayOpenRef.current = true\n\n const input = inputRef.current\n if (!input) {\n return () => {\n overlayOpenRef.current = false\n }\n }\n\n const handleInput = (event: Event) => {\n event.stopPropagation()\n latestValueRef.current = input.value\n }\n const stopTextInputPropagation = (event: Event) => {\n event.stopPropagation()\n }\n const handleKeyDown = (event: KeyboardEvent) => {\n event.stopPropagation()\n\n if (event.key === "Escape") {\n event.preventDefault()\n finishEditing(false)\n return\n }\n\n if (event.key === "Tab") {\n event.preventDefault()\n finishEditing(true, [event.shiftKey ? -1 : 1, 0])\n return\n }\n\n if (event.key === "Enter" && !event.shiftKey) {\n event.preventDefault()\n finishEditing(true, [0, 1])\n }\n }\n\n input.addEventListener("input", handleInput)\n input.addEventListener("beforeinput", stopTextInputPropagation, true)\n input.addEventListener("keydown", handleKeyDown, true)\n\n if (!readOnly) {\n input.focus()\n\n if (validatedSelection !== undefined) {\n const selectionRange =\n typeof validatedSelection === "number"\n ? [validatedSelection, validatedSelection]\n : validatedSelection\n input.setSelectionRange(selectionRange[0], selectionRange[1])\n } else if (isHighlighted) {\n input.setSelectionRange(0, input.value.length)\n } else {\n input.setSelectionRange(input.value.length, input.value.length)\n }\n }\n\n return () => {\n input.removeEventListener("input", handleInput)\n input.removeEventListener("beforeinput", stopTextInputPropagation, true)\n input.removeEventListener("keydown", handleKeyDown, true)\n overlayOpenRef.current = false\n }\n }, [\n finishEditing,\n isHighlighted,\n overlayOpenRef,\n validatedSelection,\n readOnly,\n ])\n\n React.useEffect(() => {\n const handlePointerOutside = (event: PointerEvent | MouseEvent) => {\n const input = inputRef.current\n const overlayRoot = input?.closest(".gdg-clip-region")\n\n if (!overlayRoot || overlayRoot.contains(event.target as Node | null)) {\n return\n }\n\n finishEditing(true)\n }\n\n document.addEventListener("pointerdown", handlePointerOutside, true)\n document.addEventListener("contextmenu", handlePointerOutside, true)\n\n return () => {\n document.removeEventListener("pointerdown", handlePointerOutside, true)\n document.removeEventListener("contextmenu", handlePointerOutside, true)\n }\n }, [finishEditing])\n\n return (\n <textarea\n ref={inputRef}\n className="gdg-input"\n defaultValue={initialValue}\n disabled={readOnly}\n dir="auto"\n style={{ height: "100%", minHeight: 32, resize: "none", width: "100%" }}\n />\n )\n}\n\nfunction getFieldIcon(type: SchemaPropertyType) {\n if (type === "number" || type === "integer") return InputNumericIcon\n if (type === "boolean") return TextCheckIcon\n if (type === "array") return SecondBracketIcon\n if (type === "object") return SourceCodeSquareIcon\n return InputTextIcon\n}\n\nfunction HumanReviewValueInput({\n readOnly = false,\n schema,\n value,\n onChange,\n}: {\n readOnly?: boolean\n schema: ReviewFieldSchema\n value: JsonPrimitive\n onChange: (value: JsonPrimitive) => void\n}) {\n if (schema.enum?.length) {\n return (\n <span className="relative inline-flex w-full rounded-lg border border-input bg-background text-sm text-foreground shadow-xs/5 dark:bg-input/32">\n <select\n disabled={readOnly}\n value={value === null ? "" : String(value)}\n onChange={(event) => onChange(event.target.value)}\n className="h-8.5 w-full appearance-none rounded-[inherit] bg-transparent px-3 text-sm outline-none sm:h-7.5"\n >\n {schema.enum.map((option) => (\n <option key={String(option)} value={String(option)}>\n {String(option)}\n </option>\n ))}\n </select>\n </span>\n )\n }\n\n if (schema.type === "number" || schema.type === "integer") {\n return (\n <Input\n nativeInput\n readOnly={readOnly}\n type="number"\n value={value === null ? "" : String(value)}\n onChange={(event) => {\n const nextValue = event.currentTarget.value\n onChange(nextValue === "" ? null : Number(nextValue))\n }}\n />\n )\n }\n\n if (schema.type === "boolean") {\n return (\n <div className="grid grid-cols-2 gap-1 rounded-lg bg-muted p-0.5">\n {[true, false].map((option) => (\n <Button\n key={String(option)}\n type="button"\n size="sm"\n variant={value === option ? "outline" : "ghost"}\n className={cn(\n "h-7 shadow-none",\n value === option && "bg-background dark:bg-input"\n )}\n disabled={readOnly}\n onClick={() => onChange(option)}\n >\n {option ? "True" : "False"}\n </Button>\n ))}\n </div>\n )\n }\n\n return (\n <Input\n nativeInput\n readOnly={readOnly}\n value={value === null ? "" : String(value)}\n onChange={(event) => onChange(event.currentTarget.value)}\n />\n )\n}\n\nfunction getArrayItemSchema(schema: ReviewFieldSchema): ReviewFieldSchema {\n return schema.items ?? { type: "string" }\n}\n\nfunction isComplexSchema(schema: ReviewFieldSchema) {\n return schema.type === "object" || schema.type === "array"\n}\n\nfunction summarizeComplexValue(value: JsonValue) {\n if (isJsonArray(value)) return `${value.length} items`\n if (isJsonObject(value)) return `${Object.keys(value).length} fields`\n return formatValue(value)\n}\n\nfunction getCellValueForArrayColumn(\n rowValue: JsonValue,\n itemSchema: ReviewFieldSchema,\n columnId: string\n) {\n if (itemSchema.type === "object") {\n return getObjectValue(rowValue, columnId)\n }\n\n return rowValue\n}\n\nfunction getCellSchemaForArrayColumn(\n itemSchema: ReviewFieldSchema,\n columnId: string\n) {\n if (itemSchema.type === "object") {\n return itemSchema.properties?.[columnId] ?? { type: "string" }\n }\n\n return itemSchema\n}\n\nfunction applyPrimitiveEdit(\n schema: ReviewFieldSchema,\n value: EditableGridCell\n): JsonValue | undefined {\n if (schema.type === "boolean" && value.kind === GridCellKind.Boolean) {\n return value.data\n }\n\n if (\n (schema.type === "number" || schema.type === "integer") &&\n value.kind === GridCellKind.Number\n ) {\n return value.data ?? null\n }\n\n if (value.kind === GridCellKind.Text) {\n if (schema.type === "number" || schema.type === "integer") {\n return value.data.trim() === "" ? null : Number(value.data)\n }\n\n return value.data\n }\n\n return undefined\n}\n\ntype ArrayNestedView = {\n rowIndex: number\n columnId: string\n title: string\n schema: ReviewFieldSchema\n value: JsonValue\n}\n\ntype ArrayReviewSide = "actual" | "expected"\n\ntype SyncedArrayNestedView = {\n activeSide: ArrayReviewSide | null\n stack: ArrayNestedView[]\n}\n\ntype SyncedArraySelection = {\n activeSide: ArrayReviewSide | null\n depth: number\n gridSelection: GridSelection\n}\n\nexport type HumanReviewTheme = "light" | "dark"\n\nconst EMPTY_SYNCED_ARRAY_NESTED_VIEW: SyncedArrayNestedView = {\n activeSide: null,\n stack: [],\n}\n\nconst EMPTY_SYNCED_ARRAY_SELECTION: SyncedArraySelection = {\n activeSide: null,\n depth: 0,\n gridSelection: emptyGridSelection,\n}\n\nfunction setNestedArrayValue({\n value,\n schema,\n nestedStack,\n nextNestedValue,\n}: {\n value: JsonValue\n schema: ReviewFieldSchema\n nestedStack: ArrayNestedView[]\n nextNestedValue: JsonValue\n}): JsonValue {\n const [currentView, ...remainingViews] = nestedStack\n\n if (!currentView) return nextNestedValue\n\n const itemSchema = getArrayItemSchema(schema)\n const rowValue = getArrayValue(value)[currentView.rowIndex] ?? null\n const cellSchema = getCellSchemaForArrayColumn(\n itemSchema,\n currentView.columnId\n )\n const currentCellValue = getCellValueForArrayColumn(\n rowValue,\n itemSchema,\n currentView.columnId\n )\n const nextCellValue: JsonValue = remainingViews.length\n ? setNestedArrayValue({\n value: currentCellValue,\n schema: cellSchema,\n nestedStack: remainingViews,\n nextNestedValue,\n })\n : nextNestedValue\n const nextRowValue: JsonValue =\n itemSchema.type === "object"\n ? setObjectValue(rowValue, currentView.columnId, nextCellValue)\n : nextCellValue\n\n return setArrayItemValue(value, currentView.rowIndex, nextRowValue)\n}\n\nfunction getNestedArrayValue({\n value,\n schema,\n nestedStack,\n}: {\n value: JsonValue\n schema: ReviewFieldSchema\n nestedStack: ArrayNestedView[]\n}): JsonValue {\n const [currentView, ...remainingViews] = nestedStack\n\n if (!currentView) return value\n\n const itemSchema = getArrayItemSchema(schema)\n const rowValue = getArrayValue(value)[currentView.rowIndex] ?? null\n const cellSchema = getCellSchemaForArrayColumn(\n itemSchema,\n currentView.columnId\n )\n const cellValue = getCellValueForArrayColumn(\n rowValue,\n itemSchema,\n currentView.columnId\n )\n\n if (!remainingViews.length) return cellValue\n\n return getNestedArrayValue({\n value: cellValue,\n schema: cellSchema,\n nestedStack: remainingViews,\n })\n}\n\nfunction HumanReviewArrayValueGrid({\n activeNestedSide = null,\n activeSelectionSide = null,\n label,\n nestedStackBaseDepth = 0,\n readOnly = false,\n schema,\n selectionDepth = 0,\n sharedGridSelection,\n sharedNestedStack,\n value,\n viewSide = "expected",\n onChange,\n onGridSelectionChange,\n onNestedStackChange,\n}: {\n activeNestedSide?: ArrayReviewSide | null\n activeSelectionSide?: ArrayReviewSide | null\n label: string\n nestedStackBaseDepth?: number\n readOnly?: boolean\n schema: ReviewFieldSchema\n selectionDepth?: number\n sharedGridSelection?: GridSelection\n sharedNestedStack?: ArrayNestedView[]\n value: JsonValue\n viewSide?: ArrayReviewSide\n onChange?: (value: JsonValue) => void\n onGridSelectionChange?: (\n selection: GridSelection,\n side: ArrayReviewSide,\n depth: number\n ) => void\n onNestedStackChange?: (\n stack: ArrayNestedView[],\n side: ArrayReviewSide\n ) => void\n}) {\n const rows = getArrayValue(value)\n const itemSchema = getArrayItemSchema(schema)\n const gridTheme = useHumanReviewGridTheme()\n const isDark = useIsDarkTheme()\n const [localNestedStack, setLocalNestedStack] = React.useState<\n ArrayNestedView[]\n >([])\n const [localGridSelection, setLocalGridSelection] =\n React.useState<GridSelection>(emptyGridSelection)\n const fastTextOverlayOpenRef = React.useRef(false)\n const nestedStack = sharedNestedStack ?? localNestedStack\n const syncedGridSelection = sharedGridSelection ?? localGridSelection\n const gridSelection =\n sharedGridSelection &&\n (activeSelectionSide !== viewSide ||\n selectionDepth !== nestedStackBaseDepth)\n ? emptyGridSelection\n : syncedGridSelection\n const visibleNestedStack = nestedStack.slice(nestedStackBaseDepth)\n const activeNestedView = visibleNestedStack[0] ?? null\n const activeNestedValue = activeNestedView\n ? getNestedArrayValue({\n value,\n schema,\n nestedStack: [activeNestedView],\n })\n : null\n const isActiveNestedSource =\n Boolean(activeNestedView) && activeNestedSide === viewSide\n const isMirroredNestedTarget =\n Boolean(activeNestedView) &&\n activeNestedSide !== null &&\n activeNestedSide !== viewSide\n const blueCellText = isDark ? "rgb(147, 197, 253)" : "rgb(37, 99, 235)"\n const selectedCellBackground = isDark\n ? "rgba(37, 99, 235, 0.2)"\n : "rgba(219, 234, 254, 0.85)"\n const mirroredCellBackground = isDark\n ? "rgba(167, 139, 250, 0.18)"\n : "rgba(237, 233, 254, 0.9)"\n const mirroredHighlightRegions = React.useMemo<\n React.ComponentProps<typeof DataEditor>["highlightRegions"]\n >(() => {\n if (\n !activeSelectionSide ||\n activeSelectionSide === viewSide ||\n selectionDepth !== nestedStackBaseDepth\n ) {\n return undefined\n }\n\n const ranges = getGridSelectionRanges(sharedGridSelection)\n if (!ranges.length) return undefined\n\n return ranges.map((range) => ({\n color: mirroredCellBackground,\n range,\n style: "dashed" as const,\n }))\n }, [\n activeSelectionSide,\n mirroredCellBackground,\n nestedStackBaseDepth,\n selectionDepth,\n sharedGridSelection,\n viewSide,\n ])\n\n const setNestedStack = React.useCallback(\n (\n updater:\n | ArrayNestedView[]\n | ((current: ArrayNestedView[]) => ArrayNestedView[])\n ) => {\n if (onNestedStackChange) {\n const nextStack =\n typeof updater === "function" ? updater(nestedStack) : updater\n\n onNestedStackChange(nextStack, viewSide)\n return\n }\n\n setLocalNestedStack(updater)\n },\n [nestedStack, onNestedStackChange, viewSide]\n )\n\n const handleGridSelectionChange = React.useCallback(\n (selection: GridSelection) => {\n if (onGridSelectionChange) {\n if (\n activeSelectionSide === viewSide &&\n sharedGridSelection &&\n areGridSelectionsEqual(sharedGridSelection, selection)\n ) {\n return\n }\n\n onGridSelectionChange(selection, viewSide, nestedStackBaseDepth)\n return\n }\n\n setLocalGridSelection((current) =>\n areGridSelectionsEqual(current, selection) ? current : selection\n )\n },\n [\n activeSelectionSide,\n nestedStackBaseDepth,\n onGridSelectionChange,\n sharedGridSelection,\n viewSide,\n ]\n )\n\n const columns = React.useMemo<GridColumn[]>(() => {\n if (itemSchema.type === "object") {\n const propertyEntries = Object.entries(itemSchema.properties ?? {})\n\n if (propertyEntries.length) {\n return propertyEntries.map(([key, propertySchema]) => ({\n id: key,\n title: propertySchema.title ?? key,\n width: isComplexSchema(propertySchema) ? 148 : 132,\n }))\n }\n }\n\n return [\n {\n id: "value",\n title: itemSchema.title ?? "Value",\n width: isComplexSchema(itemSchema) ? 148 : 180,\n },\n ]\n }, [itemSchema])\n\n const getCellContent = React.useCallback(\n ([col, row]: Item): GridCell => {\n const column = columns[col]\n const columnId = String(column?.id ?? "value")\n const rowValue = rows[row] ?? null\n const cellSchema = getCellSchemaForArrayColumn(itemSchema, columnId)\n const cellValue = getCellValueForArrayColumn(\n rowValue,\n itemSchema,\n columnId\n )\n const matchedNestedCell =\n activeNestedView?.rowIndex === row &&\n activeNestedView.columnId === columnId\n const nestedCellTheme: Partial<Theme> | undefined = matchedNestedCell\n ? {\n bgCell: isMirroredNestedTarget\n ? mirroredCellBackground\n : selectedCellBackground,\n textDark: blueCellText,\n }\n : undefined\n\n if (isComplexSchema(cellSchema)) {\n return {\n kind: GridCellKind.Text,\n data: summarizeComplexValue(cellValue),\n displayData: summarizeComplexValue(cellValue),\n allowOverlay: false,\n readonly: true,\n cursor: "pointer",\n activationBehaviorOverride: "double-click",\n themeOverride: {\n textDark: blueCellText,\n ...(nestedCellTheme ?? {}),\n },\n }\n }\n\n if (cellSchema.type === "boolean") {\n return {\n kind: GridCellKind.Boolean,\n data: typeof cellValue === "boolean" ? cellValue : false,\n allowOverlay: false,\n readonly: readOnly,\n }\n }\n\n if (cellSchema.type === "number" || cellSchema.type === "integer") {\n return {\n kind: GridCellKind.Number,\n data: typeof cellValue === "number" ? cellValue : undefined,\n displayData: typeof cellValue === "number" ? String(cellValue) : "",\n allowOverlay: true,\n readonly: false,\n }\n }\n\n return {\n kind: GridCellKind.Text,\n data:\n cellValue === null ||\n isJsonObject(cellValue) ||\n isJsonArray(cellValue)\n ? ""\n : String(cellValue),\n displayData:\n cellValue === null ||\n isJsonObject(cellValue) ||\n isJsonArray(cellValue)\n ? ""\n : String(cellValue),\n allowOverlay: true,\n readonly: false,\n }\n },\n [\n activeNestedView,\n blueCellText,\n columns,\n isMirroredNestedTarget,\n itemSchema,\n mirroredCellBackground,\n readOnly,\n rows,\n selectedCellBackground,\n ]\n )\n\n const updateCellValue = React.useCallback(\n ([col, row]: Item, nextCell: EditableGridCell) => {\n if (readOnly || !onChange) return\n\n const column = columns[col]\n const columnId = String(column?.id ?? "value")\n const rowValue = rows[row] ?? null\n const cellSchema = getCellSchemaForArrayColumn(itemSchema, columnId)\n const nextValue = applyPrimitiveEdit(cellSchema, nextCell)\n\n if (nextValue === undefined) return\n\n const nextRowValue =\n itemSchema.type === "object"\n ? setObjectValue(rowValue, columnId, nextValue)\n : nextValue\n\n onChange(setArrayItemValue(value, row, nextRowValue))\n },\n [columns, itemSchema, onChange, readOnly, rows, value]\n )\n const provideEditor = React.useCallback<\n NonNullable<React.ComponentProps<typeof DataEditor>["provideEditor"]>\n >(\n (cell) => {\n if (\n cell.kind !== GridCellKind.Text &&\n cell.kind !== GridCellKind.Number\n ) {\n return undefined\n }\n\n return {\n editor: (props) => {\n if (\n props.value.kind !== GridCellKind.Text &&\n props.value.kind !== GridCellKind.Number\n ) {\n return null\n }\n\n return (\n <HumanReviewTextOverlayEditor\n {...(props as React.ComponentProps<\n ProvideEditorComponent<HumanReviewOverlayCell>\n >)}\n overlayOpenRef={fastTextOverlayOpenRef}\n readOnly={readOnly}\n />\n )\n },\n }\n },\n [readOnly]\n )\n const handleOutsideClick = React.useCallback(\n () => !fastTextOverlayOpenRef.current,\n []\n )\n\n const openNestedCell = React.useCallback(\n ([col, row]: Item) => {\n const column = columns[col]\n const columnId = String(column?.id ?? "value")\n const rowValue = rows[row] ?? null\n const cellSchema = getCellSchemaForArrayColumn(itemSchema, columnId)\n\n if (!isComplexSchema(cellSchema)) return\n\n setNestedStack((current) => {\n const nextView = {\n rowIndex: row,\n columnId,\n title: `${column?.title ?? columnId} / row ${row + 1}`,\n schema: cellSchema,\n value: getCellValueForArrayColumn(rowValue, itemSchema, columnId),\n }\n const currentView = current[nestedStackBaseDepth]\n\n if (\n currentView?.rowIndex === nextView.rowIndex &&\n currentView.columnId === nextView.columnId\n ) {\n return current\n }\n\n return [...current.slice(0, nestedStackBaseDepth), nextView]\n })\n },\n [columns, itemSchema, nestedStackBaseDepth, rows, setNestedStack]\n )\n\n const updateNestedValue = React.useCallback(\n (nextNestedValue: JsonValue) => {\n if (!activeNestedView || readOnly || !onChange) return\n\n onChange(\n setNestedArrayValue({\n value,\n schema,\n nestedStack: visibleNestedStack.slice(0, 1),\n nextNestedValue,\n })\n )\n setNestedStack((current) =>\n current.map((view, index) =>\n index === nestedStackBaseDepth\n ? { ...view, value: nextNestedValue }\n : view\n )\n )\n },\n [\n activeNestedView,\n nestedStackBaseDepth,\n onChange,\n readOnly,\n schema,\n setNestedStack,\n value,\n visibleNestedStack,\n ]\n )\n\n return (\n <div\n className={cn(\n "relative overflow-hidden rounded-md border bg-background transition-[border-color,background-color,box-shadow] focus-within:border-blue-500/50 focus-within:shadow-[0_0_0_1px_rgb(59_130_246_/_8%)] hover:border-blue-500/50",\n isActiveNestedSource &&\n "border-blue-500/60 bg-blue-500/5 shadow-[0_0_0_1px_rgb(59_130_246_/_10%)]"\n )}\n >\n <div>\n <div className="flex h-8 items-center justify-between gap-2 border-b px-2 text-[11px] font-medium text-muted-foreground">\n <span>{label}</span>\n <span>{rows.length} rows</span>\n </div>\n <div className="h-[220px]">\n <DataEditor\n columns={columns}\n rows={rows.length}\n getCellContent={getCellContent}\n cellActivationBehavior="double-click"\n gridSelection={gridSelection}\n highlightRegions={mirroredHighlightRegions}\n onCellEdited={updateCellValue}\n onCellActivated={openNestedCell}\n onGridSelectionChange={handleGridSelectionChange}\n provideEditor={provideEditor}\n isOutsideClick={handleOutsideClick}\n rowMarkers="number"\n smoothScrollX\n smoothScrollY\n theme={gridTheme}\n width="100%"\n height="100%"\n rowHeight={32}\n headerHeight={34}\n />\n </div>\n </div>\n {activeNestedView ? (\n <div className="absolute inset-0 z-10 flex flex-col bg-background">\n <div className="flex h-8 items-center gap-2 border-b px-2">\n <Button\n type="button"\n variant="ghost"\n size="icon-sm"\n className="size-6 text-muted-foreground"\n onClick={() =>\n setNestedStack((current) =>\n current.slice(\n 0,\n Math.max(nestedStackBaseDepth, current.length - 1)\n )\n )\n }\n aria-label="Back to parent array"\n >\n <HugeiconsIcon icon={ArrowLeft01Icon} className="size-3.5" />\n </Button>\n <div className="min-w-0 flex-1 truncate text-xs font-medium text-foreground">\n {activeNestedView.title}\n </div>\n </div>\n <div className="min-h-0 flex-1 overflow-auto p-2">\n {activeNestedView.schema.type === "array" ? (\n <HumanReviewArrayValueGrid\n label={\n activeNestedView.schema.title ?? activeNestedView.columnId\n }\n nestedStackBaseDepth={nestedStackBaseDepth + 1}\n readOnly={readOnly}\n schema={activeNestedView.schema}\n sharedGridSelection={sharedGridSelection}\n sharedNestedStack={sharedNestedStack}\n value={activeNestedValue}\n viewSide={viewSide}\n activeNestedSide={activeNestedSide}\n activeSelectionSide={activeSelectionSide}\n selectionDepth={selectionDepth}\n onChange={updateNestedValue}\n onGridSelectionChange={onGridSelectionChange}\n onNestedStackChange={onNestedStackChange}\n />\n ) : (\n <HumanReviewObjectValueEditor\n schema={activeNestedView.schema}\n value={activeNestedValue}\n originalValue={activeNestedValue}\n readOnly={readOnly}\n onChange={updateNestedValue}\n />\n )}\n </div>\n </div>\n ) : null}\n </div>\n )\n}\n\nfunction HumanReviewObjectValueEditor({\n schema,\n value,\n originalValue,\n readOnly = false,\n onChange,\n}: {\n schema: ReviewFieldSchema\n value: JsonValue\n originalValue: JsonValue\n readOnly?: boolean\n onChange: (value: JsonValue) => void\n}) {\n const propertyEntries = Object.entries(schema.properties ?? {})\n\n if (!propertyEntries.length) {\n return (\n <div className="rounded-md bg-background px-2 py-1.5 text-sm text-muted-foreground">\n No properties\n </div>\n )\n }\n\n return (\n <div className="space-y-2">\n {propertyEntries.map(([propertyKey, propertySchema]) => (\n <HumanReviewFieldCard\n key={propertyKey}\n field={{\n key: propertyKey,\n schema: propertySchema,\n actual: getObjectValue(originalValue, propertyKey),\n expected: getObjectValue(originalValue, propertyKey),\n }}\n value={getObjectValue(value, propertyKey)}\n originalValue={getObjectValue(originalValue, propertyKey)}\n readOnly={readOnly}\n onChange={(childValue) =>\n !readOnly &&\n onChange(setObjectValue(value, propertyKey, childValue))\n }\n onUndo={() =>\n !readOnly &&\n onChange(\n setObjectValue(\n value,\n propertyKey,\n getObjectValue(originalValue, propertyKey)\n )\n )\n }\n onSetNull={() =>\n !readOnly && onChange(setObjectValue(value, propertyKey, null))\n }\n />\n ))}\n </div>\n )\n}\n\ntype HumanReviewFieldCardProps = {\n field: ReviewField\n value: JsonValue\n originalValue: JsonValue\n active?: boolean\n activeFieldKey?: string\n readOnly?: boolean\n onChange: (value: JsonValue) => void\n onFieldFocus?: (field: ReviewField) => void\n onUndo: () => void\n onSetNull: () => void\n}\n\nfunction areHumanReviewFieldCardPropsEqual(\n previous: HumanReviewFieldCardProps,\n next: HumanReviewFieldCardProps\n) {\n return (\n previous.field === next.field &&\n Object.is(previous.value, next.value) &&\n Object.is(previous.originalValue, next.originalValue) &&\n previous.active === next.active &&\n previous.activeFieldKey === next.activeFieldKey &&\n previous.readOnly === next.readOnly &&\n previous.onFieldFocus === next.onFieldFocus\n )\n}\n\nconst HumanReviewFieldCard = React.memo(\n HumanReviewFieldCardBase,\n areHumanReviewFieldCardPropsEqual\n)\n\nfunction HumanReviewFieldCardBase({\n field,\n value,\n originalValue,\n active,\n activeFieldKey,\n readOnly = false,\n onChange,\n onFieldFocus,\n onUndo,\n onSetNull,\n}: HumanReviewFieldCardProps) {\n const modified = !jsonValuesEqual(value, originalValue)\n const Icon = getFieldIcon(field.schema.type)\n const propertyEntries = Object.entries(field.schema.properties ?? {})\n const [syncedArrayNestedView, setSyncedArrayNestedView] =\n React.useState<SyncedArrayNestedView>(EMPTY_SYNCED_ARRAY_NESTED_VIEW)\n const [syncedArraySelection, setSyncedArraySelection] =\n React.useState<SyncedArraySelection>(EMPTY_SYNCED_ARRAY_SELECTION)\n const updateSyncedArrayNestedView = React.useCallback(\n (stack: ArrayNestedView[], side: ArrayReviewSide) => {\n const activeSide = stack.length ? side : null\n\n setSyncedArrayNestedView((current) =>\n current.activeSide === activeSide &&\n areArrayNestedViewsEqual(current.stack, stack)\n ? current\n : {\n activeSide,\n stack,\n }\n )\n },\n []\n )\n const updateSyncedArraySelection = React.useCallback(\n (gridSelection: GridSelection, side: ArrayReviewSide, depth: number) => {\n const activeSide = gridSelection.current ? side : null\n\n setSyncedArraySelection((current) =>\n current.activeSide === activeSide &&\n current.depth === depth &&\n areGridSelectionsEqual(current.gridSelection, gridSelection)\n ? current\n : {\n activeSide,\n depth,\n gridSelection,\n }\n )\n },\n []\n )\n\n return (\n <div\n tabIndex={0}\n onFocusCapture={() => onFieldFocus?.(field)}\n onMouseEnter={() => onFieldFocus?.(field)}\n className={cn(\n "rounded-lg border bg-background p-3 transition-[border-color,background-color,box-shadow] focus-within:border-blue-500/50 focus-within:bg-blue-500/5 hover:border-blue-500/50 hover:bg-blue-500/5 focus-visible:ring-2 focus-visible:ring-blue-500/20 focus-visible:outline-none",\n active &&\n "border-blue-500/60 bg-blue-500/5 shadow-[0_0_0_1px_rgb(59_130_246_/_8%)]"\n )}\n >\n <div className="mb-3 flex min-h-8 items-start justify-between gap-3">\n <div className="min-w-0">\n <div className="flex min-w-0 items-center gap-2">\n <div className="min-w-0">\n <div className="truncate text-sm font-medium">\n {field.schema.title ?? field.key}\n </div>\n </div>\n <span\n className={cn(\n "size-2 shrink-0 rounded-full bg-amber-400 transition-opacity",\n !modified && "opacity-0"\n )}\n />\n </div>\n <div className="truncate text-xs text-muted-foreground">\n {field.key}\n </div>\n </div>\n <div className="flex shrink-0 items-center gap-1">\n {!readOnly && modified ? (\n <Tooltip>\n <TooltipTrigger asChild>\n <Button\n type="button"\n variant="ghost"\n size="icon-sm"\n className="text-muted-foreground"\n onClick={onUndo}\n aria-label={`Undo ${field.key}`}\n >\n <HugeiconsIcon icon={Undo02Icon} className="size-4" />\n </Button>\n </TooltipTrigger>\n <TooltipContent>Revert changes</TooltipContent>\n </Tooltip>\n ) : null}\n {!readOnly ? (\n <Tooltip>\n <TooltipTrigger asChild>\n <Button\n type="button"\n variant="ghost"\n size="icon-sm"\n className="text-muted-foreground"\n onClick={onSetNull}\n aria-label={`Set ${field.key} to null`}\n >\n <HugeiconsIcon icon={CancelCircleIcon} className="size-4" />\n </Button>\n </TooltipTrigger>\n <TooltipContent>Set to NULL</TooltipContent>\n </Tooltip>\n ) : null}\n <div className="flex h-6 items-center gap-1 rounded-md border bg-muted/50 px-1.5 text-xs text-muted-foreground">\n <HugeiconsIcon icon={Icon} className="size-3.5" />\n {field.schema.type}\n </div>\n </div>\n </div>\n {field.schema.type === "object" ? (\n <div className="rounded-md border bg-muted/25 p-2">\n <div className="mb-2 flex items-center justify-between gap-3 text-[11px] font-medium text-muted-foreground">\n <span>Properties</span>\n <span>{propertyEntries.length} fields</span>\n </div>\n <div className="space-y-2">\n {propertyEntries.length ? (\n propertyEntries.map(([propertyKey, schema]) => {\n const childField: ReviewField = {\n key: `${field.key}.${propertyKey}`,\n schema,\n actual: getObjectValue(field.actual, propertyKey),\n expected: getObjectValue(originalValue, propertyKey),\n }\n\n return (\n <HumanReviewFieldCard\n key={childField.key}\n field={childField}\n value={getObjectValue(value, propertyKey)}\n originalValue={childField.expected}\n active={childField.key === activeFieldKey}\n activeFieldKey={activeFieldKey}\n readOnly={readOnly}\n onChange={(childValue) =>\n onChange(setObjectValue(value, propertyKey, childValue))\n }\n onFieldFocus={onFieldFocus}\n onUndo={() =>\n onChange(\n setObjectValue(value, propertyKey, childField.expected)\n )\n }\n onSetNull={() =>\n onChange(setObjectValue(value, propertyKey, null))\n }\n />\n )\n })\n ) : (\n <div className="rounded-md bg-background px-2 py-1.5 text-sm text-muted-foreground">\n No properties\n </div>\n )}\n </div>\n </div>\n ) : field.schema.type === "array" ? (\n <div className="grid gap-2">\n <HumanReviewArrayValueGrid\n activeNestedSide={syncedArrayNestedView.activeSide}\n activeSelectionSide={syncedArraySelection.activeSide}\n label="Actual"\n readOnly\n schema={field.schema}\n selectionDepth={syncedArraySelection.depth}\n sharedGridSelection={syncedArraySelection.gridSelection}\n sharedNestedStack={syncedArrayNestedView.stack}\n value={field.actual}\n viewSide="actual"\n onGridSelectionChange={updateSyncedArraySelection}\n onNestedStackChange={updateSyncedArrayNestedView}\n />\n <HumanReviewArrayValueGrid\n activeNestedSide={syncedArrayNestedView.activeSide}\n activeSelectionSide={syncedArraySelection.activeSide}\n label="Expected"\n readOnly={readOnly}\n schema={field.schema}\n selectionDepth={syncedArraySelection.depth}\n sharedGridSelection={syncedArraySelection.gridSelection}\n sharedNestedStack={syncedArrayNestedView.stack}\n value={value}\n viewSide="expected"\n onChange={onChange}\n onGridSelectionChange={updateSyncedArraySelection}\n onNestedStackChange={updateSyncedArrayNestedView}\n />\n </div>\n ) : (\n <div className="grid gap-2 sm:grid-cols-2">\n <div className="rounded-md border bg-muted/30 p-2">\n {field.schema.description ? (\n <p className="mb-2 text-xs text-muted-foreground">\n {field.schema.description}\n </p>\n ) : null}\n <div className="mb-1 text-[11px] font-medium text-muted-foreground">\n Actual\n </div>\n <div className="min-h-7 rounded-md bg-background px-2 py-1.5 text-sm">\n {formatValue(field.actual)}\n </div>\n </div>\n <div className="rounded-md border bg-muted/30 p-2">\n <div className="mb-1 text-[11px] font-medium text-muted-foreground">\n Expected\n </div>\n <HumanReviewValueInput\n readOnly={readOnly}\n schema={field.schema}\n value={getPrimitiveValue(value)}\n onChange={onChange}\n />\n </div>\n </div>\n )}\n </div>\n )\n}\n\nfunction HumanReviewHighlight({ field }: { field: ReviewField }) {\n const area = field.location?.area\n\n if (!area) return null\n\n return (\n <div\n className={cn(\n "pointer-events-none absolute z-10 rounded-[3px] border",\n REVIEW_HIGHLIGHT_STYLE\n )}\n style={{\n left: `${area.left}%`,\n top: `${area.top}%`,\n width: `${area.width}%`,\n height: `${area.height}%`,\n }}\n />\n )\n}\n\nexport function JsonDiffView({\n actual,\n expected,\n theme = "light",\n}: {\n actual: JsonObject\n expected: JsonObject\n theme?: HumanReviewTheme\n}) {\n const oldFile = React.useMemo(\n () => ({\n name: "actual.json",\n contents: formatJson(actual),\n lang: "json",\n }),\n [actual]\n )\n const newFile = React.useMemo(\n () => ({\n name: "expected.json",\n contents: formatJson(expected),\n lang: "json",\n }),\n [expected]\n )\n\n return (\n <Virtualizer\n className="h-full overflow-auto rounded-b-xl bg-surface/60"\n contentClassName="min-w-full"\n >\n <div className="human-review-diff h-full text-xs">\n <MultiFileDiff\n className="block min-w-full"\n style={DIFF_VIEWER_THEME}\n oldFile={oldFile}\n newFile={newFile}\n options={{\n diffStyle: "split",\n diffIndicators: "bars",\n hunkSeparators: "line-info-basic",\n overflow: "wrap",\n themeType: theme,\n theme: {\n light: "pierre-light",\n dark: "pierre-dark",\n },\n }}\n />\n </div>\n </Virtualizer>\n )\n}\n\nexport function HumanReviewPanel({\n fields = REVIEW_FIELDS,\n activeFieldKey,\n className,\n onFieldFocus,\n theme = "light",\n}: {\n fields?: ReviewField[]\n activeFieldKey?: string\n className?: string\n onFieldFocus?: (field: ReviewField) => void\n theme?: HumanReviewTheme\n} = {}) {\n const [activeTab, setActiveTab] = React.useState("form")\n const actualValues = React.useMemo(\n () => valuesFromFields(fields, "actual"),\n [fields]\n )\n const initialExpectedValues = React.useMemo(\n () => valuesFromFields(fields, "expected"),\n [fields]\n )\n const [expected, setExpected] = React.useState<JsonObject>(\n initialExpectedValues\n )\n\n React.useEffect(() => {\n setExpected(initialExpectedValues)\n }, [initialExpectedValues])\n\n const updateValue = React.useCallback((key: string, value: JsonValue) => {\n setExpected((current) =>\n Object.is(current[key], value) ? current : { ...current, [key]: value }\n )\n }, [])\n const fieldCount = React.useMemo(() => countReviewFields(fields), [fields])\n\n return (\n <TooltipProvider delay={200}>\n <Tabs\n value={activeTab}\n onValueChange={setActiveTab}\n className={cn("flex h-[560px] flex-col gap-0 bg-background", className)}\n >\n <div className="flex min-h-12 items-center justify-between gap-3 border-b px-3">\n <TabsList className="h-8 sm:h-7">\n <TabsTrigger value="form" className="h-7 sm:h-6">\n <HugeiconsIcon icon={TextCheckIcon} className="size-4" />\n Form\n </TabsTrigger>\n <TabsTrigger value="json" className="h-7 sm:h-6">\n <HugeiconsIcon icon={SourceCodeSquareIcon} className="size-4" />\n JSON\n </TabsTrigger>\n </TabsList>\n <div className="flex h-8 items-center gap-1 rounded-md border bg-muted/40 px-2 text-xs text-muted-foreground sm:h-7">\n <HugeiconsIcon icon={FileDiffIcon} className="size-3.5" />\n {fieldCount} fields\n </div>\n </div>\n <TabsContent value="form" keepMounted className="min-h-0 flex-1">\n <ScrollArea className="h-full" scrollFade>\n <div className="space-y-3 p-3">\n {fields.map((field) => (\n <HumanReviewFieldCard\n key={field.key}\n field={field}\n value={expected[field.key] ?? null}\n originalValue={field.expected}\n active={\n field.key === activeFieldKey ||\n activeFieldKey?.startsWith(`${field.key}.`)\n }\n activeFieldKey={activeFieldKey}\n onChange={(value) => updateValue(field.key, value)}\n onFieldFocus={onFieldFocus}\n onUndo={() => updateValue(field.key, field.expected)}\n onSetNull={() => updateValue(field.key, null)}\n />\n ))}\n </div>\n </ScrollArea>\n </TabsContent>\n <TabsContent value="json" keepMounted className="min-h-0 flex-1">\n <JsonDiffView\n actual={actualValues}\n expected={expected}\n theme={theme}\n />\n </TabsContent>\n </Tabs>\n </TooltipProvider>\n )\n}\n\nexport function HumanReviewBlock({\n file,\n fields = REVIEW_FIELDS,\n className,\n theme,\n}: {\n file?: string\n fields?: ReviewField[]\n className?: string\n theme?: HumanReviewTheme\n}) {\n const [activeFieldKey, setActiveFieldKey] = React.useState(fields[0]?.key)\n const viewerRef = React.useRef<PDFViewerHandle>(null)\n const activeField = findReviewField(fields, activeFieldKey) ?? fields[0]\n\n React.useEffect(() => {\n if (activeFieldKey || !fields[0]) return\n setActiveFieldKey(fields[0].key)\n }, [activeFieldKey, fields])\n\n const focusField = React.useCallback(\n (field: ReviewField) => {\n if (field.key === activeFieldKey) return\n\n setActiveFieldKey(field.key)\n\n if (field.location) {\n viewerRef.current?.scrollToPageArea(\n field.location.page,\n field.location.area\n )\n }\n },\n [activeFieldKey]\n )\n\n return (\n <PdfBlockResizableShell\n autoSaveId="pdf-block-human-review"\n className={className}\n rightDefaultSize={42}\n rightMaxSize={60}\n rightMinSize={30}\n left={\n <PDFViewer\n ref={viewerRef}\n file={file}\n defaultZoom={DEFAULT_ZOOM}\n renderPageOverlay={({ pageNumber }) =>\n activeField?.location?.page === pageNumber ? (\n <HumanReviewHighlight field={activeField} />\n ) : null\n }\n />\n }\n right={\n <aside className="min-h-0 bg-background">\n <HumanReviewPanel\n fields={fields}\n activeFieldKey={activeField?.key}\n className="h-full min-h-0"\n theme={theme}\n onFieldFocus={focusField}\n />\n </aside>\n }\n />\n )\n}\n'
export function HumanReviewSource() {
return <HighlightedCodeBlock code={humanReviewSourceCode} />
}
diff --git a/apps/v4/components/theme-provider.tsx b/apps/v4/components/theme-provider.tsx
index ea24e06..27a68f9 100644
--- a/apps/v4/components/theme-provider.tsx
+++ b/apps/v4/components/theme-provider.tsx
@@ -1,39 +1,7 @@
"use client"
import * as React from "react"
-import { ThemeProvider as NextThemesProvider, useTheme } from "next-themes"
-
-function ThemeShortcut() {
- const { setTheme, resolvedTheme } = useTheme()
-
- React.useEffect(() => {
- const down = (e: KeyboardEvent) => {
- if (
- (e.key === "d" || e.key === "D") &&
- !e.metaKey &&
- !e.ctrlKey &&
- !e.altKey
- ) {
- if (
- (e.target instanceof HTMLElement && e.target.isContentEditable) ||
- e.target instanceof HTMLInputElement ||
- e.target instanceof HTMLTextAreaElement ||
- e.target instanceof HTMLSelectElement
- ) {
- return
- }
-
- e.preventDefault()
- setTheme(resolvedTheme === "dark" ? "light" : "dark")
- }
- }
-
- document.addEventListener("keydown", down)
- return () => document.removeEventListener("keydown", down)
- }, [resolvedTheme, setTheme])
-
- return null
-}
+import { ThemeProvider as NextThemesProvider } from "next-themes"
export function ThemeProvider({
children,
@@ -48,7 +16,6 @@ export function ThemeProvider({
enableColorScheme
{...props}
>
- <ThemeShortcut />
{children}
</NextThemesProvider>
)
diff --git a/apps/v4/content/docs/components/csv-tsv-viewer.mdx b/apps/v4/content/docs/components/csv-tsv-viewer.mdx
index 1e0e5d3..7a375a1 100644
--- a/apps/v4/content/docs/components/csv-tsv-viewer.mdx
+++ b/apps/v4/content/docs/components/csv-tsv-viewer.mdx
@@ -26,7 +26,7 @@ the last child of your app's `<body>` before using the component:
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/csv-viewer.json
+npx shadcn@latest add https://ui.extend.ai/r/csv-viewer.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/document-splits.mdx b/apps/v4/content/docs/components/document-splits.mdx
index 73479a5..1673c33 100644
--- a/apps/v4/content/docs/components/document-splits.mdx
+++ b/apps/v4/content/docs/components/document-splits.mdx
@@ -21,7 +21,7 @@ See it composed with the PDF Viewer in the [Document Splits block](/blocks#docum
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/document-splits.json
+npx shadcn@latest add https://ui.extend.ai/r/document-splits.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/docx-editor.mdx b/apps/v4/content/docs/components/docx-editor.mdx
index c771b3a..26dd488 100644
--- a/apps/v4/content/docs/components/docx-editor.mdx
+++ b/apps/v4/content/docs/components/docx-editor.mdx
@@ -20,7 +20,7 @@ The DOCX Editor is an experimental document editing surface built on top of `@ex
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/docx-editor.json
+npx shadcn@latest add https://ui.extend.ai/r/docx-editor.json
```
</TabsContent>
@@ -41,7 +41,7 @@ npm install @extend-ai/react-docx
</Step>
```bash
-npx shadcn@latest add http://localhost:4000/r/file-thumbnail.json
+npx shadcn@latest add https://ui.extend.ai/r/file-thumbnail.json
```
<Step>Copy and paste the following code into your project.</Step>
diff --git a/apps/v4/content/docs/components/docx-viewer.mdx b/apps/v4/content/docs/components/docx-viewer.mdx
index 738f6e0..f35737a 100644
--- a/apps/v4/content/docs/components/docx-viewer.mdx
+++ b/apps/v4/content/docs/components/docx-viewer.mdx
@@ -20,7 +20,7 @@ The DOCX Viewer is for contracts, proposals, reports, and internal docs where vi
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/docx-viewer.json
+npx shadcn@latest add https://ui.extend.ai/r/docx-viewer.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/e-signature.mdx b/apps/v4/content/docs/components/e-signature.mdx
index 0000f8f..02a683f 100644
--- a/apps/v4/content/docs/components/e-signature.mdx
+++ b/apps/v4/content/docs/components/e-signature.mdx
@@ -21,7 +21,7 @@ See it composed with the PDF Viewer in the [E-Signature block](/blocks#e-signatu
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/e-signature.json
+npx shadcn@latest add https://ui.extend.ai/r/e-signature.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/file-thumbnail.mdx b/apps/v4/content/docs/components/file-thumbnail.mdx
index 641a1f4..fc0c746 100644
--- a/apps/v4/content/docs/components/file-thumbnail.mdx
+++ b/apps/v4/content/docs/components/file-thumbnail.mdx
@@ -19,7 +19,7 @@ File Thumbnail gives document lists, upload queues, and attachment trays a consi
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/file-thumbnail.json
+npx shadcn@latest add https://ui.extend.ai/r/file-thumbnail.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/file-upload.mdx b/apps/v4/content/docs/components/file-upload.mdx
index 81cfcc7..203fb09 100644
--- a/apps/v4/content/docs/components/file-upload.mdx
+++ b/apps/v4/content/docs/components/file-upload.mdx
@@ -20,7 +20,7 @@ File Upload is the starting point for document processing workflows. It should h
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/file-upload.json
+npx shadcn@latest add https://ui.extend.ai/r/file-upload.json
```
</TabsContent>
@@ -41,7 +41,7 @@ npm install border-beam
</Step>
```bash
-npx shadcn@latest add http://localhost:4000/r/file-thumbnail.json
+npx shadcn@latest add https://ui.extend.ai/r/file-thumbnail.json
```
<Step>Copy and paste the following code into your project.</Step>
diff --git a/apps/v4/content/docs/components/human-review.mdx b/apps/v4/content/docs/components/human-review.mdx
index 623a314..f0b5c0d 100644
--- a/apps/v4/content/docs/components/human-review.mdx
+++ b/apps/v4/content/docs/components/human-review.mdx
@@ -28,7 +28,7 @@ the last child of your app's `<body>` before using the component:
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/human-review.json
+npx shadcn@latest add https://ui.extend.ai/r/human-review.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/ocr-blocks.mdx b/apps/v4/content/docs/components/ocr-blocks.mdx
index 9b353e7..2398aae 100644
--- a/apps/v4/content/docs/components/ocr-blocks.mdx
+++ b/apps/v4/content/docs/components/ocr-blocks.mdx
@@ -21,7 +21,7 @@ See it composed with the PDF Viewer in the [OCR Blocks block](/blocks#ocr-blocks
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/ocr-blocks.json
+npx shadcn@latest add https://ui.extend.ai/r/ocr-blocks.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/pdf-viewer.mdx b/apps/v4/content/docs/components/pdf-viewer.mdx
index 9a062aa..3fa29bf 100644
--- a/apps/v4/content/docs/components/pdf-viewer.mdx
+++ b/apps/v4/content/docs/components/pdf-viewer.mdx
@@ -20,7 +20,7 @@ The PDF Viewer is the core page-based document surface. It should support dense
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/pdf-viewer.json
+npx shadcn@latest add https://ui.extend.ai/r/pdf-viewer.json
```
</TabsContent>
diff --git a/apps/v4/content/docs/components/xlsx-editor.mdx b/apps/v4/content/docs/components/xlsx-editor.mdx
index 8192a0d..4946f3a 100644
--- a/apps/v4/content/docs/components/xlsx-editor.mdx
+++ b/apps/v4/content/docs/components/xlsx-editor.mdx
@@ -20,7 +20,7 @@ The Excel Editor is an experimental spreadsheet editing surface built on top of
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/xlsx-editor.json
+npx shadcn@latest add https://ui.extend.ai/r/xlsx-editor.json
```
</TabsContent>
@@ -41,7 +41,7 @@ npm install @extend-ai/react-xlsx
</Step>
```bash
-npx shadcn@latest add http://localhost:4000/r/xlsx-viewer.json http://localhost:4000/r/group.json
+npx shadcn@latest add https://ui.extend.ai/r/xlsx-viewer.json https://ui.extend.ai/r/group.json
```
<Step>Copy and paste the following code into your project.</Step>
diff --git a/apps/v4/content/docs/components/xlsx-viewer.mdx b/apps/v4/content/docs/components/xlsx-viewer.mdx
index 2f18c85..da4e4b6 100644
--- a/apps/v4/content/docs/components/xlsx-viewer.mdx
+++ b/apps/v4/content/docs/components/xlsx-viewer.mdx
@@ -20,7 +20,7 @@ The Excel Viewer is the core spreadsheet preview for financial models, imports,
<TabsContent value="cli">
```bash
-npx shadcn@latest add http://localhost:4000/r/xlsx-viewer.json
+npx shadcn@latest add https://ui.extend.ai/r/xlsx-viewer.json
```
</TabsContent>
diff --git a/apps/v4/lib/config.ts b/apps/v4/lib/config.ts
index 3a5d366..70eec88 100644
--- a/apps/v4/lib/config.ts
+++ b/apps/v4/lib/config.ts
@@ -1,7 +1,9 @@
+const appUrl = process.env.NEXT_PUBLIC_APP_URL ?? "https://ui.extend.ai"
+
export const siteConfig = {
name: "Extend UI",
- url: "http://localhost:4000",
- ogImage: "http://localhost:4000/opengraph-image.png",
+ url: appUrl,
+ ogImage: `${appUrl}/opengraph-image.png`,
description:
"Open source UI primitives for building document processing products with viewers, review surfaces, and validation workflows.",
links: {
diff --git a/apps/v4/lib/utils.ts b/apps/v4/lib/utils.ts
index ffbf9e9..ecdc38e 100644
--- a/apps/v4/lib/utils.ts
+++ b/apps/v4/lib/utils.ts
@@ -1,10 +1,12 @@
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
+import { siteConfig } from "@/lib/config"
+
export function cn(...inputs: ClassValue[]): string {
return twMerge(clsx(inputs))
}
export function absoluteUrl(path: string): string {
- return `${process.env.NEXT_PUBLIC_APP_URL || "http://localhost:4000"}${path}`
+ return `${siteConfig.url}${path}`
}
diff --git a/apps/v4/registry/new-york-v4/blocks/human-review.tsx b/apps/v4/registry/new-york-v4/blocks/human-review.tsx
index 9f35986..3e40bdf 100644
--- a/apps/v4/registry/new-york-v4/blocks/human-review.tsx
+++ b/apps/v4/registry/new-york-v4/blocks/human-review.tsx
@@ -5,13 +5,15 @@ import {
DataEditor,
emptyGridSelection,
GridCellKind,
- type DrawCellCallback,
type EditableGridCell,
type GridCell,
type GridColumn,
type GridSelection,
type Item,
+ type NumberCell,
+ type ProvideEditorComponent,
type Rectangle,
+ type TextCell,
type Theme,
} from "@glideapps/glide-data-grid"
import {
@@ -555,6 +557,202 @@ function areGridSelectionsEqual(left: GridSelection, right: GridSelection) {
)
}
+function getGridSelectionRanges(selection: GridSelection | undefined) {
+ const current = selection?.current
+ if (!current) return []
+
+ return [...(current.rangeStack ?? []), current.range]
+}
+
+function areArrayNestedViewsEqual(
+ left: ArrayNestedView[],
+ right: ArrayNestedView[]
+) {
+ if (left === right) return true
+ if (left.length !== right.length) return false
+
+ return left.every((view, index) => {
+ const other = right[index]
+
+ return (
+ view.rowIndex === other?.rowIndex &&
+ view.columnId === other.columnId &&
+ view.title === other.title &&
+ view.schema === other.schema &&
+ Object.is(view.value, other.value)
+ )
+ })
+}
+
+type HumanReviewOverlayCell = TextCell | NumberCell
+
+type HumanReviewTextOverlayEditorProps = React.ComponentProps<
+ ProvideEditorComponent<HumanReviewOverlayCell>
+> & {
+ overlayOpenRef: React.RefObject<boolean>
+ readOnly?: boolean
+}
+
+function HumanReviewTextOverlayEditor({
+ isHighlighted,
+ onFinishedEditing,
+ overlayOpenRef,
+ readOnly = false,
+ validatedSelection,
+ value,
+}: HumanReviewTextOverlayEditorProps) {
+ const inputRef = React.useRef<HTMLTextAreaElement | null>(null)
+ const initialValue =
+ value.kind === GridCellKind.Number ? value.displayData : value.data
+ const latestValueRef = React.useRef(initialValue)
+ const finishedRef = React.useRef(false)
+
+ const finishEditing = React.useCallback(
+ (
+ shouldSave: boolean,
+ movement: readonly [-1 | 0 | 1, -1 | 0 | 1] = [0, 0]
+ ) => {
+ if (finishedRef.current) return
+
+ finishedRef.current = true
+ overlayOpenRef.current = false
+
+ if (!shouldSave || readOnly) {
+ onFinishedEditing(undefined, movement)
+ return
+ }
+
+ if (value.kind === GridCellKind.Number) {
+ const numericValue = Number(latestValueRef.current)
+
+ onFinishedEditing(
+ {
+ ...value,
+ data: Number.isFinite(numericValue) ? numericValue : value.data,
+ displayData: latestValueRef.current,
+ },
+ movement
+ )
+ return
+ }
+
+ onFinishedEditing(
+ {
+ ...value,
+ data: latestValueRef.current,
+ displayData: latestValueRef.current,
+ },
+ movement
+ )
+ },
+ [onFinishedEditing, overlayOpenRef, readOnly, value]
+ )
+
+ React.useEffect(() => {
+ overlayOpenRef.current = true
+
+ const input = inputRef.current
+ if (!input) {
+ return () => {
+ overlayOpenRef.current = false
+ }
+ }
+
+ const handleInput = (event: Event) => {
+ event.stopPropagation()
+ latestValueRef.current = input.value
+ }
+ const stopTextInputPropagation = (event: Event) => {
+ event.stopPropagation()
+ }
+ const handleKeyDown = (event: KeyboardEvent) => {
+ event.stopPropagation()
+
+ if (event.key === "Escape") {
+ event.preventDefault()
+ finishEditing(false)
+ return
+ }
+
+ if (event.key === "Tab") {
+ event.preventDefault()
+ finishEditing(true, [event.shiftKey ? -1 : 1, 0])
+ return
+ }
+
+ if (event.key === "Enter" && !event.shiftKey) {
+ event.preventDefault()
+ finishEditing(true, [0, 1])
+ }
+ }
+
+ input.addEventListener("input", handleInput)
+ input.addEventListener("beforeinput", stopTextInputPropagation, true)
+ input.addEventListener("keydown", handleKeyDown, true)
+
+ if (!readOnly) {
+ input.focus()
+
+ if (validatedSelection !== undefined) {
+ const selectionRange =
+ typeof validatedSelection === "number"
+ ? [validatedSelection, validatedSelection]
+ : validatedSelection
+ input.setSelectionRange(selectionRange[0], selectionRange[1])
+ } else if (isHighlighted) {
+ input.setSelectionRange(0, input.value.length)
+ } else {
+ input.setSelectionRange(input.value.length, input.value.length)
+ }
+ }
+
+ return () => {
+ input.removeEventListener("input", handleInput)
+ input.removeEventListener("beforeinput", stopTextInputPropagation, true)
+ input.removeEventListener("keydown", handleKeyDown, true)
+ overlayOpenRef.current = false
+ }
+ }, [
+ finishEditing,
+ isHighlighted,
+ overlayOpenRef,
+ validatedSelection,
+ readOnly,
+ ])
+
+ React.useEffect(() => {
+ const handlePointerOutside = (event: PointerEvent | MouseEvent) => {
+ const input = inputRef.current
+ const overlayRoot = input?.closest(".gdg-clip-region")
+
+ if (!overlayRoot || overlayRoot.contains(event.target as Node | null)) {
+ return
+ }
+
+ finishEditing(true)
+ }
+
+ document.addEventListener("pointerdown", handlePointerOutside, true)
+ document.addEventListener("contextmenu", handlePointerOutside, true)
+
+ return () => {
+ document.removeEventListener("pointerdown", handlePointerOutside, true)
+ document.removeEventListener("contextmenu", handlePointerOutside, true)
+ }
+ }, [finishEditing])
+
+ return (
+ <textarea
+ ref={inputRef}
+ className="gdg-input"
+ defaultValue={initialValue}
+ disabled={readOnly}
+ dir="auto"
+ style={{ height: "100%", minHeight: 32, resize: "none", width: "100%" }}
+ />
+ )
+}
+
function getFieldIcon(type: SchemaPropertyType) {
if (type === "number" || type === "integer") return InputNumericIcon
if (type === "boolean") return TextCheckIcon
@@ -814,24 +1012,6 @@ function getNestedArrayValue({
})
}
-function isCellInRange(col: number, row: number, range: Rectangle) {
- return (
- col >= range.x &&
- col < range.x + range.width &&
- row >= range.y &&
- row < range.y + range.height
- )
-}
-
-function getRangeEdges(col: number, row: number, range: Rectangle) {
- return {
- bottom: row === range.y + range.height - 1,
- left: col === range.x,
- right: col === range.x + range.width - 1,
- top: row === range.y,
- }
-}
-
function HumanReviewArrayValueGrid({
activeNestedSide = null,
activeSelectionSide = null,
@@ -879,6 +1059,7 @@ function HumanReviewArrayValueGrid({
>([])
const [localGridSelection, setLocalGridSelection] =
React.useState<GridSelection>(emptyGridSelection)
+ const fastTextOverlayOpenRef = React.useRef(false)
const nestedStack = sharedNestedStack ?? localNestedStack
const syncedGridSelection = sharedGridSelection ?? localGridSelection
const gridSelection =
@@ -887,28 +1068,6 @@ function HumanReviewArrayValueGrid({
selectionDepth !== nestedStackBaseDepth)
? emptyGridSelection
: syncedGridSelection
- const sharedSelectionRange = sharedGridSelection?.current?.range
- const sharedSelectionRangeStack = sharedGridSelection?.current?.rangeStack
- const mirroredSelectionRanges = React.useMemo<readonly Rectangle[]>(() => {
- if (
- !activeSelectionSide ||
- activeSelectionSide === viewSide ||
- selectionDepth !== nestedStackBaseDepth ||
- !sharedSelectionRange ||
- !sharedSelectionRangeStack
- ) {
- return []
- }
-
- return [...sharedSelectionRangeStack, sharedSelectionRange]
- }, [
- activeSelectionSide,
- nestedStackBaseDepth,
- selectionDepth,
- sharedSelectionRange,
- sharedSelectionRangeStack,
- viewSide,
- ])
const visibleNestedStack = nestedStack.slice(nestedStackBaseDepth)
const activeNestedView = visibleNestedStack[0] ?? null
const activeNestedValue = activeNestedView
@@ -931,7 +1090,33 @@ function HumanReviewArrayValueGrid({
const mirroredCellBackground = isDark
? "rgba(167, 139, 250, 0.18)"
: "rgba(237, 233, 254, 0.9)"
- const mirroredCellBorder = isDark ? "rgb(196, 181, 253)" : "rgb(124, 58, 237)"
+ const mirroredHighlightRegions = React.useMemo<
+ React.ComponentProps<typeof DataEditor>["highlightRegions"]
+ >(() => {
+ if (
+ !activeSelectionSide ||
+ activeSelectionSide === viewSide ||
+ selectionDepth !== nestedStackBaseDepth
+ ) {
+ return undefined
+ }
+
+ const ranges = getGridSelectionRanges(sharedGridSelection)
+ if (!ranges.length) return undefined
+
+ return ranges.map((range) => ({
+ color: mirroredCellBackground,
+ range,
+ style: "dashed" as const,
+ }))
+ }, [
+ activeSelectionSide,
+ mirroredCellBackground,
+ nestedStackBaseDepth,
+ selectionDepth,
+ sharedGridSelection,
+ viewSide,
+ ])
const setNestedStack = React.useCallback(
(
@@ -955,6 +1140,13 @@ function HumanReviewArrayValueGrid({
const handleGridSelectionChange = React.useCallback(
(selection: GridSelection) => {
if (onGridSelectionChange) {
+ if (
+ sharedGridSelection &&
+ areGridSelectionsEqual(sharedGridSelection, selection)
+ ) {
+ return
+ }
+
onGridSelectionChange(selection, viewSide, nestedStackBaseDepth)
return
}
@@ -963,7 +1155,7 @@ function HumanReviewArrayValueGrid({
areGridSelectionsEqual(current, selection) ? current : selection
)
},
- [nestedStackBaseDepth, onGridSelectionChange, viewSide]
+ [nestedStackBaseDepth, onGridSelectionChange, sharedGridSelection, viewSide]
)
const columns = React.useMemo<GridColumn[]>(() => {
@@ -1002,9 +1194,6 @@ function HumanReviewArrayValueGrid({
const matchedNestedCell =
activeNestedView?.rowIndex === row &&
activeNestedView.columnId === columnId
- const matchesMirroredSelection = mirroredSelectionRanges.some((range) =>
- isCellInRange(col, row, range)
- )
const nestedCellTheme: Partial<Theme> | undefined = matchedNestedCell
? {
bgCell: isMirroredNestedTarget
@@ -1013,13 +1202,6 @@ function HumanReviewArrayValueGrid({
textDark: blueCellText,
}
: undefined
- const selectionCellTheme: Partial<Theme> | undefined =
- matchesMirroredSelection
- ? {
- bgCell: mirroredCellBackground,
- textDark: isDark ? "rgb(233, 213, 255)" : "rgb(91, 33, 182)",
- }
- : undefined
if (isComplexSchema(cellSchema)) {
return {
@@ -1032,7 +1214,6 @@ function HumanReviewArrayValueGrid({
activationBehaviorOverride: "double-click",
themeOverride: {
textDark: blueCellText,
- ...(selectionCellTheme ?? {}),
...(nestedCellTheme ?? {}),
},
}
@@ -1044,7 +1225,6 @@ function HumanReviewArrayValueGrid({
data: typeof cellValue === "boolean" ? cellValue : false,
allowOverlay: false,
readonly: readOnly,
- ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),
}
}
@@ -1054,8 +1234,7 @@ function HumanReviewArrayValueGrid({
data: typeof cellValue === "number" ? cellValue : undefined,
displayData: typeof cellValue === "number" ? String(cellValue) : "",
allowOverlay: true,
- readonly: readOnly,
- ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),
+ readonly: false,
}
}
@@ -1074,8 +1253,7 @@ function HumanReviewArrayValueGrid({
? ""
: String(cellValue),
allowOverlay: true,
- readonly: readOnly,
- ...(selectionCellTheme ? { themeOverride: selectionCellTheme } : {}),
+ readonly: false,
}
},
[
@@ -1083,10 +1261,8 @@ function HumanReviewArrayValueGrid({
blueCellText,
columns,
isMirroredNestedTarget,
- isDark,
itemSchema,
mirroredCellBackground,
- mirroredSelectionRanges,
readOnly,
rows,
selectedCellBackground,
@@ -1114,58 +1290,43 @@ function HumanReviewArrayValueGrid({
},
[columns, itemSchema, onChange, readOnly, rows, value]
)
-
- const drawCell = React.useCallback<DrawCellCallback>(
- (args, drawContent) => {
- drawContent()
-
- const selectionRange = mirroredSelectionRanges.find((range) =>
- isCellInRange(args.col, args.row, range)
- )
-
- if (!selectionRange) {
- return
- }
-
- const { ctx, rect } = args
- const edges = getRangeEdges(args.col, args.row, selectionRange)
-
- ctx.save()
- ctx.strokeStyle = mirroredCellBorder
- ctx.lineWidth = 1.5
- ctx.setLineDash([4, 3])
-
- if (edges.top) {
- ctx.beginPath()
- ctx.moveTo(rect.x + 1.5, rect.y + 1.5)
- ctx.lineTo(rect.x + rect.width - 1.5, rect.y + 1.5)
- ctx.stroke()
- }
-
- if (edges.right) {
- ctx.beginPath()
- ctx.moveTo(rect.x + rect.width - 1.5, rect.y + 1.5)
- ctx.lineTo(rect.x + rect.width - 1.5, rect.y + rect.height - 1.5)
- ctx.stroke()
+ const provideEditor = React.useCallback<
+ NonNullable<React.ComponentProps<typeof DataEditor>["provideEditor"]>
+ >(
+ (cell) => {
+ if (
+ cell.kind !== GridCellKind.Text &&
+ cell.kind !== GridCellKind.Number
+ ) {
+ return undefined
}
- if (edges.bottom) {
- ctx.beginPath()
- ctx.moveTo(rect.x + 1.5, rect.y + rect.height - 1.5)
- ctx.lineTo(rect.x + rect.width - 1.5, rect.y + rect.height - 1.5)
- ctx.stroke()
- }
+ return {
+ editor: (props) => {
+ if (
+ props.value.kind !== GridCellKind.Text &&
+ props.value.kind !== GridCellKind.Number
+ ) {
+ return null
+ }
- if (edges.left) {
- ctx.beginPath()
- ctx.moveTo(rect.x + 1.5, rect.y + 1.5)
- ctx.lineTo(rect.x + 1.5, rect.y + rect.height - 1.5)
- ctx.stroke()
+ return (
+ <HumanReviewTextOverlayEditor
+ {...(props as React.ComponentProps<
+ ProvideEditorComponent<HumanReviewOverlayCell>
+ >)}
+ overlayOpenRef={fastTextOverlayOpenRef}
+ readOnly={readOnly}
+ />
+ )
+ },
}
-
- ctx.restore()
},
- [mirroredCellBorder, mirroredSelectionRanges]
+ [readOnly]
+ )
+ const handleOutsideClick = React.useCallback(
+ () => !fastTextOverlayOpenRef.current,
+ []
)
const openNestedCell = React.useCallback(
@@ -1251,11 +1412,13 @@ function HumanReviewArrayValueGrid({
rows={rows.length}
getCellContent={getCellContent}
cellActivationBehavior="double-click"
- drawCell={drawCell}
gridSelection={gridSelection}
+ highlightRegions={mirroredHighlightRegions}
onCellEdited={updateCellValue}
onCellActivated={openNestedCell}
onGridSelectionChange={handleGridSelectionChange}
+ provideEditor={provideEditor}
+ isOutsideClick={handleOutsideClick}
rowMarkers="number"
smoothScrollX
smoothScrollY
@@ -1441,10 +1604,17 @@ function HumanReviewFieldCardBase({
React.useState<SyncedArraySelection>(EMPTY_SYNCED_ARRAY_SELECTION)
const updateSyncedArrayNestedView = React.useCallback(
(stack: ArrayNestedView[], side: ArrayReviewSide) => {
- setSyncedArrayNestedView({
- activeSide: stack.length ? side : null,
- stack,
- })
+ const activeSide = stack.length ? side : null
+
+ setSyncedArrayNestedView((current) =>
+ current.activeSide === activeSide &&
+ areArrayNestedViewsEqual(current.stack, stack)
+ ? current
+ : {
+ activeSide,
+ stack,
+ }
+ )
},
[]
)