Skip to main content

@ttoss/react-dashboard

About

A comprehensive React dashboard module that provides fully customizable dashboard functionality with filters, templates, and responsive grid layouts. This module enables you to create data-rich dashboards with support for multiple card types, filter systems, and template management.

Installation

pnpm add @ttoss/react-dashboard

Getting Started

Provider Setup

If you're using the Dashboard component directly, you don't need to set up the provider separately as it wraps the DashboardProvider internally. However, if you need to use the useDashboard hook in other components, you can wrap your application with the DashboardProvider:

import { DashboardProvider } from '@ttoss/react-dashboard';
import { ThemeProvider } from '@ttoss/ui';
import type {
DashboardTemplate,
DashboardFilter,
} from '@ttoss/react-dashboard';

const templates: DashboardTemplate[] = [];
const filters: DashboardFilter[] = [];
const selectedTemplate: DashboardTemplate | undefined = undefined;

ReactDOM.render(
<React.StrictMode>
<ThemeProvider>
<DashboardProvider
filters={filters}
templates={templates}
selectedTemplate={selectedTemplate}
>
<App />
</DashboardProvider>
</ThemeProvider>
</React.StrictMode>,
document.getElementById('root')
);

Basic Dashboard Usage

import { Dashboard } from '@ttoss/react-dashboard';
import type {
DashboardTemplate,
DashboardFilter,
} from '@ttoss/react-dashboard';
import { DashboardFilterType } from '@ttoss/react-dashboard';

const MyDashboard = () => {
const selectedTemplate: DashboardTemplate = {
id: 'default',
name: 'Default Template',
description: 'My default dashboard layout',
grid: [
{
i: 'card-1',
x: 0,
y: 0,
w: 4,
h: 4,
card: {
title: 'Total Revenue',
numberType: 'currency',
type: 'bigNumber',
sourceType: [{ source: 'api' }],
data: {
api: { total: 150000 },
},
},
},
],
};

const filters: DashboardFilter[] = [
{
key: 'date-range',
type: DashboardFilterType.DATE_RANGE,
label: 'Date Range',
value: { from: new Date(), to: new Date() },
},
];

return (
<Dashboard
selectedTemplate={selectedTemplate}
templates={[]}
filters={filters}
loading={false}
/>
);
};

Components

Dashboard

The main dashboard component that orchestrates the entire dashboard experience.

import { Dashboard } from '@ttoss/react-dashboard';

<Dashboard
selectedTemplate={selectedTemplate}
templates={templates}
filters={filters}
loading={false}
headerChildren={<CustomHeaderContent />}
onFiltersChange={(filters) => {
// Handle filter changes
}}
/>;

Props:

PropTypeDefaultDescription
selectedTemplateDashboardTemplate | undefined-The template to display in the dashboard
templatesDashboardTemplate[][]Array of dashboard templates
filtersDashboardFilter[][]Array of dashboard filters
loadingbooleanfalseLoading state for the dashboard
headerChildrenReact.ReactNode-Additional content to render in the header
onFiltersChange(filters: DashboardFilter[]) => void-Callback when filters change

DashboardProvider

Context provider that manages dashboard state (filters and templates).

import { DashboardProvider } from '@ttoss/react-dashboard';

<DashboardProvider
filters={filters}
templates={templates}
selectedTemplate={selectedTemplate}
onFiltersChange={handleFiltersChange}
>
{children}
</DashboardProvider>;

Props:

PropTypeDefaultDescription
childrenReact.ReactNode-Child components
filtersDashboardFilter[][]Filter state
templatesDashboardTemplate[][]Template state
selectedTemplateDashboardTemplate | undefined-The template to display
onFiltersChange(filters: DashboardFilter[]) => void-Callback when filters change

useDashboard Hook

Hook to access and modify dashboard state.

import { useDashboard } from '@ttoss/react-dashboard';

const MyComponent = () => {
const { filters, updateFilter, templates, selectedTemplate } = useDashboard();

// Use dashboard state
const handleFilterChange = (key: string, value: DashboardFilterValue) => {
updateFilter(key, value);
};
};

Returns:

PropertyTypeDescription
filtersDashboardFilter[]Current filter state
updateFilter(key: string, value: DashboardFilterValue) => voidFunction to update a specific filter by key
templatesDashboardTemplate[]Current template state
selectedTemplateDashboardTemplate | undefinedCurrently selected template passed as prop

DashboardCard

Component for rendering individual dashboard cards. Currently supports bigNumber type.

import { DashboardCard } from '@ttoss/react-dashboard';

<DashboardCard
title="Total Revenue"
description="Revenue from all sources"
numberType="currency"
type="bigNumber"
sourceType={[{ source: 'api' }]}
data={{
api: { total: 150000 },
}}
trend={{
value: 15.5,
status: 'positive',
}}
/>;

Props:

PropTypeDefaultDescription
titlestring-Card title
descriptionstring-Optional card description
iconstring-Optional icon name
colorstring-Optional color for the card
variantCardVariant-Card variant ('default' | 'dark' | 'light-green')
numberTypeCardNumberType-Number formatting type ('number' | 'percentage' | 'currency')
typeDashboardCardType-Card type ('bigNumber' | 'pieChart' | 'barChart' | 'lineChart' | 'table' | 'list')
sourceTypeCardSourceType[]-Data source configuration
labelsArray<string | number>-Optional labels for the card
dataDashboardCardData-Card data from various sources
trendTrendIndicator-Optional trend indicator
additionalInfostring-Optional additional information text
statusStatusIndicator-Optional status indicator

DashboardFilters

Component that automatically renders filters based on the dashboard state.

import { DashboardFilters } from '@ttoss/react-dashboard';

// Automatically renders filters from DashboardProvider context
<DashboardFilters />;

This component reads filters from the DashboardProvider context and renders the appropriate filter component for each filter type.

DashboardHeader

Header component that displays filters and optional custom content.

import { DashboardHeader } from '@ttoss/react-dashboard';

<DashboardHeader>
<CustomActionButtons />
</DashboardHeader>;

Props:

PropTypeDescription
childrenReact.ReactNodeOptional content to render in header

DashboardGrid

Responsive grid layout component that displays dashboard cards using react-grid-layout.

import { DashboardGrid } from '@ttoss/react-dashboard';

<DashboardGrid loading={false} />;

Props:

PropTypeDefaultDescription
loadingboolean-Shows loading spinner if true

Filter Types

Text Filter

A text input filter for string values.

import { DashboardFilterType } from '@ttoss/react-dashboard';

const textFilter: DashboardFilter = {
key: 'search',
type: DashboardFilterType.TEXT,
label: 'Search',
placeholder: 'Enter search term...',
value: '',
onChange: (value) => {
console.log('Search:', value);
},
};

Select Filter

A dropdown select filter for predefined options.

const selectFilter: DashboardFilter = {
key: 'status',
type: DashboardFilterType.SELECT,
label: 'Status',
value: 'active',
options: [
{ label: 'Active', value: 'active' },
{ label: 'Inactive', value: 'inactive' },
],
onChange: (value) => {
console.log('Status:', value);
},
};

Date Range Filter

A date range picker with optional presets.

const dateRangeFilter: DashboardFilter = {
key: 'date-range',
type: DashboardFilterType.DATE_RANGE,
label: 'Date Range',
value: { from: new Date(), to: new Date() },
presets: [
{
label: 'Last 7 days',
getValue: () => ({
from: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
to: new Date(),
}),
},
{
label: 'Last 30 days',
getValue: () => ({
from: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
to: new Date(),
}),
},
],
onChange: (value) => {
console.log('Date range:', value);
},
};

Types

DashboardTemplate

interface DashboardTemplate {
id: string;
name: string;
description?: string;
grid: DashboardGridItem[];
}

DashboardGridItem

interface DashboardGridItem extends ReactGridLayout.Layout {
card: DashboardCard;
}

DashboardFilter

interface DashboardFilter {
key: string;
type: DashboardFilterType;
label: string;
placeholder?: string;
value: DashboardFilterValue;
onChange?: (value: DashboardFilterValue) => void;
options?: { label: string; value: string | number | boolean }[];
presets?: { label: string; getValue: () => DateRange }[];
}

DashboardFilterValue

type DashboardFilterValue =
| string
| number
| boolean
| { from: Date; to: Date };

DashboardFilterType

enum DashboardFilterType {
TEXT = 'text',
SELECT = 'select',
DATE_RANGE = 'date-range',
NUMBER = 'number',
BOOLEAN = 'boolean',
}

DashboardCard

See DashboardCard section for full interface.

CardNumberType

type CardNumberType = 'number' | 'percentage' | 'currency';

CardSourceType

type CardSourceType = {
source: 'meta' | 'oneclickads' | 'api';
level?: 'adAccount' | 'campaign' | 'adSet' | 'ad';
};

DashboardCardType

type DashboardCardType =
| 'bigNumber'
| 'pieChart'
| 'barChart'
| 'lineChart'
| 'table'
| 'list';

CardVariant

type CardVariant = 'default' | 'dark' | 'light-green';

Complete Example

import { Dashboard } from '@ttoss/react-dashboard';
import type {
DashboardTemplate,
DashboardFilter,
} from '@ttoss/react-dashboard';
import { DashboardFilterType } from '@ttoss/react-dashboard';

const App = () => {
const selectedTemplate: DashboardTemplate = {
id: 'default',
name: 'Default Dashboard',
grid: [
{
i: 'revenue',
x: 0,
y: 0,
w: 4,
h: 4,
card: {
title: 'Total Revenue',
numberType: 'currency',
type: 'bigNumber',
sourceType: [{ source: 'api' }],
data: {
api: { total: 150000 },
},
trend: {
value: 15.5,
status: 'positive',
},
},
},
{
i: 'roas',
x: 4,
y: 0,
w: 4,
h: 4,
card: {
title: 'ROAS',
numberType: 'number',
type: 'bigNumber',
sourceType: [{ source: 'api' }],
data: {
api: { total: 3.5 },
},
variant: 'light-green',
},
},
],
};

// Optional: Keep templates array if you need to switch between templates
const templates: DashboardTemplate[] = [selectedTemplate];

const filters: DashboardFilter[] = [
{
key: 'date-range',
type: DashboardFilterType.DATE_RANGE,
label: 'Date Range',
value: {
from: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
to: new Date(),
},
presets: [
{
label: 'Last 7 days',
getValue: () => ({
from: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000),
to: new Date(),
}),
},
{
label: 'Last 30 days',
getValue: () => ({
from: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000),
to: new Date(),
}),
},
],
},
];

const handleFiltersChange = (updatedFilters: DashboardFilter[]) => {
console.log('Filters changed:', updatedFilters);
// Update your data fetching logic here
};

return (
<Dashboard
selectedTemplate={selectedTemplate}
templates={templates}
filters={filters}
loading={false}
onFiltersChange={handleFiltersChange}
/>
);
};

License

MIT