2024-07-10 16:44:39 +00:00
|
|
|
import { useStore } from '@nanostores/react';
|
2024-07-17 18:54:46 +00:00
|
|
|
import { AnimatePresence, motion } from 'framer-motion';
|
|
|
|
import { computed } from 'nanostores';
|
2024-07-18 09:10:12 +00:00
|
|
|
import { memo, useEffect, useRef, useState } from 'react';
|
2024-07-17 18:54:46 +00:00
|
|
|
import { createHighlighter, type BundledLanguage, type BundledTheme, type HighlighterGeneric } from 'shiki';
|
2024-07-24 15:43:32 +00:00
|
|
|
import type { ActionState } from '~/lib/runtime/action-runner';
|
|
|
|
import { workbenchStore } from '~/lib/stores/workbench';
|
|
|
|
import { classNames } from '~/utils/classNames';
|
|
|
|
import { cubicEasingFn } from '~/utils/easings';
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
const highlighterOptions = {
|
|
|
|
langs: ['shell'],
|
|
|
|
themes: ['light-plus', 'dark-plus'],
|
|
|
|
};
|
|
|
|
|
|
|
|
const shellHighlighter: HighlighterGeneric<BundledLanguage, BundledTheme> =
|
|
|
|
import.meta.hot?.data.shellHighlighter ?? (await createHighlighter(highlighterOptions));
|
|
|
|
|
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.data.shellHighlighter = shellHighlighter;
|
|
|
|
}
|
2024-07-10 16:44:39 +00:00
|
|
|
|
|
|
|
interface ArtifactProps {
|
|
|
|
messageId: string;
|
|
|
|
}
|
|
|
|
|
2024-07-22 15:40:28 +00:00
|
|
|
export const Artifact = memo(({ messageId }: ArtifactProps) => {
|
2024-07-18 09:10:12 +00:00
|
|
|
const userToggledActions = useRef(false);
|
2024-07-17 18:54:46 +00:00
|
|
|
const [showActions, setShowActions] = useState(false);
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-07-17 18:54:46 +00:00
|
|
|
const artifacts = useStore(workbenchStore.artifacts);
|
2024-07-22 15:40:28 +00:00
|
|
|
const artifact = artifacts[messageId];
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
const actions = useStore(
|
2024-07-22 15:40:28 +00:00
|
|
|
computed(artifact.runner.actions, (actions) => {
|
2024-07-17 18:54:46 +00:00
|
|
|
return Object.values(actions);
|
|
|
|
}),
|
|
|
|
);
|
2024-07-10 16:44:39 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
const toggleActions = () => {
|
|
|
|
userToggledActions.current = true;
|
|
|
|
setShowActions(!showActions);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (actions.length && !showActions && !userToggledActions.current) {
|
|
|
|
setShowActions(true);
|
|
|
|
}
|
|
|
|
}, [actions]);
|
|
|
|
|
2024-07-10 16:44:39 +00:00
|
|
|
return (
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="artifact border border-bolt-elements-borderColor flex flex-col overflow-hidden rounded-lg w-full transition-border duration-150">
|
2024-07-17 18:54:46 +00:00
|
|
|
<div className="flex">
|
|
|
|
<button
|
2024-08-08 13:56:36 +00:00
|
|
|
className="flex items-stretch bg-bolt-elements-artifacts-background hover:bg-bolt-elements-artifacts-backgroundHover w-full overflow-hidden"
|
2024-07-17 18:54:46 +00:00
|
|
|
onClick={() => {
|
|
|
|
const showWorkbench = workbenchStore.showWorkbench.get();
|
|
|
|
workbenchStore.showWorkbench.set(!showWorkbench);
|
|
|
|
}}
|
|
|
|
>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="px-5 p-3.5 w-full text-left">
|
|
|
|
<div className="w-full text-bolt-elements-textPrimary font-medium leading-5 text-sm">{artifact?.title}</div>
|
|
|
|
<div className="w-full w-full text-bolt-elements-textSecondary text-xs mt-0.5">Click to open Workbench</div>
|
2024-07-17 18:54:46 +00:00
|
|
|
</div>
|
|
|
|
</button>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="bg-bolt-elements-artifacts-borderColor w-[1px]" />
|
2024-07-17 18:54:46 +00:00
|
|
|
<AnimatePresence>
|
|
|
|
{actions.length && (
|
|
|
|
<motion.button
|
|
|
|
initial={{ width: 0 }}
|
|
|
|
animate={{ width: 'auto' }}
|
|
|
|
exit={{ width: 0 }}
|
|
|
|
transition={{ duration: 0.15, ease: cubicEasingFn }}
|
2024-08-08 13:56:36 +00:00
|
|
|
className="bg-bolt-elements-artifacts-background hover:bg-bolt-elements-artifacts-backgroundHover"
|
2024-07-18 09:10:12 +00:00
|
|
|
onClick={toggleActions}
|
2024-07-17 18:54:46 +00:00
|
|
|
>
|
|
|
|
<div className="p-4">
|
|
|
|
<div className={showActions ? 'i-ph:caret-up-bold' : 'i-ph:caret-down-bold'}></div>
|
|
|
|
</div>
|
|
|
|
</motion.button>
|
|
|
|
)}
|
|
|
|
</AnimatePresence>
|
2024-07-10 16:44:39 +00:00
|
|
|
</div>
|
2024-07-17 18:54:46 +00:00
|
|
|
<AnimatePresence>
|
|
|
|
{showActions && actions.length > 0 && (
|
|
|
|
<motion.div
|
|
|
|
className="actions"
|
|
|
|
initial={{ height: 0 }}
|
|
|
|
animate={{ height: 'auto' }}
|
|
|
|
exit={{ height: '0px' }}
|
|
|
|
transition={{ duration: 0.15 }}
|
|
|
|
>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="bg-bolt-elements-artifacts-borderColor h-[1px]" />
|
|
|
|
<div className="p-5 text-left bg-bolt-elements-actions-background">
|
2024-07-22 15:40:28 +00:00
|
|
|
<ActionList actions={actions} />
|
2024-07-17 18:54:46 +00:00
|
|
|
</div>
|
|
|
|
</motion.div>
|
|
|
|
)}
|
|
|
|
</AnimatePresence>
|
|
|
|
</div>
|
|
|
|
);
|
2024-07-18 09:10:12 +00:00
|
|
|
});
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
interface ShellCodeBlockProps {
|
|
|
|
classsName?: string;
|
|
|
|
code: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ShellCodeBlock({ classsName, code }: ShellCodeBlockProps) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={classNames('text-xs', classsName)}
|
2024-08-08 13:56:36 +00:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: shellHighlighter.codeToHtml(code, {
|
|
|
|
lang: 'shell',
|
|
|
|
theme: 'dark-plus',
|
|
|
|
}),
|
|
|
|
}}
|
2024-07-17 18:54:46 +00:00
|
|
|
></div>
|
2024-07-10 16:44:39 +00:00
|
|
|
);
|
|
|
|
}
|
2024-07-22 15:40:28 +00:00
|
|
|
|
|
|
|
interface ActionListProps {
|
|
|
|
actions: ActionState[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const actionVariants = {
|
|
|
|
hidden: { opacity: 0, y: 20 },
|
|
|
|
visible: { opacity: 1, y: 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
const ActionList = memo(({ actions }: ActionListProps) => {
|
|
|
|
return (
|
|
|
|
<motion.div initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.15 }}>
|
|
|
|
<ul className="list-none space-y-2.5">
|
|
|
|
{actions.map((action, index) => {
|
|
|
|
const { status, type, content } = action;
|
2024-08-08 13:56:36 +00:00
|
|
|
const isLast = index === actions.length - 1;
|
2024-07-22 15:40:28 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<motion.li
|
|
|
|
key={index}
|
|
|
|
variants={actionVariants}
|
|
|
|
initial="hidden"
|
|
|
|
animate="visible"
|
|
|
|
transition={{
|
|
|
|
duration: 0.2,
|
|
|
|
ease: cubicEasingFn,
|
|
|
|
}}
|
|
|
|
>
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="flex items-center gap-1.5 text-sm">
|
|
|
|
<div className={classNames('text-lg', getIconColor(action.status))}>
|
2024-07-22 15:40:28 +00:00
|
|
|
{status === 'running' ? (
|
|
|
|
<div className="i-svg-spinners:90-ring-with-bg"></div>
|
|
|
|
) : status === 'pending' ? (
|
|
|
|
<div className="i-ph:circle-duotone"></div>
|
|
|
|
) : status === 'complete' ? (
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="i-ph:check"></div>
|
2024-07-22 15:40:28 +00:00
|
|
|
) : status === 'failed' || status === 'aborted' ? (
|
2024-08-08 13:56:36 +00:00
|
|
|
<div className="i-ph:x"></div>
|
2024-07-22 15:40:28 +00:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
{type === 'file' ? (
|
|
|
|
<div>
|
2024-08-08 13:56:36 +00:00
|
|
|
Create{' '}
|
|
|
|
<code className="bg-bolt-elements-artifacts-inlineCode-background text-bolt-elements-artifacts-inlineCode-text px-1.5 py-1 rounded-md">
|
|
|
|
{action.filePath}
|
|
|
|
</code>
|
2024-07-22 15:40:28 +00:00
|
|
|
</div>
|
|
|
|
) : type === 'shell' ? (
|
|
|
|
<div className="flex items-center w-full min-h-[28px]">
|
|
|
|
<span className="flex-1">Run command</span>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</div>
|
2024-08-08 13:56:36 +00:00
|
|
|
{type === 'shell' && (
|
|
|
|
<ShellCodeBlock
|
|
|
|
classsName={classNames('mt-1', {
|
|
|
|
'mb-3.5': !isLast,
|
|
|
|
})}
|
|
|
|
code={content}
|
|
|
|
/>
|
|
|
|
)}
|
2024-07-22 15:40:28 +00:00
|
|
|
</motion.li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</motion.div>
|
|
|
|
);
|
|
|
|
});
|
2024-08-08 13:56:36 +00:00
|
|
|
|
|
|
|
function getIconColor(status: ActionState['status']) {
|
|
|
|
switch (status) {
|
|
|
|
case 'pending': {
|
|
|
|
return 'text-bolt-elements-textTertiary';
|
|
|
|
}
|
|
|
|
case 'running': {
|
|
|
|
return 'text-bolt-elements-loader-progress';
|
|
|
|
}
|
|
|
|
case 'complete': {
|
|
|
|
return 'text-bolt-elements-icon-success';
|
|
|
|
}
|
|
|
|
case 'aborted': {
|
|
|
|
return 'text-bolt-elements-textSecondary';
|
|
|
|
}
|
|
|
|
case 'failed': {
|
|
|
|
return 'text-bolt-elements-icon-error';
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|