Files
supabase/apps/ui-library/registry/default/blocks/realtime-flow/components/realtime-flow.tsx
Tiago Antunes b79a64e301 feat: add Realtime Flow component (#44273)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Feature, docs update

## What is the new behavior?

This PR introduces a new `RealtimeFlow` component and hook to the UI
library for building collaborative React Flow with Supabase Realtime:
- keeps nodes and edges in sync across multiple connected clients in
real time
- uses Yjs with `@supabase-labs/y-supabase` to propagate flow updates
- supports optional persistence, so a flow can be restored from
previously saved shared state

## Additional context


https://github.com/user-attachments/assets/90d3a381-6f9c-427f-a493-5d91c2141462



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

* **New Features**
* Collaborative "Realtime Flow" diagram editor with syncing overlays and
a dual-view demo component
* Interactive demo page and registry example for live editing
(add/remove/rename nodes)
* Framework-ready registry packages for Next.js, React, React Router,
and TanStack

* **Documentation**
* Comprehensive docs added for Next.js, React, React Router, and
TanStack (usage, persistence, hook API)

* **Chores**
  * Added runtime dependency for the flow component package

[![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/44273)
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-26 13:28:52 +03:00

80 lines
1.9 KiB
TypeScript

'use client'
import {
ReactFlow,
ReactFlowProvider,
Background,
Controls,
type Node,
type Edge,
type NodeTypes,
type EdgeTypes,
} from '@xyflow/react'
import '@xyflow/react/dist/style.css'
import type { SupabasePersistenceOptions } from '@supabase-labs/y-supabase'
import { RealtimeFlowOverlay } from './realtime-flow-overlay'
import { useRealtimeFlow } from '../hooks/use-realtime-flow'
type RealtimeFlowProps = {
channel: string
className?: string
style?: React.CSSProperties
persistence?: boolean | SupabasePersistenceOptions
initialNodes?: Node[]
initialEdges?: Edge[]
nodeTypes?: NodeTypes
edgeTypes?: EdgeTypes
height?: string | number
}
const DEFAULT_HEIGHT = 550
const RealtimeFlowContent = ({
channel,
className,
style,
persistence,
initialNodes,
initialEdges,
nodeTypes,
edgeTypes,
height = DEFAULT_HEIGHT,
}: RealtimeFlowProps) => {
const { nodes, edges, synced, syncError, onNodesChange, onEdgesChange, onConnect } =
useRealtimeFlow({
channel,
persistence,
initialNodes,
initialEdges,
})
return (
<div style={{ height, position: 'relative', ...style }} className={className}>
<ReactFlow
nodes={synced ? nodes : []}
edges={synced ? edges : []}
onNodesChange={synced ? onNodesChange : undefined}
onEdgesChange={synced ? onEdgesChange : undefined}
onConnect={synced ? onConnect : undefined}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
fitView
>
<Background />
<Controls />
</ReactFlow>
{!synced && !syncError && <RealtimeFlowOverlay status="syncing" />}
{!synced && syncError && <RealtimeFlowOverlay status="error" message={syncError} />}
</div>
)
}
const RealtimeFlow = (props: RealtimeFlowProps) => (
<ReactFlowProvider>
<RealtimeFlowContent {...props} />
</ReactFlowProvider>
)
export { RealtimeFlow }