component-usage.ts 22 KB
import { getProjectProfile } from '../../../utils/project-profile';

/**
 * Shared, framework-agnostic generation guidance for two concerns:
 *
 *  1. STRICT tech-stack compliance — the model MUST build UI from the selected
 *     stack's components and NEVER fall back to raw HTML primitives when an
 *     equivalent component exists. A degradation chain governs which component
 *     wins: custom/project component > library component > native HTML.
 *
 *  2. Fill-container behaviour — whether the generated root fills its host
 *     container (default, for embedding into an app shell) or renders at the
 *     design's intrinsic width (for fixed-width standalone previews).
 *
 * This module is the single extension point for "which component to use".
 * Adding a new UI kit = one entry in LIBRARY_COMPONENT_CATALOG (Open-Closed
 * Principle); no edit to React/Vue prompt code is required.
 */

/** A single library component mapped from a generic UI widget. */
export interface LibComponent {
    /** Tag / JSX element name to use in markup. */
    tag: string;
    /** Named or default import name ('' for auto-import / global registration). */
    importName: string;
    /** Module to import from ('' when globally registered by the kit). */
    importFrom: string;
    /** Optional composition note (for composite components like MUI/shadcn tables). */
    note?: string;
}

/**
 * Per-library UI widget → component catalog.
 *
 * Keys are matched by longest case-insensitive substring against
 * ProjectProfile.componentLibrary, so "Ant Design Vue" wins over "Ant Design",
 * "Material UI (MUI)" resolves, etc. Adding a new kit is a single map entry.
 */
const LIBRARY_COMPONENT_CATALOG: Record<string, Record<string, LibComponent>> = {
    'Element Plus': {
        table: { tag: 'el-table', importName: 'ElTable', importFrom: 'element-plus' },
        button: { tag: 'el-button', importName: 'ElButton', importFrom: 'element-plus' },
        input: { tag: 'el-input', importName: 'ElInput', importFrom: 'element-plus' },
        select: { tag: 'el-select', importName: 'ElSelect', importFrom: 'element-plus' },
        option: { tag: 'el-option', importName: 'ElOption', importFrom: 'element-plus' },
        form: { tag: 'el-form', importName: 'ElForm', importFrom: 'element-plus' },
        formItem: { tag: 'el-form-item', importName: 'ElFormItem', importFrom: 'element-plus' },
        dialog: { tag: 'el-dialog', importName: 'ElDialog', importFrom: 'element-plus' },
        drawer: { tag: 'el-drawer', importName: 'ElDrawer', importFrom: 'element-plus' },
        tabs: { tag: 'el-tabs', importName: 'ElTabs', importFrom: 'element-plus' },
        tabPane: { tag: 'el-tab-pane', importName: 'ElTabPane', importFrom: 'element-plus' },
        card: { tag: 'el-card', importName: 'ElCard', importFrom: 'element-plus' },
        tag: { tag: 'el-tag', importName: 'ElTag', importFrom: 'element-plus' },
        tooltip: { tag: 'el-tooltip', importName: 'ElTooltip', importFrom: 'element-plus' },
        dropdown: { tag: 'el-dropdown', importName: 'ElDropdown', importFrom: 'element-plus' },
        menu: { tag: 'el-menu', importName: 'ElMenu', importFrom: 'element-plus' },
        breadcrumb: { tag: 'el-breadcrumb', importName: 'ElBreadcrumb', importFrom: 'element-plus' },
        pagination: { tag: 'el-pagination', importName: 'ElPagination', importFrom: 'element-plus' },
        checkbox: { tag: 'el-checkbox', importName: 'ElCheckbox', importFrom: 'element-plus' },
        radio: { tag: 'el-radio', importName: 'ElRadio', importFrom: 'element-plus' },
        switch: { tag: 'el-switch', importName: 'ElSwitch', importFrom: 'element-plus' },
        datePicker: { tag: 'el-date-picker', importName: 'ElDatePicker', importFrom: 'element-plus' },
        upload: { tag: 'el-upload', importName: 'ElUpload', importFrom: 'element-plus' },
        progress: { tag: 'el-progress', importName: 'ElProgress', importFrom: 'element-plus' },
        avatar: { tag: 'el-avatar', importName: 'ElAvatar', importFrom: 'element-plus' },
        alert: { tag: 'el-alert', importName: 'ElAlert', importFrom: 'element-plus' },
        steps: { tag: 'el-steps', importName: 'ElSteps', importFrom: 'element-plus' },
        collapse: { tag: 'el-collapse', importName: 'ElCollapse', importFrom: 'element-plus' },
        tree: { tag: 'el-tree', importName: 'ElTree', importFrom: 'element-plus' },
        popover: { tag: 'el-popover', importName: 'ElPopover', importFrom: 'element-plus' },
        slider: { tag: 'el-slider', importName: 'ElSlider', importFrom: 'element-plus' },
        rate: { tag: 'el-rate', importName: 'ElRate', importFrom: 'element-plus' },
        image: { tag: 'el-image', importName: 'ElImage', importFrom: 'element-plus' },
        link: { tag: 'el-link', importName: 'ElLink', importFrom: 'element-plus' },
    },
    'Ant Design Vue': {
        table: { tag: 'a-table', importName: 'Table', importFrom: 'ant-design-vue' },
        button: { tag: 'a-button', importName: 'Button', importFrom: 'ant-design-vue' },
        input: { tag: 'a-input', importName: 'Input', importFrom: 'ant-design-vue' },
        select: { tag: 'a-select', importName: 'Select', importFrom: 'ant-design-vue' },
        option: { tag: 'a-select-option', importName: 'Select', importFrom: 'ant-design-vue', note: 'use <a-select-option> as child of <a-select>' },
        form: { tag: 'a-form', importName: 'Form', importFrom: 'ant-design-vue' },
        formItem: { tag: 'a-form-item', importName: 'Form', importFrom: 'ant-design-vue', note: 'use <a-form-item> as child of <a-form>' },
        modal: { tag: 'a-modal', importName: 'Modal', importFrom: 'ant-design-vue' },
        drawer: { tag: 'a-drawer', importName: 'Drawer', importFrom: 'ant-design-vue' },
        tabs: { tag: 'a-tabs', importName: 'Tabs', importFrom: 'ant-design-vue' },
        tabPane: { tag: 'a-tab-pane', importName: 'Tabs', importFrom: 'ant-design-vue', note: 'use <a-tab-pane> as child of <a-tabs>' },
        card: { tag: 'a-card', importName: 'Card', importFrom: 'ant-design-vue' },
        tag: { tag: 'a-tag', importName: 'Tag', importFrom: 'ant-design-vue' },
        tooltip: { tag: 'a-tooltip', importName: 'Tooltip', importFrom: 'ant-design-vue' },
        dropdown: { tag: 'a-dropdown', importName: 'Dropdown', importFrom: 'ant-design-vue' },
        menu: { tag: 'a-menu', importName: 'Menu', importFrom: 'ant-design-vue' },
        breadcrumb: { tag: 'a-breadcrumb', importName: 'Breadcrumb', importFrom: 'ant-design-vue' },
        pagination: { tag: 'a-pagination', importName: 'Pagination', importFrom: 'ant-design-vue' },
        checkbox: { tag: 'a-checkbox', importName: 'Checkbox', importFrom: 'ant-design-vue' },
        radio: { tag: 'a-radio', importName: 'Radio', importFrom: 'ant-design-vue' },
        switch: { tag: 'a-switch', importName: 'Switch', importFrom: 'ant-design-vue' },
        datePicker: { tag: 'a-date-picker', importName: 'DatePicker', importFrom: 'ant-design-vue' },
        upload: { tag: 'a-upload', importName: 'Upload', importFrom: 'ant-design-vue' },
        progress: { tag: 'a-progress', importName: 'Progress', importFrom: 'ant-design-vue' },
        avatar: { tag: 'a-avatar', importName: 'Avatar', importFrom: 'ant-design-vue' },
        alert: { tag: 'a-alert', importName: 'Alert', importFrom: 'ant-design-vue' },
        steps: { tag: 'a-steps', importName: 'Steps', importFrom: 'ant-design-vue' },
        collapse: { tag: 'a-collapse', importName: 'Collapse', importFrom: 'ant-design-vue' },
        tree: { tag: 'a-tree', importName: 'Tree', importFrom: 'ant-design-vue' },
        popover: { tag: 'a-popover', importName: 'Popover', importFrom: 'ant-design-vue' },
        slider: { tag: 'a-slider', importName: 'Slider', importFrom: 'ant-design-vue' },
        rate: { tag: 'a-rate', importName: 'Rate', importFrom: 'ant-design-vue' },
        image: { tag: 'a-image', importName: 'Image', importFrom: 'ant-design-vue' },
    },
    'Ant Design': {
        table: { tag: 'Table', importName: 'Table', importFrom: 'antd' },
        button: { tag: 'Button', importName: 'Button', importFrom: 'antd' },
        input: { tag: 'Input', importName: 'Input', importFrom: 'antd' },
        select: { tag: 'Select', importName: 'Select', importFrom: 'antd' },
        option: { tag: 'Select.Option', importName: 'Select', importFrom: 'antd', note: 'use <Select.Option> as child of <Select>' },
        form: { tag: 'Form', importName: 'Form', importFrom: 'antd' },
        formItem: { tag: 'Form.Item', importName: 'Form', importFrom: 'antd', note: 'use <Form.Item> as child of <Form>' },
        modal: { tag: 'Modal', importName: 'Modal', importFrom: 'antd' },
        drawer: { tag: 'Drawer', importName: 'Drawer', importFrom: 'antd' },
        tabs: { tag: 'Tabs', importName: 'Tabs', importFrom: 'antd' },
        tabPane: { tag: 'Tabs.TabPane', importName: 'Tabs', importFrom: 'antd', note: 'use <Tabs.TabPane> as child of <Tabs>' },
        card: { tag: 'Card', importName: 'Card', importFrom: 'antd' },
        tag: { tag: 'Tag', importName: 'Tag', importFrom: 'antd' },
        tooltip: { tag: 'Tooltip', importName: 'Tooltip', importFrom: 'antd' },
        dropdown: { tag: 'Dropdown', importName: 'Dropdown', importFrom: 'antd' },
        menu: { tag: 'Menu', importName: 'Menu', importFrom: 'antd' },
        breadcrumb: { tag: 'Breadcrumb', importName: 'Breadcrumb', importFrom: 'antd' },
        pagination: { tag: 'Pagination', importName: 'Pagination', importFrom: 'antd' },
        checkbox: { tag: 'Checkbox', importName: 'Checkbox', importFrom: 'antd' },
        radio: { tag: 'Radio', importName: 'Radio', importFrom: 'antd' },
        switch: { tag: 'Switch', importName: 'Switch', importFrom: 'antd' },
        datePicker: { tag: 'DatePicker', importName: 'DatePicker', importFrom: 'antd' },
        upload: { tag: 'Upload', importName: 'Upload', importFrom: 'antd' },
        progress: { tag: 'Progress', importName: 'Progress', importFrom: 'antd' },
        avatar: { tag: 'Avatar', importName: 'Avatar', importFrom: 'antd' },
        alert: { tag: 'Alert', importName: 'Alert', importFrom: 'antd' },
        steps: { tag: 'Steps', importName: 'Steps', importFrom: 'antd' },
        collapse: { tag: 'Collapse', importName: 'Collapse', importFrom: 'antd' },
        tree: { tag: 'Tree', importName: 'Tree', importFrom: 'antd' },
        popover: { tag: 'Popover', importName: 'Popover', importFrom: 'antd' },
        slider: { tag: 'Slider', importName: 'Slider', importFrom: 'antd' },
        rate: { tag: 'Rate', importName: 'Rate', importFrom: 'antd' },
        image: { tag: 'Image', importName: 'Image', importFrom: 'antd' },
    },
    'Material UI (MUI)': {
        table: { tag: 'Table', importName: 'Table', importFrom: '@mui/material', note: 'compose TableContainer > Table > TableHead > TableRow > TableCell' },
        button: { tag: 'Button', importName: 'Button', importFrom: '@mui/material' },
        input: { tag: 'TextField', importName: 'TextField', importFrom: '@mui/material' },
        select: { tag: 'Select', importName: 'Select', importFrom: '@mui/material' },
        form: { tag: 'FormControl', importName: 'FormControl', importFrom: '@mui/material' },
        dialog: { tag: 'Dialog', importName: 'Dialog', importFrom: '@mui/material' },
        drawer: { tag: 'Drawer', importName: 'Drawer', importFrom: '@mui/material' },
        tabs: { tag: 'Tabs', importName: 'Tabs', importFrom: '@mui/material' },
        card: { tag: 'Card', importName: 'Card', importFrom: '@mui/material' },
        chip: { tag: 'Chip', importName: 'Chip', importFrom: '@mui/material' },
        tooltip: { tag: 'Tooltip', importName: 'Tooltip', importFrom: '@mui/material' },
        menu: { tag: 'Menu', importName: 'Menu', importFrom: '@mui/material' },
        breadcrumb: { tag: 'Breadcrumbs', importName: 'Breadcrumbs', importFrom: '@mui/material' },
        pagination: { tag: 'Pagination', importName: 'Pagination', importFrom: '@mui/material' },
        checkbox: { tag: 'Checkbox', importName: 'Checkbox', importFrom: '@mui/material' },
        radio: { tag: 'Radio', importName: 'Radio', importFrom: '@mui/material' },
        switch: { tag: 'Switch', importName: 'Switch', importFrom: '@mui/material' },
        datePicker: { tag: 'DatePicker', importName: 'DatePicker', importFrom: '@mui/x-date-pickers' },
        progress: { tag: 'LinearProgress', importName: 'LinearProgress', importFrom: '@mui/material' },
        avatar: { tag: 'Avatar', importName: 'Avatar', importFrom: '@mui/material' },
        alert: { tag: 'Alert', importName: 'Alert', importFrom: '@mui/material' },
        slider: { tag: 'Slider', importName: 'Slider', importFrom: '@mui/material' },
    },
    'shadcn/ui': {
        table: { tag: 'Table', importName: 'Table', importFrom: '@/components/ui/table', note: 'compose Table > TableHeader > TableBody > TableRow > TableCell' },
        button: { tag: 'Button', importName: 'Button', importFrom: '@/components/ui/button' },
        input: { tag: 'Input', importName: 'Input', importFrom: '@/components/ui/input' },
        select: { tag: 'Select', importName: 'Select', importFrom: '@/components/ui/select' },
        form: { tag: 'Form', importName: 'Form', importFrom: '@/components/ui/form', note: 'react-hook-form + @/components/ui/form' },
        dialog: { tag: 'Dialog', importName: 'Dialog', importFrom: '@/components/ui/dialog' },
        drawer: { tag: 'Sheet', importName: 'Sheet', importFrom: '@/components/ui/sheet' },
        tabs: { tag: 'Tabs', importName: 'Tabs', importFrom: '@/components/ui/tabs' },
        card: { tag: 'Card', importName: 'Card', importFrom: '@/components/ui/card' },
        badge: { tag: 'Badge', importName: 'Badge', importFrom: '@/components/ui/badge' },
        tooltip: { tag: 'Tooltip', importName: 'Tooltip', importFrom: '@/components/ui/tooltip' },
        dropdown: { tag: 'DropdownMenu', importName: 'DropdownMenu', importFrom: '@/components/ui/dropdown-menu' },
        avatar: { tag: 'Avatar', importName: 'Avatar', importFrom: '@/components/ui/avatar' },
        alert: { tag: 'Alert', importName: 'Alert', importFrom: '@/components/ui/alert' },
        checkbox: { tag: 'Checkbox', importName: 'Checkbox', importFrom: '@/components/ui/checkbox' },
        radio: { tag: 'RadioGroup', importName: 'RadioGroup', importFrom: '@/components/ui/radio-group' },
        switch: { tag: 'Switch', importName: 'Switch', importFrom: '@/components/ui/switch' },
        progress: { tag: 'Progress', importName: 'Progress', importFrom: '@/components/ui/progress' },
        slider: { tag: 'Slider', importName: 'Slider', importFrom: '@/components/ui/slider' },
    },
    'Naive UI': {
        table: { tag: 'n-data-table', importName: 'NDataTable', importFrom: 'naive-ui' },
        button: { tag: 'n-button', importName: 'NButton', importFrom: 'naive-ui' },
        input: { tag: 'n-input', importName: 'NInput', importFrom: 'naive-ui' },
        select: { tag: 'n-select', importName: 'NSelect', importFrom: 'naive-ui' },
        form: { tag: 'n-form', importName: 'NForm', importFrom: 'naive-ui' },
        formItem: { tag: 'n-form-item', importName: 'NFormItem', importFrom: 'naive-ui' },
        dialog: { tag: 'n-modal', importName: 'NModal', importFrom: 'naive-ui' },
        drawer: { tag: 'n-drawer', importName: 'NDrawer', importFrom: 'naive-ui' },
        tabs: { tag: 'n-tabs', importName: 'NTabs', importFrom: 'naive-ui' },
        tabPane: { tag: 'n-tab-pane', importName: 'NTabPane', importFrom: 'naive-ui' },
        card: { tag: 'n-card', importName: 'NCard', importFrom: 'naive-ui' },
        tag: { tag: 'n-tag', importName: 'NTag', importFrom: 'naive-ui' },
        tooltip: { tag: 'n-tooltip', importName: 'NTooltip', importFrom: 'naive-ui' },
        dropdown: { tag: 'n-dropdown', importName: 'NDropdown', importFrom: 'naive-ui' },
        menu: { tag: 'n-menu', importName: 'NMenu', importFrom: 'naive-ui' },
        breadcrumb: { tag: 'n-breadcrumb', importName: 'NBreadcrumb', importFrom: 'naive-ui' },
        pagination: { tag: 'n-pagination', importName: 'NPagination', importFrom: 'naive-ui' },
        checkbox: { tag: 'n-checkbox', importName: 'NCheckbox', importFrom: 'naive-ui' },
        radio: { tag: 'n-radio', importName: 'NRadio', importFrom: 'naive-ui' },
        switch: { tag: 'n-switch', importName: 'NSwitch', importFrom: 'naive-ui' },
        datePicker: { tag: 'n-date-picker', importName: 'NDatePicker', importFrom: 'naive-ui' },
        upload: { tag: 'n-upload', importName: 'NUpload', importFrom: 'naive-ui' },
        progress: { tag: 'n-progress', importName: 'NProgress', importFrom: 'naive-ui' },
        avatar: { tag: 'n-avatar', importName: 'NAvatar', importFrom: 'naive-ui' },
        alert: { tag: 'n-alert', importName: 'NAlert', importFrom: 'naive-ui' },
        steps: { tag: 'n-steps', importName: 'NSteps', importFrom: 'naive-ui' },
        collapse: { tag: 'n-collapse', importName: 'NCollapse', importFrom: 'naive-ui' },
        tree: { tag: 'n-tree', importName: 'NTree', importFrom: 'naive-ui' },
        popover: { tag: 'n-popover', importName: 'NPopover', importFrom: 'naive-ui' },
        slider: { tag: 'n-slider', importName: 'NSlider', importFrom: 'naive-ui' },
        rate: { tag: 'n-rate', importName: 'NRate', importFrom: 'naive-ui' },
    },
};

/** Resolve the catalog for a component-library string (longest-substring match). */
function resolveCatalog(library?: string): Record<string, LibComponent> | null {
    if (!library) return null;
    const lower = library.toLowerCase();
    const keys = Object.keys(LIBRARY_COMPONENT_CATALOG).sort((a, b) => b.length - a.length);
    const hit = keys.find((k) => lower.includes(k.toLowerCase()));
    return hit ? (LIBRARY_COMPONENT_CATALOG[hit] ?? null) : null;
}

/**
 * Build the "STRICT tech-stack compliance" guidance block. Reads the runtime
 * project profile so custom (Tier-1) components are honoured. Returns a
 * non-empty string always (even with no library selected, it still enforces
 * custom-first + semantic-HTML fallback).
 */
export function buildComponentUsageGuidance(): string {
    const profile = getProjectProfile();
    const library = profile?.componentLibrary;
    const catalog = resolveCatalog(library);

    const lines: string[] = [];

    lines.push(
        '      - **STRICT tech-stack compliance (CRITICAL)**: You MUST build every UI widget using the selected stack’s components. NEVER replace a stack/library component with a raw HTML primitive when an equivalent exists in the stack.',
    );
    lines.push(
        '      - **Component priority (degradation chain)** — for any UI widget, pick the FIRST available in this order:',
    );
    lines.push(
        '        1. **Custom / project component** — any component listed in <project_profile><components> is a bespoke component of the target project. ALWAYS prefer it over the library equivalent. Example: if a custom `XTable` is listed and the design needs a data grid, use `XTable`, NOT el-table / Table.',
    );
    lines.push(
        '        2. **Library component** — the component from the selected UI kit (see the catalog below). The active kit is decided by the generation’s componentLibrary setting, so choosing Element Plus vs Ant Design Vue vs Ant Design vs MUI determines which table/button you get.',
    );
    lines.push(
        '        3. **Native HTML** — ONLY when neither a custom nor a library component covers that widget. Even then prefer semantic elements (table, button, form, nav, …).',
    );

    if (catalog) {
        lines.push(`      - **Selected UI kit**: "${library}". Use these components (import the named export, then use the tag in markup):`);
        lines.push('        <component_catalog>');
        for (const [widget, c] of Object.entries(catalog)) {
            const importHint = c.importFrom
                ? `import { ${c.importName} } from '${c.importFrom}'`
                : `${c.importName} (globally registered)`;
            const note = c.note ? ` — ${c.note}` : '';
            lines.push(`          - ${widget}: <${c.tag}>  (${importHint})${note}`);
        }
        lines.push('        </component_catalog>');
    } else {
        lines.push(
            '      - **No UI component library selected** for this generation. Use semantic HTML primitives (table, button, form, nav, …) together with the selected styling strategy. Still prefer any custom component from <project_profile><components> when one matches the widget.',
        );
    }

    lines.push(
        '      - **Concrete example — data table**: if the design contains a data grid / table, do NOT write a native <table><tr><td>. Use, in priority order: (1) a custom table component from <project_profile>, else (2) the kit component — <el-table> (Element Plus), <a-table> (Ant Design Vue), <Table> (Ant Design / MUI), <Table> from @/components/ui/table (shadcn) — driven by its data/columns API. The SAME rule applies to buttons (el-button / <Button>), inputs, forms, selects, dialogs, tabs, cards, tags, pagination, menus, breadcrumbs, etc.',
    );
    lines.push(
        '      - **Hard rule**: generating a native <table> (or a plain <button> / <input>) for a widget that has a stack or library component is a FAILURE. Always map the widget to its stack component.',
    );

    return lines.join('\n');
}

/**
 * Build the "fill container" guidance based on the user's choice.
 *  - fill = true  (default): root must fill the host container (embed scenario).
 *  - fill = false: render at the design's intrinsic width, centered, no stretch.
 */
export function buildStretchGuidance(fill: boolean): string {
    if (fill) {
        return [
            '      - **Fill the container (CRITICAL)**: The generated root component MUST fill its host container. Use `width: 100%; height: 100%` (or, when nested in a flex column, `flex: 1; min-height: 0`). Do NOT hardcode a max-width, a fixed page width, or `min-height: 100vh` that assumes a standalone viewport. The parent app shell owns the outer layout.',
        ].join('\n');
    }
    return [
        '      - **Render at the design’s intrinsic width (do NOT stretch)**: Do NOT expand to 100% of a wide viewport. Render the root at the Figma frame’s original width (e.g. width: 1440px) inside a centered container with `margin: 0 auto`, and let the inner content scroll. This keeps fixed-width designs from breaking on ultra-wide screens.',
    ].join('\n');
}