Get started
Embedded Studio

Use your own design system

Use your own design system

@mountaya/studio-react/headless is the studio's behavior without its look: the store, the tool bus and the hooks the whole editor runs on. Use it to draw the studio with your own components.

Renders nothing

That list is a plain <ul> with plain <input>s, driven by useSegmentList and useSegmentRow.

Wire the provider

MountayaStudioProvider hands three things to every hook beneath it: a tool bus from createStudioToolBus, a store, and the context the bus dispatches with. MountayaStudioHost builds these for you; here you build them once.

import { useMountaya } from "@mountaya/client-react";
import {
  createStaticViewer,
  createUiStore,
  restStudioPersistence,
  type StudioToolContext,
} from "@mountaya/studio";
import { createStudioToolBus } from "@mountaya/studio/handlers";
import { browserSaveFile } from "@mountaya/studio/seams";
import { MountayaStudioProvider, useStudioCache } from "@mountaya/studio-react/headless";
import { type ReactNode, useMemo } from "react";

export const OurStudio = ({ children }: { readonly children: ReactNode }) => {
  const client = useMountaya();
  const cache = useStudioCache(queryClient);
  const { bus, context, store } = useMemo(() => {
    const ui = createUiStore();
    const created = createStudioToolBus();
    const ctx: StudioToolContext = {
      api: { cache, client, persistence: restStudioPersistence(client) },
      bus: created.dispatcher,
      capabilities: new Set(["authenticated", "editor", "embedding"]),
      confirm: async () => window.confirm("Delete this?"),
      map: null,
      saveFile: browserSaveFile,
      ui,
      viewer: createStaticViewer({ getUserId: () => userId }),
    };
    return { bus: created, context: ctx, store: ui };
  }, [cache, client]);

  return (
    <MountayaStudioProvider client={client} data={{ bus, context, store }}>
      {children}
    </MountayaStudioProvider>
  );
};

The client is the one your app already builds, over a member's studio-embedded session token, under a MountayaProvider. The quickstart shows the token route.

Draw a row with the hooks

A hook reports what is true and returns the actions that may change it. Your markup calls the actions; every change goes through the bus.

import { useSegmentRow } from "@mountaya/studio-react/headless";

export const Row = ({ name, segmentId }: { readonly name: string; readonly segmentId: string }) => {
  const row = useSegmentRow<HTMLLIElement>({ itineraryId, segmentId, segmentName: name });

  return (
    <li {...row.rowProps}>
      {row.rename.editing ? (
        <input {...row.rename.inputProps} />
      ) : (
        <button onDoubleClick={row.startRename} type="button">
          {name}
        </button>
      )}
      <button onClick={() => void row.actions.remove()} type="button">
        Delete
      </button>
    </li>
  );
};
HookUse it to
useStudioDispatchRun any verb, typed by its contract.
useUiStoreRead a slice of the studio's state.
useCapabilityHide or disable a control the session may not use.
useSegmentList, useSegmentRowDraw the segment chain.
useWaypointList, useWaypointRowDraw the waypoints.
useStudioObserveSee every dispatch, a click's and the co-pilot's alike.

Dispatch a verb

useStudioDispatch runs any verb and answers its result. A refusal is a result, never a throw.

import { useStudioDispatch } from "@mountaya/studio-react/headless";

export const OpenButton = ({ itineraryId }: { readonly itineraryId: string }) => {
  const { dispatch, pending } = useStudioDispatch();

  const open = async () => {
    const opened = await dispatch("studio.itinerary.openForEdit", { itineraryId });
    if (!opened.ok) {
      console.warn(opened.code, opened.error);
      return;
    }
    console.log(opened.data.opened);
  };

  return (
    <button disabled={pending} onClick={() => void open()} type="button">
      Open
    </button>
  );
};

Bind your app's services

The studio asks your app for a few answers through small providers. Each is optional: without it, the studio stays silent or offers nothing.

ProviderWhat your app answers
StudioNotifyProviderHow to show a success, a warning or an error toast.
StudioOrganizationsProviderWhich organizations the reader may browse and write to.
StudioBillingGateProviderWhat to say when a create reaches your agreement's limit.
StudioNavigationBlockerProviderHow to hold a navigation while an edit is unsaved.
StudioCopilotProviderWhether the co-pilot may run, and what to show if not.
StudioPluginsProviderThe plugins that add menu items and controls.
import { notifications } from "@mantine/notifications";
import { StudioNotifyProvider, StudioProvider } from "@mountaya/studio-react/headless";

const notify = {
  error: (message: string) => notifications.show({ color: "red", message }),
  success: (message: string) => notifications.show({ color: "green", message }),
  warning: (message: string) => notifications.show({ color: "orange", message }),
};

<StudioNotifyProvider notify={notify}>
  <StudioProvider data={{ bus: studioBus }}>{ourSubtree}</StudioProvider>
</StudioNotifyProvider>;

The strip above is this page's own markup, and its third button dispatches a verb no handler answers.

Next steps

Last updated