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

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import type {Meta, StoryObj} from '@storybook/react-vite';
import React from 'react';
import {useAppStore} from '../../state/appStore.js';
import {Explorer} from './Explorer.js';

const meta: Meta<typeof Explorer> = {
    title: 'Data/Explorer',
    component: Explorer,
    tags: ['autodocs'],
    parameters: {layout: 'fullscreen'},
};
export default meta;

type Story = StoryObj<typeof Explorer>;

// ── Shared columns ─────────────────────────────────────────────────────────

const columns = [
    {field: 'coralName', header: 'Name', sortable: true},
    {field: 'coralType', header: 'Type', sortable: true},
    {field: 'maxDepth', header: 'Max Depth', sortable: true},
    {field: 'endangered', header: 'Endangered', sortable: false},
];

// ── Shared toolbar ─────────────────────────────────────────────────────────

const toolbar = [
    {
        label: 'Create',
        icon: 'pi pi-plus',
        method: 'coralCoralAdd',
    },
    {
        label: 'Edit',
        icon: 'pi pi-pencil',
        enabled: 'current' as const,
        method: 'coralCoralEdit',
    },
    {
        label: 'Delete',
        icon: 'pi pi-trash',
        enabled: 'selected' as const,
        confirm: 'Do you want to delete the selected corals?',
        method: 'coralCoralDelete',
    },
];

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

/** Default — full-featured coral list with toolbar and pagination. */
export const Default: Story = {
    args: {
        keyField: 'coralId',
        columns,
        listAction: 'coralCoralFind',
        selectionMode: 'multiple',
        toolbar,
    },
};

/** Loading — `listAction` never resolves; skeleton / loading state persists. */
export const Loading: Story = {
    args: {
        ...Default.args,
        listAction: 'coralCoralLoad',
    },
};

/** GetError — list fetch rejects; global error dialog appears. */
export const GetError: Story = {
    args: {
        ...Default.args,
        listAction: 'coralCoralFindError',
    },
};

/**
 * ActionPermissions — Create/Delete are forbidden; Edit is granted.
 * Demonstrates permission-aware toolbar button visibility.
 */
export const ActionPermissions: Story = {
    decorators: [
        (Story: React.ComponentType) => {
            React.useEffect(() => {
                useAppStore.getState().setPermissions({granted: true, forbidden: false});
                return () => useAppStore.getState().setPermissions({});
            }, []);
            return <Story />;
        },
    ],
    args: {
        ...Default.args,
        toolbar: [
            {label: 'Create', icon: 'pi pi-plus', permission: 'forbidden', method: 'coralCoralAdd'},
            {
                label: 'Edit',
                icon: 'pi pi-pencil',
                permission: 'granted',
                enabled: 'current' as const,
                method: 'coralCoralEdit',
            },
            {
                label: 'Delete',
                icon: 'pi pi-trash',
                permission: 'forbidden',
                enabled: 'selected' as const,
                method: 'coralCoralDelete',
            },
        ],
    },
};

/**
 * Details — side panel shows the currently selected coral's fields and total
 * record count.  Click a row to populate the panel.
 */
export const Details: Story = {
    args: {
        ...Default.args,
        selectionMode: 'single',
        details: (row, total) => (
            <div style={{padding: '0.25rem'}}>
                <div
                    style={{
                        marginBottom: '0.5rem',
                        color: 'var(--text-color-secondary)',
                        fontSize: '0.75rem',
                    }}
                >
                    Records: {total ?? 0}
                </div>
                {row ? (
                    <>
                        <div>
                            <strong>Name:</strong> {String(row.coralName ?? '')}
                        </div>
                        <div>
                            <strong>Type:</strong> {String(row.coralType ?? '')}
                        </div>
                        <div>
                            <strong>Max Depth:</strong> {String(row.maxDepth ?? '')}m
                        </div>
                        <div>
                            <strong>Endangered:</strong> {row.endangered ? 'Yes' : 'No'}
                        </div>
                    </>
                ) : (
                    <div style={{color: 'var(--text-color-secondary)'}}>Select a row</div>
                )}
            </div>
        ),
    },
};

/**
 * Children — a custom navigation panel rendered to the left of the table.
 * Demonstrates the `children` left-panel prop.
 */
export const Children: Story = {
    args: {
        ...Default.args,
        children: (
            <div style={{padding: '0.75rem'}}>
                <strong>Filter by type</strong>
                <ul style={{margin: '0.5rem 0 0', padding: '0 0 0 1rem'}}>
                    {['All', 'Hard', 'Soft', 'Black', 'Fire'].map(t => (
                        <li
                            key={t}
                            style={{cursor: 'pointer', padding: '0.25rem 0'}}
                        >
                            {t}
                        </li>
                    ))}
                </ul>
            </div>
        ),
    },
};

/**
 * Grid — DataView card layout; useful for image-heavy or rich-content rows.
 */
export const Grid: Story = {
    args: {
        ...Default.args,
        view: 'grid',
        selectionMode: 'none',
    },
};

/**
 * GridFlex — compact horizontal cards (icon + fields side by side).
 */
export const GridFlex: Story = {
    args: {
        ...Default.args,
        view: 'grid',
        selectionMode: 'none',
        pageSize: 25,
        cardTemplate: row => (
            <div className="col-6 md:col-3">
                <div
                    style={{
                        display: 'flex',
                        alignItems: 'center',
                        gap: '0.5rem',
                        border: '1px solid var(--surface-border)',
                        borderRadius: 'var(--border-radius)',
                        padding: '0.5rem 0.75rem',
                        margin: '0.25rem',
                        background: 'var(--surface-card)',
                        cursor: 'pointer',
                    }}
                >
                    <i
                        className="pi pi-paperclip"
                        style={{color: 'var(--text-color-secondary)'}}
                    />
                    <span style={{flex: 1, fontSize: '0.875rem', fontWeight: 600}}>
                        {String(row.coralName ?? '')}
                    </span>
                    <span style={{fontSize: '0.875rem', color: 'var(--text-color-secondary)'}}>
                        {String(row.coralType ?? '')}
                    </span>
                </div>
            </div>
        ),
    },
};

/**
 * WithStaticData — inline render with explicit columns and toolbar.
 * Useful for snapshot tests and design validation.
 */
export const WithStaticData: Story = {
    render: () => (
        <div style={{height: 500}}>
            <Explorer
                columns={columns}
                listAction="coralCoralFind"
                selectionMode="multiple"
                toolbar={toolbar}
                onSelectionChange={v => console.log('selected', v)}
            />
        </div>
    ),
};

// ── Submit ─────────────────────────────────────────────────────────────────
// Demonstrates:
//   • ${coralId}       — passes the keyField value of the current row
//   • ${current}       — passes the entire current row
//   • ${selected}      — passes all selected rows
//   • ${current.field} — passes a single field from the current row
//   • successHint      — shows an overlay next to the button after success
//   • column action    — Species Name cells render as clickable links
//   • filter row       — inline filter inputs below the column headers

const submitColumns = [
    {
        field: 'coralName',
        header: 'Name',
        sortable: true,
        filterable: true,
        action: 'coralCoralOpen',
    },
    {field: 'coralType', header: 'Type', sortable: true, filterable: true},
    {field: 'maxDepth', header: 'Max Depth', sortable: true, filterable: true},
    {field: 'endangered', header: 'Endangered', sortable: false},
];

/**
 * Submit — demonstrates ${id}, ${current}, ${selected}, ${current.field} template
 * resolution in toolbar button params, column-link actions, and the successHint overlay.
 *
 * Click a row to select it, then use the toolbar buttons:
 * — **Submit id**: sends `{id: row.id}` to `coralCoralSubmit`
 * — **Submit current**: sends the full row to `coralCoralSubmit`
 * — **Submit selected**: sends the selected rows array to `coralCoralSubmit`
 * — **Submit template**: sends `{id, maxDepth}` derived from the current row
 * — **Error**: always callable; triggers a server-side error toast
 * — **Delay**: 1.5 s delay then shows a "Done" hint overlay on the button
 *
 * The **Species Name** column renders as a link — clicking it fires `coralCoralOpen`
 * with the full row, and the result is shown in the Storybook toast.
 */
export const Submit: Story = {
    args: {
        columns: submitColumns,
        listAction: 'coralCoralFind',
        selectionMode: 'multiple',
        keyField: 'coralId',
        toolbar: [
            {
                label: 'Submit id',
                icon: 'pi pi-send',
                enabled: 'current',
                method: 'coralCoralSubmit',
                params: '${coralId}',
            },
            {
                label: 'Submit current',
                icon: 'pi pi-inbox',
                enabled: 'current',
                method: 'coralCoralSubmit',
                params: '${current}',
            },
            {
                label: 'Submit selected',
                icon: 'pi pi-list',
                enabled: 'selected',
                method: 'coralCoralSubmit',
                params: '${selected}',
            },
            {
                label: 'Submit template',
                icon: 'pi pi-file-export',
                enabled: 'current',
                method: 'coralCoralSubmit',
                params: {coralId: '${coralId}', maxDepth: '${current.maxDepth}'},
            },
            {
                label: 'Error',
                icon: 'pi pi-exclamation-circle',
                method: 'coralCoralSubmitError',
                params: {},
            },
            {
                label: 'Delay',
                icon: 'pi pi-clock',
                method: 'coralCoralSubmitDelay',
                successHint: 'Done',
                params: {},
            },
        ],
    },
};