2025-06-07 23:41:25 +02:00
|
|
|
import React, { memo, useMemo, type MutableRefObject } from 'react';
|
|
|
|
|
import { SandpackPreview, SandpackProvider } from '@codesandbox/sandpack-react/unstyled';
|
2025-10-24 11:08:57 +02:00
|
|
|
import type {
|
|
|
|
|
SandpackProviderProps,
|
|
|
|
|
SandpackPreviewRef,
|
|
|
|
|
PreviewProps,
|
|
|
|
|
} from '@codesandbox/sandpack-react/unstyled';
|
2025-04-26 04:30:58 -04:00
|
|
|
import type { TStartupConfig } from 'librechat-data-provider';
|
2025-01-23 18:19:04 -05:00
|
|
|
import type { ArtifactFiles } from '~/common';
|
|
|
|
|
import { sharedFiles, sharedOptions } from '~/utils/artifacts';
|
2024-08-27 17:03:16 -04:00
|
|
|
|
|
|
|
|
export const ArtifactPreview = memo(function ({
|
2025-01-23 18:19:04 -05:00
|
|
|
files,
|
|
|
|
|
fileKey,
|
|
|
|
|
template,
|
2025-04-26 04:30:58 -04:00
|
|
|
sharedProps,
|
|
|
|
|
previewRef,
|
|
|
|
|
currentCode,
|
|
|
|
|
startupConfig,
|
2024-08-27 17:03:16 -04:00
|
|
|
}: {
|
2025-01-23 18:19:04 -05:00
|
|
|
files: ArtifactFiles;
|
|
|
|
|
fileKey: string;
|
|
|
|
|
template: SandpackProviderProps['template'];
|
|
|
|
|
sharedProps: Partial<SandpackProviderProps>;
|
2025-06-07 23:41:25 +02:00
|
|
|
previewRef: MutableRefObject<SandpackPreviewRef>;
|
2025-04-26 04:30:58 -04:00
|
|
|
currentCode?: string;
|
|
|
|
|
startupConfig?: TStartupConfig;
|
2024-08-27 17:03:16 -04:00
|
|
|
}) {
|
2025-01-23 18:19:04 -05:00
|
|
|
const artifactFiles = useMemo(() => {
|
|
|
|
|
if (Object.keys(files).length === 0) {
|
|
|
|
|
return files;
|
2024-08-27 17:03:16 -04:00
|
|
|
}
|
2025-01-23 18:19:04 -05:00
|
|
|
const code = currentCode ?? '';
|
|
|
|
|
if (!code) {
|
|
|
|
|
return files;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...files,
|
2025-06-07 23:41:25 +02:00
|
|
|
[fileKey]: { code },
|
2025-01-23 18:19:04 -05:00
|
|
|
};
|
|
|
|
|
}, [currentCode, files, fileKey]);
|
2025-03-05 16:03:54 -05:00
|
|
|
|
|
|
|
|
const options: typeof sharedOptions = useMemo(() => {
|
2025-04-26 04:30:58 -04:00
|
|
|
if (!startupConfig) {
|
2025-03-05 16:03:54 -05:00
|
|
|
return sharedOptions;
|
|
|
|
|
}
|
2025-06-07 23:41:25 +02:00
|
|
|
return {
|
2025-03-05 16:03:54 -05:00
|
|
|
...sharedOptions,
|
2025-04-26 04:30:58 -04:00
|
|
|
bundlerURL: template === 'static' ? startupConfig.staticBundlerURL : startupConfig.bundlerURL,
|
2025-03-05 16:03:54 -05:00
|
|
|
};
|
2025-04-26 04:30:58 -04:00
|
|
|
}, [startupConfig, template]);
|
2025-03-05 16:03:54 -05:00
|
|
|
|
2025-01-23 18:19:04 -05:00
|
|
|
if (Object.keys(artifactFiles).length === 0) {
|
2024-08-27 17:03:16 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-23 18:19:04 -05:00
|
|
|
return (
|
2024-08-27 17:03:16 -04:00
|
|
|
<SandpackProvider
|
2025-06-07 23:41:25 +02:00
|
|
|
files={{ ...artifactFiles, ...sharedFiles }}
|
2025-03-05 16:03:54 -05:00
|
|
|
options={options}
|
2024-08-27 17:03:16 -04:00
|
|
|
{...sharedProps}
|
|
|
|
|
template={template}
|
|
|
|
|
>
|
|
|
|
|
<SandpackPreview
|
|
|
|
|
showOpenInCodeSandbox={false}
|
|
|
|
|
showRefreshButton={false}
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
ref={previewRef}
|
|
|
|
|
/>
|
|
|
|
|
</SandpackProvider>
|
|
|
|
|
);
|
|
|
|
|
});
|