All files / blong-browser/src/components/Report Report.tsx

29.49% Statements 64/217
50% Branches 2/4
0% Functions 0/1
29.49% Lines 64/217

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 2181x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Report — read-only data viewer with export capabilities.
 *
 * Combines a filter bar, a summary section (cards), and a detail DataTable.
 * Designed for print/export-oriented display of aggregated data.
 */
import {Column, DataTable, Panel, Skeleton, Toolbar} from '../../primereact/index.js';
 
import type {IEnrichedSchema} from '@feasibleone/blong';
import {useState} from 'react';
import {useAction} from '../../hooks/useAction.js';
import {Button} from '../Button/Button.js';
import {Form} from '../Form/Form.js';
 
export interface IReportColumn {
    field: string;
    header?: string;
    sortable?: boolean;
    width?: string | number;
    /** Aggregate function for footer */
    aggregate?: 'sum' | 'count' | 'avg';
}
 
export interface IReportSummaryMetric {
    label: string;
    /** Field path from the report data */
    field: string;
    icon?: string;
    /** Color class */
    color?: 'success' | 'warn' | 'danger' | 'info';
}
 
export interface IReportProps {
    /** Action name returning report data */
    dataAction?: string;
 
    /** Schema for the filter form */
    filterSchema?: IEnrichedSchema;
    /** Initial filter values */
    defaultFilter?: Record<string, unknown>;
 
    /** Columns for the detail table */
    columns?: IReportColumn[];
 
    /** Summary metric definitions shown above the table */
    metrics?: IReportSummaryMetric[];

    /** Title */
    title?: string;

    /** Enable CSV/XLS/PDF export buttons */
    exportable?: boolean;

    className?: string;
}

type ReportRow = Record<string, unknown>;

export function Report({
    dataAction = '',
    filterSchema,
    defaultFilter,
    columns = [],
    metrics = [],
    title,
    exportable = true,
    className = '',
}: IReportProps) {
    const [filter, setFilter] = useState<Record<string, unknown>>(defaultFilter ?? {});

    const {data, loading, refetch} = useAction<{
        rows: ReportRow[];
        summary?: Record<string, unknown>;
    }>(dataAction, 'query', filter);

    const rows: ReportRow[] = data?.rows ?? [];
    const summary = data?.summary ?? {};

    const exportCSV = () => {
        if (!rows.length) return;
        const headers = columns.map(c => c.header ?? c.field).join(',');
        const body = rows
            .map(r => columns.map(c => JSON.stringify(r[c.field] ?? '')).join(','))
            .join('\n');
        const blob = new Blob([`${headers}\n${body}`], {type: 'text/csv'});
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = `${title ?? 'report'}.csv`;
        a.click();
        URL.revokeObjectURL(url);
    };

    return (
        <div className={`blong-report ${className}`}>
            {/* Filter form */}
            {filterSchema && (
                <Panel
                    header="Filters"
                    toggleable
                    collapsed={!defaultFilter}
                    className="blong-report-filters"
                >
                    <Form
                        schema={filterSchema}
                        value={filter}
                        onChange={setFilter}
                        readOnly={false}
                        loading={loading}
                    />
                    <Button
                        label="Run Report"
                        icon="pi pi-play"
                        onClick={() => refetch?.()}
                        loading={loading}
                        className="blong-report-run"
                    />
                </Panel>
            )}

            {/* Summary metrics */}
            {metrics.length > 0 && (
                <div className="blong-report-metrics">
                    {metrics.map(metric => (
                        <div
                            key={metric.field}
                            className={`blong-report-metric blong-report-metric--${metric.color ?? 'info'}`}
                        >
                            {metric.icon && (
                                <i className={`pi ${metric.icon} blong-report-metric__icon`} />
                            )}
                            <div className="blong-report-metric__value">
                                {loading ? (
                                    <Skeleton
                                        width="60px"
                                        height="1.5rem"
                                    />
                                ) : (
                                    String(
                                        summary[metric.field] ??
                                            rows.reduce(
                                                (acc, r) => acc + Number(r[metric.field] ?? 0),
                                                0,
                                            ),
                                    )
                                )}
                            </div>
                            <div className="blong-report-metric__label">{metric.label}</div>
                        </div>
                    ))}
                </div>
            )}

            {/* Data table */}
            <div className="blong-report-table">
                <Toolbar
                    left={title ? <h3 className="blong-report-title">{title}</h3> : undefined}
                    right={
                        <div>
                            <Button
                                icon="pi pi-refresh"
                                className="p-button-text"
                                onClick={() => refetch?.()}
                                tooltip="Refresh"
                            />
                            {exportable && (
                                <Button
                                    icon="pi pi-download"
                                    className="p-button-text"
                                    onClick={exportCSV}
                                    tooltip="Export CSV"
                                />
                            )}
                        </div>
                    }
                    className="blong-report-toolbar"
                />
                <DataTable
                    value={rows}
                    loading={loading}
                    sortMode="multiple"
                    removableSort
                    scrollable
                    scrollHeight="flex"
                    paginator
                    rows={50}
                    rowsPerPageOptions={[25, 50, 100, 500]}
                    emptyMessage="No data."
                    size="small"
                    className="blong-report-dt"
                >
                    {columns.map(col => (
                        <Column
                            key={col.field}
                            field={col.field}
                            header={col.header ?? col.field}
                            sortable={col.sortable !== false}
                            style={col.width ? {width: col.width} : undefined}
                            footer={
                                col.aggregate === 'sum'
                                    ? String(
                                          rows.reduce(
                                              (acc, r) => acc + Number(r[col.field] ?? 0),
                                              0,
                                          ),
                                      )
                                    : col.aggregate === 'count'
                                      ? String(rows.length)
                                      : undefined
                            }
                        />
                    ))}
                </DataTable>
            </div>
        </div>
    );
}