import { computed, Default, handler, ifElse, lift, NAME, navigateTo, pattern, toSchema, UI, Writable, } from "commonfabric"; type Piece = { [NAME]: string; [UI]: string; [key: string]: any; }; // Define interfaces for type safety interface AddPieceState { piece: any; cellRef: Writable; isInitialized: Writable; } const AddPieceSchema = toSchema(); // Simple piece that will be instantiated multiple times const SimplePiece = pattern<{ id: string }>(({ id }) => ({ [NAME]: computed(() => `SimplePiece: ${id}`), [UI]:
Simple Piece id {id}
, })); // Lift that adds a piece to the array and navigates to it. // The isInitialized flag prevents duplicate additions: // - Without it: lift runs → adds to array → array changes → lift runs again → duplicate // - With it: lift runs once → sets isInitialized → subsequent runs skip const addPieceAndNavigate = lift( AddPieceSchema, undefined, ({ piece, cellRef, isInitialized }) => { if (!isInitialized.get()) { if (cellRef) { cellRef.push(piece); isInitialized.set(true); return navigateTo(piece); } else { console.log("addPieceAndNavigate undefined cellRef"); } } return undefined; }, ); // Handler that creates a new piece instance and adds it to the array. // Each invocation creates its own isInitialized cell for tracking. const createSimplePiece = handler }>( (_, { cellRef }) => { // Create isInitialized cell for this piece addition const isInitialized = Writable.of(false); // Create a random 5-digit ID const randomId = Math.floor(10000 + Math.random() * 90000).toString(); // Create the piece with unique ID const piece = SimplePiece({ id: randomId }); // Store the piece in the array and navigate return addPieceAndNavigate({ piece, cellRef, isInitialized }); }, ); // Handler to navigate to a specific piece from the list const goToPiece = handler( (_, { piece }) => { console.log("goToPiece clicked"); return navigateTo(piece); }, ); // Pattern input/output type type PatternInOutput = { cellRef: Piece[] | Default<[]>; }; // Main pattern that manages an array of piece references export default pattern( ({ cellRef }) => { return { [NAME]: "Pieces Launcher", [UI]: (

Stored Pieces:

{ifElse( !cellRef?.length,
No pieces created yet
,
    {cellRef.map((piece: any, index: number) => (
  • Go to Piece {computed(() => index + 1)} Piece {computed(() => index + 1)}:{" "} {piece[NAME] || "Unnamed"}
  • ))}
, )} Create New Piece
), cellRef, }; }, );