mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-20 18:30:15 +01:00
* v0.7.791 * feat: configuration via `SANDPACK_STATIC_BUNDLER_URL` env var and update bundlerURL logic in Artifact components * fix: update minimum length requirement for auth fields from 10 to 1 character
81 lines
1.9 KiB
TypeScript
81 lines
1.9 KiB
TypeScript
import React, { memo, useMemo } from 'react';
|
|
import {
|
|
SandpackPreview,
|
|
SandpackProvider,
|
|
SandpackProviderProps,
|
|
} from '@codesandbox/sandpack-react/unstyled';
|
|
import type { SandpackPreviewRef } from '@codesandbox/sandpack-react/unstyled';
|
|
import type { ArtifactFiles } from '~/common';
|
|
import { sharedFiles, sharedOptions } from '~/utils/artifacts';
|
|
import { useGetStartupConfig } from '~/data-provider';
|
|
import { useEditorContext } from '~/Providers';
|
|
|
|
export const ArtifactPreview = memo(function ({
|
|
files,
|
|
fileKey,
|
|
previewRef,
|
|
sharedProps,
|
|
template,
|
|
}: {
|
|
files: ArtifactFiles;
|
|
fileKey: string;
|
|
template: SandpackProviderProps['template'];
|
|
sharedProps: Partial<SandpackProviderProps>;
|
|
previewRef: React.MutableRefObject<SandpackPreviewRef>;
|
|
}) {
|
|
const { currentCode } = useEditorContext();
|
|
const { data: config } = useGetStartupConfig();
|
|
|
|
const artifactFiles = useMemo(() => {
|
|
if (Object.keys(files).length === 0) {
|
|
return files;
|
|
}
|
|
const code = currentCode ?? '';
|
|
if (!code) {
|
|
return files;
|
|
}
|
|
return {
|
|
...files,
|
|
[fileKey]: {
|
|
code,
|
|
},
|
|
};
|
|
}, [currentCode, files, fileKey]);
|
|
|
|
const options: typeof sharedOptions = useMemo(() => {
|
|
if (!config) {
|
|
return sharedOptions;
|
|
}
|
|
const _options: typeof sharedOptions = {
|
|
...sharedOptions,
|
|
bundlerURL: template === 'static' ? config.staticBundlerURL : config.bundlerURL,
|
|
};
|
|
|
|
return _options;
|
|
}, [config, template]);
|
|
|
|
console.log(options);
|
|
|
|
if (Object.keys(artifactFiles).length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<SandpackProvider
|
|
files={{
|
|
...artifactFiles,
|
|
...sharedFiles,
|
|
}}
|
|
options={options}
|
|
{...sharedProps}
|
|
template={template}
|
|
>
|
|
<SandpackPreview
|
|
showOpenInCodeSandbox={false}
|
|
showRefreshButton={false}
|
|
tabIndex={0}
|
|
ref={previewRef}
|
|
/>
|
|
</SandpackProvider>
|
|
);
|
|
});
|