All files / blong-browser/src/components/Portal Portal.stories.tsx

0% Statements 0/275
0% Branches 0/1
0% Functions 0/1
0% Lines 0/275

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
/**
 * Portal stories — demonstrate portal shell capabilities:
 * tab management, menu navigation, error handling per tab, multi-page layouts.
 *
 * Each story wraps <Portal /> inside a thin setup component that seeds the
 * Zustand app store with the desired tabs + menu config, then cleans up on
 * unmount so stories don't bleed state into each other.
 *
 * The global `withDispatch` decorator (from .storybook/preview.tsx) already
 * wraps every story in <App> providing BlongProvider + Theme context, so
 * Portal can read from the store and dispatch can be used inside tabs.
 */
import type {Meta, StoryObj} from '@storybook/react-vite';
import type {within} from '@testing-library/react';
import type {UserEvent} from '@testing-library/user-event';
import {useEffect} from 'react';
import {useAppStore} from '../../state/appStore.js';
import type {IPortalConfig, ITab} from '../../types/portal.js';
import {Basic as EditorBasic} from '../Editor/Editor.stories.js';
import {Explorer} from '../Explorer/Explorer.js';
import {Default as ExplorerDefault} from '../Explorer/Explorer.stories.js';
import {Portal} from './Portal.js';

// ── Meta ───────────────────────────────────────────────────────────────────

const meta: Meta = {
    title: 'Portal',
    parameters: {layout: 'fullscreen'},
};
export default meta;

type Story = Omit<StoryObj<typeof meta>, 'play'> & {
    play?: (ctx: {canvas: ReturnType<typeof within>; userEvent: UserEvent}) => Promise<void>;
    args?: Record<string, unknown>;
};

// ── Helpers ────────────────────────────────────────────────────────────────

/**
 * Seed the portal Zustand store with tabs (and optional menu config) and
 * clean up on unmount.  Wraps Portal so stories are self-contained.
 */
function PortalSetup({tabs, portalConfig}: {tabs: ITab[]; portalConfig?: IPortalConfig}) {
    useEffect(() => {
        // Reset first to avoid bleed-through from previous stories
        useAppStore.setState({portal: {tabs: [], activeTabId: null, portalConfig: null}});
        const store = useAppStore.getState();
        tabs.forEach(tab => store.openTab(tab));
        if (portalConfig) store.setPortalConfig(portalConfig);
        return () => {
            useAppStore.setState({portal: {tabs: [], activeTabId: null, portalConfig: null}});
        };
        // eslint-disable-next-line @eslint-react/exhaustive-deps -- run once on mount
    }, []);
    return <Portal />;
}

// ── Simple page components used by multiple stories ───────────────────────

function PageOne() {
    return (
        <div style={{padding: '2rem'}}>
            <h2>Page One</h2>
            <p>This is the content of page one.</p>
        </div>
    );
}

function PageTwo() {
    return (
        <div style={{padding: '2rem'}}>
            <h2>Page Two</h2>
            <p>This is the content of page two.</p>
        </div>
    );
}

function PageThree() {
    return (
        <div style={{padding: '2rem'}}>
            <h2>Page Three</h2>
            <p>This is the content of page three.</p>
        </div>
    );
}

function ErrorPage() {
    throw new Error('This is an intentional error to test the tab error boundary');
    return null;
}

// ── Stories ────────────────────────────────────────────────────────────────

/**
 * Basic — portal with two plain-text tabs already open.
 * Demonstrates tab switching and the close-tab button.
 */
export const Basic: Story = {
    render: () => (
        <PortalSetup
            tabs={[
                {id: 'page1', actionName: 'page1', params: {}, title: 'Page 1', component: PageOne},
                {id: 'page2', actionName: 'page2', params: {}, title: 'Page 2', component: PageTwo},
            ]}
        />
    ),
};

/**
 * MultiplePages — portal with three tabs showing different content types.
 */
export const MultiplePages: Story = {
    render: () => (
        <PortalSetup
            tabs={[
                {id: 'page1', actionName: 'page1', params: {}, title: 'Page 1', component: PageOne},
                {id: 'page2', actionName: 'page2', params: {}, title: 'Page 2', component: PageTwo},
                {
                    id: 'page3',
                    actionName: 'page3',
                    params: {},
                    title: 'Page 3',
                    component: PageThree,
                },
            ]}
        />
    ),
};

MultiplePages.play = async ({canvas, userEvent}) => {
    // Switch to the second tab
    await userEvent.click(await canvas.findByText('Page 2' as never));
    await new Promise(r => setTimeout(r, 100));
    // Switch to the third tab
    await userEvent.click(await canvas.findByText('Page 3' as never));
};

/**
 * WithMenu — portal whose menubar has navigation items.
 * Clicking a menu item opens the corresponding page tab.
 *
 * The actions are registered via registerActions so openByAction can resolve them.
 */
function WithMenuStory() {
    useEffect(() => {
        const store = useAppStore.getState();
        // Configure menu
        store.setPortalConfig({
            name: 'demo',
            title: 'Demo Portal',
            menu: [
                {
                    title: 'View',
                    items: ['view.pageOne', 'view.pageTwo'],
                },
            ],
        });
        useAppStore.setState(s => ({portal: {...s.portal, tabs: [], activeTabId: null}}));
        return () => {
            useAppStore.setState({portal: {tabs: [], activeTabId: null, portalConfig: null}});
        };
    }, []);
    return <Portal />;
}

export const WithMenu: Story = {
    render: () => <WithMenuStory />,
};

WithMenu.play = async ({canvas, userEvent}) => {
    // Open the "View" menu
    await userEvent.click(await canvas.findByText('View' as never));
    await new Promise(r => setTimeout(r, 200));
    // Click "Page 1"
    await userEvent.click(await canvas.findByText('Page 1' as never));
};

/**
 * ErrorTab — one tab whose component throws an error.
 * Demonstrates the per-tab error boundary: only the failing tab shows an error
 * message; the rest of the portal remains functional.
 */
export const ErrorTab: Story = {
    render: () => (
        <PortalSetup
            tabs={[
                {id: 'ok', actionName: 'ok', params: {}, title: 'Working Tab', component: PageOne},
                {
                    id: 'error-tab',
                    actionName: 'error-tab',
                    params: {},
                    title: 'Error Tab',
                    component: ErrorPage,
                },
            ]}
        />
    ),
};

ErrorTab.play = async ({canvas, userEvent}) => {
    // Navigate to the tab with the error
    await userEvent.click(await canvas.findByText('Error Tab' as never));
};

/**
 * DirtyTab — demonstrates the unsaved-changes indicator (●) in the tab header.
 * The second tab is flagged dirty from the start.
 */
export const DirtyTab: Story = {
    render: () => (
        <PortalSetup
            tabs={[
                {id: 'clean', actionName: 'clean', params: {}, title: 'Saved', component: PageOne},
                {
                    id: 'dirty',
                    actionName: 'dirty',
                    params: {},
                    title: 'Unsaved',
                    component: PageTwo,
                    dirty: true,
                },
            ]}
        />
    ),
};

/**
 * WithEditor — portal hosting an Editor component in a tab.
 * Reuses the `Basic` story from Editor.stories — same fixture data, same layout.
 */
export const WithEditor: Story = {
    render: () => (
        <PortalSetup
            tabs={[
                {
                    id: 'editor',
                    actionName: 'editor',
                    params: {},
                    title: 'Edit Tree',
                    component: EditorBasic,
                },
            ]}
        />
    ),
};

/**
 * WithExplorer — portal hosting an Explorer component in a tab.
 * Reuses the `Default` story args from Explorer.stories — same columns, toolbar,
 * and list action.
 */
function ExplorerTab(props: Record<string, unknown>) {
    return (
        <Explorer
            {...(ExplorerDefault.args as Record<string, unknown>)}
            {...props}
        />
    );
}

export const WithExplorer: Story = {
    render: () => (
        <PortalSetup
            tabs={[
                {
                    id: 'explorer',
                    actionName: 'explorer',
                    params: {},
                    title: 'Coral List',
                    component: ExplorerTab,
                },
            ]}
        />
    ),
};