2025-09-21 12:03:29 +01:00
|
|
|
import { Artifact } from '@/components/create-artifact';
|
2025-02-03 13:33:48 +05:30
|
|
|
import {
|
|
|
|
|
CopyIcon,
|
|
|
|
|
LineChartIcon,
|
|
|
|
|
RedoIcon,
|
|
|
|
|
SparklesIcon,
|
|
|
|
|
UndoIcon,
|
2025-09-21 12:03:29 +01:00
|
|
|
} from '@/components/icons';
|
|
|
|
|
import { SpreadsheetEditor } from '@/components/sheet-editor';
|
|
|
|
|
import { parse, unparse } from 'papaparse';
|
|
|
|
|
import { toast } from 'sonner';
|
2025-02-03 13:33:48 +05:30
|
|
|
|
|
|
|
|
type Metadata = any;
|
|
|
|
|
|
2025-09-21 12:03:29 +01:00
|
|
|
export const sheetArtifact = new Artifact<'sheet', Metadata>({
|
|
|
|
|
kind: 'sheet',
|
|
|
|
|
description: 'Useful for working with spreadsheets',
|
|
|
|
|
initialize: async () => {},
|
2025-02-13 08:25:57 -08:00
|
|
|
onStreamPart: ({ setArtifact, streamPart }) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
if (streamPart.type === 'data-sheetDelta') {
|
2025-02-13 08:25:57 -08:00
|
|
|
setArtifact((draftArtifact) => ({
|
|
|
|
|
...draftArtifact,
|
2025-07-03 02:26:34 -07:00
|
|
|
content: streamPart.data,
|
2025-02-03 13:33:48 +05:30
|
|
|
isVisible: true,
|
2025-09-21 12:03:29 +01:00
|
|
|
status: 'streaming',
|
2025-02-03 13:33:48 +05:30
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-09-21 12:03:29 +01:00
|
|
|
content: ({
|
|
|
|
|
content,
|
|
|
|
|
currentVersionIndex,
|
|
|
|
|
isCurrentVersion,
|
|
|
|
|
onSaveContent,
|
|
|
|
|
status,
|
|
|
|
|
}) => {
|
2025-02-03 13:33:48 +05:30
|
|
|
return (
|
|
|
|
|
<SpreadsheetEditor
|
|
|
|
|
content={content}
|
|
|
|
|
currentVersionIndex={currentVersionIndex}
|
2025-09-21 12:03:29 +01:00
|
|
|
isCurrentVersion={isCurrentVersion}
|
2025-02-03 13:33:48 +05:30
|
|
|
saveContent={onSaveContent}
|
|
|
|
|
status={status}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
actions: [
|
|
|
|
|
{
|
|
|
|
|
icon: <UndoIcon size={18} />,
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'View Previous version',
|
2025-02-03 13:33:48 +05:30
|
|
|
onClick: ({ handleVersionChange }) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
handleVersionChange('prev');
|
2025-02-03 13:33:48 +05:30
|
|
|
},
|
|
|
|
|
isDisabled: ({ currentVersionIndex }) => {
|
|
|
|
|
if (currentVersionIndex === 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <RedoIcon size={18} />,
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'View Next version',
|
2025-02-03 13:33:48 +05:30
|
|
|
onClick: ({ handleVersionChange }) => {
|
2025-09-21 12:03:29 +01:00
|
|
|
handleVersionChange('next');
|
2025-02-03 13:33:48 +05:30
|
|
|
},
|
|
|
|
|
isDisabled: ({ isCurrentVersion }) => {
|
|
|
|
|
if (isCurrentVersion) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
icon: <CopyIcon />,
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'Copy as .csv',
|
2025-02-03 13:33:48 +05:30
|
|
|
onClick: ({ content }) => {
|
|
|
|
|
const parsed = parse<string[]>(content, { skipEmptyLines: true });
|
|
|
|
|
|
|
|
|
|
const nonEmptyRows = parsed.data.filter((row) =>
|
2025-09-21 12:03:29 +01:00
|
|
|
row.some((cell) => cell.trim() !== ''),
|
2025-02-03 13:33:48 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const cleanedCsv = unparse(nonEmptyRows);
|
|
|
|
|
|
|
|
|
|
navigator.clipboard.writeText(cleanedCsv);
|
2025-09-21 12:03:29 +01:00
|
|
|
toast.success('Copied csv to clipboard!');
|
2025-02-03 13:33:48 +05:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
toolbar: [
|
|
|
|
|
{
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'Format and clean data',
|
2025-02-03 13:33:48 +05:30
|
|
|
icon: <SparklesIcon />,
|
2025-07-03 02:26:34 -07:00
|
|
|
onClick: ({ sendMessage }) => {
|
|
|
|
|
sendMessage({
|
2025-09-21 12:03:29 +01:00
|
|
|
role: 'user',
|
2025-07-03 02:26:34 -07:00
|
|
|
parts: [
|
2025-09-21 12:03:29 +01:00
|
|
|
{ type: 'text', text: 'Can you please format and clean the data?' },
|
2025-07-03 02:26:34 -07:00
|
|
|
],
|
2025-02-03 13:33:48 +05:30
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-09-21 12:03:29 +01:00
|
|
|
description: 'Analyze and visualize data',
|
2025-02-03 13:33:48 +05:30
|
|
|
icon: <LineChartIcon />,
|
2025-07-03 02:26:34 -07:00
|
|
|
onClick: ({ sendMessage }) => {
|
|
|
|
|
sendMessage({
|
2025-09-21 12:03:29 +01:00
|
|
|
role: 'user',
|
2025-07-03 02:26:34 -07:00
|
|
|
parts: [
|
|
|
|
|
{
|
2025-09-21 12:03:29 +01:00
|
|
|
type: 'text',
|
|
|
|
|
text: 'Can you please analyze and visualize the data by creating a new code artifact in python?',
|
2025-07-03 02:26:34 -07:00
|
|
|
},
|
|
|
|
|
],
|
2025-02-03 13:33:48 +05:30
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|