Files
supabase/apps/studio/components/interfaces/Database/Schemas/DefaultEdge.tsx
Alaister Young 7e9badc6b8 chore(studio): migrate useStaticEffectEvent to React 19 useEffectEvent (#46415)
Studio is on `react@^19.2.6`, and `useEffectEvent` shipped stable in
React 19.2 with the same signature as the userland polyfill. This drops
the local hook in `apps/studio` and `apps/www` in favor of the built-in.

**Removed:**
- `apps/studio/hooks/useStaticEffectEvent.ts`
- `apps/www/hooks/useStaticEffectEvent.ts`
- `.claude/skills/use-static-effect-event/` — skill is obsolete

**Changed:**
- 26 call sites: dropped the `useStaticEffectEvent` import, added
`useEffectEvent` to the existing `react` import, renamed call sites
- `.claude/CLAUDE.md`: `apps/studio` row updated React 18 → React 19
- `.claude/skills/vercel-composition-patterns/SKILL.md`: removed stale
"Studio uses React 18, skip these patterns" warning

## To test

- `pnpm typecheck --filter=studio` — passes locally
- `pnpm typecheck --filter=www` — passes locally
- `grep -rn "useStaticEffectEvent"` returns nothing outside
`node_modules`
- Smoke-test areas that use the hook: schema visualizer edges
(intersection check), spreadsheet import, sign-in/CLI login flows, side
panels with unsaved-changes prompts

**Out of scope:** pre-existing Tailwind lint warning on
`DefaultEdge.tsx:141` (`outline` + `outline-1` conflict) — unrelated to
this migration

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Refactor**
* Internal event handling migrated to React’s built-in event hooks
across the Studio app; no user-facing changes.

* **Documentation**
* Clarified React 19 compatibility and noted Studio now targets React
19.
  * Removed obsolete documentation for a deprecated internal hook.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46415?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-05-28 23:30:42 +08:00

206 lines
5.6 KiB
TypeScript

import {
BaseEdge,
Edge,
EdgeLabelRenderer,
EdgeProps,
getSmoothStepPath,
Position,
useReactFlow,
} from '@xyflow/react'
import { ArrowLeft, ArrowRight } from 'lucide-react'
import { memo, useCallback, useState } from 'react'
import { Badge, cn } from 'ui'
import { useSchemaGraphContext } from './SchemaGraphContext'
import { EdgeData } from './Schemas.constants'
import { useQuerySchemaState } from '@/hooks/misc/useSchemaQueryState'
const DefaultEdgeComponent = ({
id,
animated,
data,
deletable,
selectable,
source,
sourceX,
sourceY,
sourceHandleId,
sourcePosition = Position.Bottom,
target,
targetX,
targetY,
targetHandleId,
targetPosition = Position.Top,
selected,
pathOptions,
...props
}: EdgeProps<Edge<EdgeData>>) => {
const { isDownloading } = useSchemaGraphContext()
const [edgePath, labelX, labelY] = getSmoothStepPath({
sourceX,
sourceY,
targetX,
targetY,
sourcePosition,
targetPosition,
borderRadius: pathOptions?.borderRadius,
offset: pathOptions?.offset,
stepPosition: pathOptions?.stepPosition,
})
return (
<>
<BaseEdge
id={id}
path={edgePath}
className={cn(selected ? 'stroke-brand!' : isDownloading ? 'stroke-black!' : undefined)}
stroke="#000000"
{...props}
/>
{data && selected ? (
<EdgeRelationInfo
source={source}
target={target}
edgePath={edgePath}
labelX={labelX}
labelY={labelY}
sourceX={sourceX}
targetX={targetX}
data={data}
/>
) : null}
</>
)
}
export const DefaultEdge = memo(DefaultEdgeComponent)
const EdgeRelationInfo = ({
data,
source,
target,
labelX,
labelY,
targetX,
sourceX,
}: {
data: EdgeData
edgePath: string
source: string
target: string
labelX: number
labelY: number
sourceX: number
targetX: number
}) => {
const [show, setShow] = useState(false)
const reactFlowInstance = useReactFlow()
const checkIfShouldBeDisplayed = useCallback(
(relationInfoElement: HTMLDivElement | null) => {
if (!relationInfoElement) return
const sourceNode = reactFlowInstance.getNode(source)
const targetNode = reactFlowInstance.getNode(target)
if (!sourceNode || !targetNode) return
const relationInfoRect = relationInfoElement.getBoundingClientRect()
// Get the origin position of the relation information badge in the ReactFlow coordinates
const relationInfoOriginPositionInReactFlow = reactFlowInstance.screenToFlowPosition({
x: relationInfoRect.x,
y: relationInfoRect.y,
})
// Get the end position (origin + dimensions) of the relation information badge in the ReactFlow coordinates
const relationInfoTargetPositionInReactFlow = reactFlowInstance.screenToFlowPosition({
x: relationInfoRect.x + relationInfoRect.width,
y: relationInfoRect.y + relationInfoRect.height,
})
// Create a ReactFlow Rect from the computed position above
const relationInfoReactFlowRect = {
x: relationInfoOriginPositionInReactFlow.x,
y: relationInfoOriginPositionInReactFlow.y,
width: relationInfoTargetPositionInReactFlow.x - relationInfoOriginPositionInReactFlow.x,
height: relationInfoTargetPositionInReactFlow.y - relationInfoOriginPositionInReactFlow.y,
}
// Check whether the relation information badge is intersecting with either the source or target node
const isNodeIntersectingWithSource = reactFlowInstance.isNodeIntersecting(
sourceNode,
relationInfoReactFlowRect
)
const isNodeIntersectingWithTarget = reactFlowInstance.isNodeIntersecting(
targetNode,
relationInfoReactFlowRect
)
// If it is, hide it as they are too close
setShow(!isNodeIntersectingWithSource && !isNodeIntersectingWithTarget)
},
[reactFlowInstance, source, target]
)
return (
<EdgeLabelRenderer>
<Badge
ref={checkIfShouldBeDisplayed}
className={cn(
'absolute pointer-events-auto z-50 p-1 rounded-[4px] gap-1 outline outline-1 outline-brand',
show ? 'opacity-100' : 'opacity-0'
)}
style={{
transform: `translate(-50%, -50%) translate(${labelX}px, ${labelY}px)`,
}}
>
{
// Show the columns in the order of the schema instead of the Postgre relation order
sourceX < targetX ? (
<>
<EdgeNodeData
schema={data.sourceSchemaName}
table={data.sourceName}
column={data.sourceColumnName}
/>
<ArrowRight size={12} />
<EdgeNodeData
schema={data.targetSchemaName}
table={data.targetName}
column={data.targetColumnName}
/>
</>
) : (
<>
<EdgeNodeData
schema={data.targetSchemaName}
table={data.targetName}
column={data.targetColumnName}
/>
<ArrowLeft size={12} />
<EdgeNodeData
schema={data.sourceSchemaName}
table={data.sourceName}
column={data.sourceColumnName}
/>
</>
)
}
</Badge>
</EdgeLabelRenderer>
)
}
const EdgeNodeData = ({
schema,
table,
column,
}: {
schema: string
table: string
column: string
}) => {
const { selectedSchema } = useQuerySchemaState()
return (
<Badge className="normal-case text-[8px]">
{selectedSchema === schema ? '' : `${schema}.`}
{table}.{column}
</Badge>
)
}