Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
"superjson": "^2.2.6",
"tailwind-merge": "^3.4.0",
"tailwindcss": "^4.0.6",
"use-stick-to-bottom": "^1.1.1"
"use-stick-to-bottom": "^1.1.1",
"write-excel-file": "^4.1.1"
},
"devDependencies": {
"@tanstack/devtools-vite": "^0.3.11",
Expand Down
53 changes: 33 additions & 20 deletions apps/frontend/src/components/data-table-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { useMutation } from '@tanstack/react-query';
import { Copy, Download, Maximize2 } from 'lucide-react';
import type { ReactNode } from 'react';
import type { ColumnConditionalFormats } from '@nao/shared/conditional-formatting';
import type { DataExportFormat } from '@/components/export-data-menu';
import { ExportDataMenu } from '@/components/export-data-menu';
import { TableDisplay } from '@/components/tool-calls/display-table';
import { Button } from '@/components/ui/button';
import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog';
import { downloadCsv, tableToCsv, tableToTsv } from '@/lib/table-export';
import { tableToTsv } from '@/lib/table-export';
import { cn } from '@/lib/utils';
import { trpc } from '@/main';

Expand Down Expand Up @@ -44,10 +46,9 @@ export function DataTableCard({
}

const handleCopy = () => navigator.clipboard.writeText(tableToTsv(resolvedColumns, data));
const handleDownload = () => {
downloadCsv('table.csv', tableToCsv(resolvedColumns, data));
const handleExport = (format: DataExportFormat) => {
if (chatId) {
logDownload.mutate({ chatId, format: 'csv', title });
logDownload.mutate({ chatId, format, title });
}
};

Expand All @@ -66,15 +67,21 @@ export function DataTableCard({
>
<Copy className='size-3 text-muted-foreground/70' />
</Button>
<Button
variant='ghost-muted'
size='icon-xs'
className='hover:rounded-full hover:bg-accent/70'
onClick={handleDownload}
title='Download as CSV'
<ExportDataMenu
columns={resolvedColumns}
data={data}
filename={title || 'table'}
onExport={handleExport}
>
<Download className='size-3 text-muted-foreground/70' />
</Button>
<Button
variant='ghost-muted'
size='icon-xs'
className='hover:rounded-full hover:bg-accent/70'
title='Export data'
>
<Download className='size-3 text-muted-foreground/70' />
</Button>
</ExportDataMenu>
<Button
variant='ghost-muted'
size='icon-xs'
Expand Down Expand Up @@ -110,15 +117,21 @@ export function DataTableCard({
>
<Copy className='size-3.5' />
</Button>
<Button
variant='ghost-muted'
size='icon-xs'
className='hover:rounded-full'
onClick={handleDownload}
title='Download as CSV'
<ExportDataMenu
columns={resolvedColumns}
data={data}
filename={title || 'table'}
onExport={handleExport}
>
<Download className='size-3.5' />
</Button>
<Button
variant='ghost-muted'
size='icon-xs'
className='hover:rounded-full'
title='Export data'
>
<Download className='size-3.5' />
</Button>
</ExportDataMenu>
</div>
<TableDisplay
data={data}
Expand Down
59 changes: 59 additions & 0 deletions apps/frontend/src/components/export-data-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { FileSpreadsheet } from 'lucide-react';
import { useState } from 'react';
import type { ReactElement } from 'react';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { useToolCallActions } from '@/contexts/tool-call-actions';
import { downloadCsv, downloadXlsx, tableToCsv } from '@/lib/table-export';

export type DataExportFormat = 'csv' | 'xlsx';

interface ExportDataMenuProps {
columns: string[];
data: Record<string, unknown>[];
filename: string;
onExport?: (format: DataExportFormat) => void;
align?: 'start' | 'end';
children: ReactElement;
}

export function ExportDataMenu({ columns, data, filename, onExport, align = 'end', children }: ExportDataMenuProps) {
const [open, setOpen] = useState(false);
const toolCallActions = useToolCallActions();

const handleOpenChange = (next: boolean) => {
setOpen(next);
toolCallActions?.setMenuOpen(next);
};

const handleExport = async (format: DataExportFormat) => {
if (format === 'csv') {
downloadCsv(`${filename}.csv`, tableToCsv(columns, data));
} else {
await downloadXlsx(`${filename}.xlsx`, columns, data);
}
onExport?.(format);
};

return (
<DropdownMenu open={open} onOpenChange={handleOpenChange}>
<DropdownMenuTrigger asChild onClick={(event) => event.stopPropagation()}>
{children}
</DropdownMenuTrigger>
<DropdownMenuContent align={align}>
<DropdownMenuItem onSelect={() => handleExport('csv')}>
<FileSpreadsheet />
<span>CSV</span>
</DropdownMenuItem>
<DropdownMenuItem onSelect={() => handleExport('xlsx')}>
<FileSpreadsheet />
<span>Excel (XLSX)</span>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}
66 changes: 38 additions & 28 deletions apps/frontend/src/components/tool-calls/display-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { ChartConfig } from '../ui/chart';
import type { executeSql } from '@nao/shared/tools';
import type { UIMessage } from '@nao/backend/chat';
import type { DateRange } from '@/lib/charts.utils';
import type { DataExportFormat } from '@/components/export-data-menu';
import { trpc } from '@/main';
import { findStoryIds } from '@/lib/story.utils';
import {
Expand All @@ -36,7 +37,7 @@ import { useChatId } from '@/hooks/use-chat-id';
import { useSidePanel } from '@/contexts/side-panel';
import { StoryViewer } from '@/components/side-panel/story-viewer';
import { cn } from '@/lib/utils';
import { downloadCsv, tableToCsv } from '@/lib/table-export';
import { ExportDataMenu } from '@/components/export-data-menu';

const Colors = ['var(--chart-1)', 'var(--chart-2)', 'var(--chart-3)', 'var(--chart-4)', 'var(--chart-5)'];
const EMPTY_MESSAGES: UIMessage[] = [];
Expand Down Expand Up @@ -97,23 +98,8 @@ export const DisplayChartToolCall = ({
const sourceData = sourceQuery?.output ?? null;
const sqlQuery = sourceQuery?.input?.sql_query;

const handleDownload = async () => {
if (!chartConfig || !sourceData) {
return;
}
if (viewMode !== 'chart') {
downloadCsv(
`${chartConfig.title || 'chart'}.csv`,
tableToCsv(sourceData.columns, sourceData.data as Record<string, unknown>[]),
);
if (chatId) {
logDownload.mutate({
chatId,
format: 'csv',
queryId: chartConfig.query_id,
title: chartConfig.title,
});
}
const handleDownloadPng = async () => {
if (!chartConfig) {
return;
}

Expand All @@ -131,6 +117,12 @@ export const DisplayChartToolCall = ({
}
};

const handleExportData = (format: DataExportFormat) => {
if (chatId) {
logDownload.mutate({ chatId, format, queryId: chartConfig?.query_id, title: chartConfig?.title });
}
};

const filteredData = useMemo(() => {
if (!sourceData?.data || !chartConfig) {
return [];
Expand Down Expand Up @@ -310,17 +302,35 @@ export const DisplayChartToolCall = ({
</Button>
)}

{(viewMode !== 'chart' || chartConfig.chart_type != 'kpi_card') && (
<Button
variant='ghost-muted'
size='icon-xs'
className='rounded-full hover:bg-accent/70'
onClick={handleDownload}
disabled={isDownloading}
title={viewMode === 'chart' ? 'Download as PNG' : 'Download data as CSV'}
{viewMode === 'chart' ? (
chartConfig.chart_type != 'kpi_card' && (
<Button
variant='ghost-muted'
size='icon-xs'
className='rounded-full hover:bg-accent/70'
onClick={handleDownloadPng}
disabled={isDownloading}
title='Download as PNG'
>
<Download className='size-3 text-muted-foreground/70' strokeWidth={2.25} />
</Button>
)
) : (
<ExportDataMenu
columns={sourceData.columns}
data={sourceData.data as Record<string, unknown>[]}
filename={chartConfig.title || 'chart'}
onExport={handleExportData}
>
<Download className='size-3 text-muted-foreground/70' strokeWidth={2.25} />
</Button>
<Button
variant='ghost-muted'
size='icon-xs'
className='rounded-full hover:bg-accent/70'
title='Export data'
>
<Download className='size-3 text-muted-foreground/70' strokeWidth={2.25} />
</Button>
</ExportDataMenu>
)}
</div>
{isEditable && (
Expand Down
43 changes: 24 additions & 19 deletions apps/frontend/src/components/tool-calls/execute-sql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import { ToolCallWrapper } from './tool-call-wrapper';
import { TableFormatEditDialog } from './display-table-edit-dialog';
import { SqlQueryDisplay } from './sql-query-display';
import { SqlResultDisplay } from './sql-result-display';
import type { ActionButton } from './tool-call-wrapper';
import type { ToolCallComponentProps } from '.';
import type { ColumnConditionalFormats } from '@nao/shared/conditional-formatting';
import type { DataExportFormat } from '@/components/export-data-menu';
import { useOptionalAgentContext } from '@/contexts/agent.provider';
import { useSidePanel } from '@/contexts/side-panel';
import { useToolCallContext } from '@/contexts/tool-call';
import { SidePanelContent } from '@/components/side-panel/sql-editor';
import { downloadCsv, tableToCsv } from '@/lib/table-export';
import { trpc } from '@/main';

type ViewMode = 'results' | 'query';
Expand All @@ -27,7 +28,13 @@ export const ExecuteSqlToolCall = ({
const chatId = useOptionalAgentContext()?.chatId;
const logDownload = useMutation(trpc.analyticsEvent.logChatDownload.mutationOptions());

const actions = [
const handleExport = (format: DataExportFormat) => {
if (chatId) {
logDownload.mutate({ chatId, format, queryId: toolCallId, title: input?.name });
}
};

const actions: ActionButton[] = [
{
id: 'results',
label: <TableIcon className='size-3 text-muted-foreground/70' strokeWidth={2.25} />,
Expand Down Expand Up @@ -64,23 +71,21 @@ export const ExecuteSqlToolCall = ({
},
title: 'Copy query',
},
{
id: 'download',
label: <Download className='size-3 text-muted-foreground/70' strokeWidth={2.25} />,
onClick: () => {
if (!output) {
return;
}
downloadCsv(
`${input?.name || 'query'}.csv`,
tableToCsv(output.columns, output.data as Record<string, unknown>[]),
);
if (chatId) {
logDownload.mutate({ chatId, format: 'csv', queryId: toolCallId, title: input?.name });
}
},
title: 'Download results as CSV',
},
...(output
? [
{
id: 'download',
label: <Download className='size-3 text-muted-foreground/70' strokeWidth={2.25} />,
title: 'Export results',
export: {
columns: output.columns,
data: output.data as Record<string, unknown>[],
filename: input?.name || 'query',
onExport: handleExport,
},
},
]
: []),
{
id: 'expand',
label: <ArrowUpRight className='size-3 text-muted-foreground/70' strokeWidth={2.25} />,
Expand Down
Loading
Loading