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