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

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

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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
 * App stories — demonstrate high-level application features:
 * portal navigation, login page, self-registration, translations.
 *
 * Pattern:
 *  - Portal stories seed Zustand state in a useEffect and render <Portal />.
 *    `auth.isAuthenticated` is set to true so the App shell shows the portal.
 *  - Login stories pass a `loginComponent` in `story.parameters.loginComponent`.
 *    The global `withDispatch` decorator forwards it to <App>, which renders it
 *    when `auth.isAuthenticated` is false.  The story's render sets auth to
 *    unauthenticated and returns null — the App handles the rest.
 *
 * Story map:
 *  WithPortal         — full app: menu + two open tabs, menu-driven navigation
 *  Navigate           — play() opens a menu item and loads a new tab
 *  BulgarianPortal    — same portal but with Bulgarian text (lang='bg')
 *  Login              — app showing the login page (unauthenticated state)
 *  BulgarianLogin     — login page in Bulgarian
 *  LoginError         — login attempt fails, error message shown in form
 */
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 {Login as LoginForm} from '../Login/Login.js';
import {Portal} from '../Portal/Portal.js';

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

const meta: Meta = {
    title: 'App',
    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>;
};

// ── Page components ────────────────────────────────────────────────────────

function DashboardPage() {
    return (
        <div style={{padding: '2rem'}}>
            <h2>Dashboard</h2>
            <p>
                Welcome to the Marine Biology Portal. Select an item from the menu to get started.
            </p>
        </div>
    );
}

// ── Shared portal config ───────────────────────────────────────────────────

const portalConfig: IPortalConfig = {
    name: 'marine-biology',
    title: 'Marine Biology Portal',
    menu: [
        {
            title: 'Data',
            items: [
                {title: 'Species', method: 'app.species', icon: 'pi pi-list'},
                {title: 'Reports', method: 'app.reports', icon: 'pi pi-chart-bar'},
            ],
        },
    ],
};

const portalInitialTabs: ITab[] = [
    {
        id: 'dashboard',
        actionName: 'dashboard',
        params: {},
        title: 'Dashboard',
        component: DashboardPage,
    },
];

// ── Helper: seed portal store ──────────────────────────────────────────────

function PortalApp({tabs, portalConfig}: {tabs: ITab[]; portalConfig?: IPortalConfig}) {
    useEffect(() => {
        useAppStore.setState({portal: {tabs: [], activeTabId: null, portalConfig: null}});
        const store = useAppStore.getState();
        // Authenticate so the App shell shows the portal (not the login page).
        store.setToken('demo-token');
        tabs.forEach(tab => store.openTab(tab));
        if (portalConfig) store.setPortalConfig(portalConfig);
        return () => {
            useAppStore.setState({portal: {tabs: [], activeTabId: null, portalConfig: null}});
            store.logout();
        };
    }, [tabs, portalConfig]);
    return <Portal />;
}

// ── Login page components (used via parameters.loginComponent) ────────────────

/**
 * MarineTitleComponent — pluggable header slot above the brand area.
 */
function MarineTitleComponent() {
    return (
        <div style={{textAlign: 'center', marginBottom: '0.5rem'}}>
            <div style={{color: 'var(--text-color)', fontSize: '1.1rem', fontWeight: 500}}>
                Marine Biology Research Platform
            </div>
            <div style={{color: 'var(--text-color-secondary)', fontSize: '0.875rem'}}>
                Data collection &amp; analysis
            </div>
        </div>
    );
}

/**
 * LoginPage — basic login wrapping Login component.
 * Clicking "Sign In" sets a demo token (transitions App shell to portal).
 */
function LoginPage(_: Record<string, never>) {
    return (
        <LoginForm
            onLogin={async () => {
                await new Promise(r => setTimeout(r, 800));
                useAppStore.getState().setToken('demo-token');
            }}
            title="Marine Biology Portal"
            logoIcon="pi pi-globe"
        />
    );
}

/**
 * MarineOrgComponent — org brand slot below the login card.
 * Equal height to the product area above, keeping the card centred.
 */
function MarineOrgComponent() {
    return (
        <div style={{textAlign: 'center', opacity: 0.75}}>
            <div
                style={{
                    color: 'var(--text-color-secondary)',
                    fontSize: '0.8rem',
                    marginBottom: '0.25rem',
                }}
            >
                Powered by
            </div>
            <div
                style={{color: 'var(--text-color-secondary)', fontSize: '0.95rem', fontWeight: 500}}
            >
                FeasibleOne Platform
            </div>
        </div>
    );
}

/**
 * LoginPageWithTitle — demonstrates the `titleComponent` slot (product area) above the brand
 * and the `orgComponent` slot (org brand area) below — both equal-height to keep the card centred.
 */
function LoginPageWithTitle(_: Record<string, never>) {
    return (
        <LoginForm
            onLogin={async () => {
                await new Promise(r => setTimeout(r, 800));
                useAppStore.getState().setToken('demo-token');
            }}
            title="Marine Biology Portal"
            logoIcon="pi pi-globe"
            titleComponent={MarineTitleComponent}
            orgComponent={MarineOrgComponent}
        />
    );
}

/**
 * LoginPageWithRegister — demonstrates `registerPage` registration mechanism.
 * - Shows a "Register" button in the top-right corner.
 * - Clicking it dispatches `component/xxx` with page `'user.selfRegistration'`.
 * - The global dispatch mock returns a placeholder registration form for page names
 *   ending in 'Registration' / 'Register' / 'SelfRegister'.
 */
function LoginPageWithRegister(_: Record<string, never>) {
    return (
        <LoginForm
            onLogin={async () => {
                await new Promise(r => setTimeout(r, 800));
                useAppStore.getState().setToken('demo-token');
            }}
            title="Marine Biology Portal"
            logoIcon="pi pi-globe"
            titleComponent={MarineTitleComponent}
            orgComponent={MarineOrgComponent}
            registerPage="user.selfRegistration"
        />
    );
}

/** Same as LoginPage but with Bulgarian title (i18n demo). */
function BulgarianLoginPage(_: Record<string, never>) {
    return (
        <LoginForm
            onLogin={async () => {
                await new Promise(r => setTimeout(r, 800));
                useAppStore.getState().setToken('demo-token');
            }}
            title="Портал за морска биология"
            logoIcon="pi pi-globe"
        />
    );
}

/** Login page that always rejects — demonstrates the inline error message. */
function LoginErrorPage(_: Record<string, never>) {
    return (
        <LoginForm
            onLogin={async () => {
                await new Promise(r => setTimeout(r, 500));
                throw new Error('Invalid username or password');
            }}
            title="Marine Biology Portal"
            logoIcon="pi pi-globe"
        />
    );
}

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

/**
 * WithPortal — the full authenticated application shell.
 * Dashboard tab is open by default; the menu lets the user open Species/Reports.
 */
export const WithPortal: Story = {
    render: () => (
        <PortalApp
            tabs={portalInitialTabs}
            portalConfig={portalConfig}
        />
    ),
};

/**
 * Navigate — play() opens the Data menu and navigates to the Species page.
 */
export const Navigate: Story = {
    render: () => (
        <PortalApp
            tabs={portalInitialTabs}
            portalConfig={portalConfig}
        />
    ),
    play: async ({canvas, userEvent}) => {
        // Open the 'Data' sub-menu
        await userEvent.click(await canvas.findByText('Data' as never));
        // Click the 'Species' item
        await userEvent.click(await canvas.findByText('Species' as never));
        await new Promise(r => setTimeout(r, 300));
    },
};

/**
 * BulgarianPortal — authenticated portal with Bulgarian UI labels.
 * Pass `lang: 'bg'` — the global decorator translates PrimeReact widgets + store labels.
 */
export const BulgarianPortal: Story = {
    args: {lang: 'bg'},
    render: () => (
        <PortalApp
            tabs={[
                {
                    id: 'dashboard',
                    actionName: 'dashboard',
                    params: {},
                    title: 'Табло',
                    component: DashboardPage,
                },
            ]}
            portalConfig={{
                ...portalConfig,
                title: 'Портал за морска биология',
                menu: [
                    {
                        title: 'Данни',
                        items: [
                            {title: 'Видове', method: 'app.species', icon: 'pi pi-list'},
                            {title: 'Справки', method: 'app.reports', icon: 'pi pi-chart-bar'},
                        ],
                    },
                ],
            }}
        />
    ),
};

/** Ensures the app is in unauthenticated state when the story mounts. */
function LogoutOnMount() {
    useEffect(() => {
        useAppStore.getState().logout();
    }, []);
    return <></>;
}

/**
 * Login — app in unauthenticated state: shows the login form with brand area.
 * The `loginComponent` parameter is forwarded to <App> by the global `withDispatch`
 * decorator. App renders it when `auth.isAuthenticated` is false.
 * Clicking Login sets a demo token and transitions to the portal.
 */
export const Login: Story = {
    parameters: {loginComponent: LoginPage},
    render: () => <LogoutOnMount />,
};

/**
 * LoginWithTitle — login screen with the pluggable `titleComponent` slot above
 * the brand area.
 */
export const LoginWithTitle: Story = {
    parameters: {loginComponent: LoginPageWithTitle},
    render: () => <LogoutOnMount />,
};
LoginWithTitle.storyName = 'Login With Title';

/**
 * LoginWithRegister — login screen with a "Register" button.
 * Clicking Register dispatches `component/xxx` with page `'user.selfRegistration'`.
 * The global mock returns a placeholder registration component for page names
 * ending with 'Registration', 'Register', or 'SelfRegister'.
 */
export const LoginWithRegister: Story = {
    parameters: {loginComponent: LoginPageWithRegister},
    render: () => <LogoutOnMount />,
};
LoginWithRegister.storyName = 'Login With Register';

/**
 * BulgarianLogin — login page in Bulgarian.
 */
export const BulgarianLogin: Story = {
    args: {lang: 'bg'},
    parameters: {loginComponent: BulgarianLoginPage},
    render: () => <LogoutOnMount />,
};
BulgarianLogin.storyName = 'Bulgarian Login';

/**
 * LoginError — login attempt always fails; demonstrates the inline form error state.
 */
export const LoginError: Story = {
    parameters: {loginComponent: LoginErrorPage},
    play: async ({canvas, userEvent}) => {
        await userEvent.type(await canvas.findByLabelText('Username' as never), 'testuser');
        await userEvent.type(await canvas.findByLabelText('Password' as never), 'wrongpass');
        await userEvent.click(await canvas.findByRole('button' as never, {name: /^login$/i}));
    },
    render: () => <LogoutOnMount />,
};