Skip to content
MSO CloudDocumentation

UI recipes: exports and period comparison

On this page

Two page-level patterns that are not part of the plugin contract. Each is a short recipe for a page author; the plugin contract is in Plugin development.

Add an export to a page#

Exports go through @yng/exports, <ExportMenu> and useExportMenu. PDF renders server-side from the page's own URL. Reader view: Exports: CSV, Excel, PDF.

  1. Tabular export (CSV and Excel): build items from typed rows, headers from t().

    const { items } = useExportMenu<OrderRow>({
      locale,
      filename: 'orders',
      excludePdf: true, // operations lists are tabular only
      table: {
        sheet: {
          name: t('orders.export.sheet'),
          columns: [
            { header: t('orders.col.id'), value: (r) => r.id },
            { header: t('orders.col.amount'), value: (r) => r.amountVnd }, // integer VND
            { header: t('orders.col.placedAt'), value: (r) => r.placedAt }, // Date to ISO
          ],
          rows,
        },
      },
    });
    return <ExportMenu label={t('common.export')} items={items} />;
    
  2. PDF of a server page: wrap the body in <PdfExportPage filename="…" pdfTitle="…"> from @/lib/page-export-wrapper; its header prop places the action in the page header.

  3. PDF plus tabular: pass pdfRef and pdfTitle with table. A multi-tab deck passes pdfWholeDeck for one whole-deck PDF.

  4. File names go through safeFilename(): no diacritics, at most 64 characters, an ISO date suffix.

Wire period comparison into a report#

Comparison state lives in the URL; @yng/ui/compare-period is the one period implementation for filter bar and server. Reader view: Explore and period comparison.

  1. Parse the URL with parseReportFilters() (apps/web/src/app/(dashboard)/reports/_lib/report-filters.ts): compare=1 plus comparePreset (previous, year-ago, custom) yields filters.compare.

  2. Fetch the primary and comparison windows in parallel:

    const filters = parseReportFilters(await searchParams, 45);
    const compareFilters = filters.compare
      ? { ...filters, from: filters.compare.from, to: filters.compare.to }
      : null;
    const [report, compareReport] = await Promise.all([
      caller.reports.settlement(filters),
      compareFilters ? caller.reports.settlement(compareFilters) : Promise.resolve(null),
    ]);
    
  3. Render each KPI delta through the shared delta formatter: a dash for a zero or non-finite baseline.

  4. On an Explore-style page, pass compareFrom and compareTo to explore.query; the response carries summary.previousValue and previousTimeSeries.

Verify#

pnpm run typecheck && pnpm run lint
python3 scripts/audit/compare-period-helper.py