mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-16 16:30:15 +01:00
🔮 fix: Artifacts readOnly to Re-render when Expected (#8954)
This commit is contained in:
parent
9ca1847535
commit
4eeadddfe6
4 changed files with 14 additions and 21 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import debounce from 'lodash/debounce';
|
import debounce from 'lodash/debounce';
|
||||||
import React, { memo, useEffect, useMemo, useCallback, useState } from 'react';
|
import React, { useMemo, useState, useEffect, useCallback } from 'react';
|
||||||
import {
|
import {
|
||||||
useSandpack,
|
useSandpack,
|
||||||
SandpackCodeEditor,
|
SandpackCodeEditor,
|
||||||
|
|
@ -10,8 +10,8 @@ import type { SandpackBundlerFile } from '@codesandbox/sandpack-client';
|
||||||
import type { CodeEditorRef } from '@codesandbox/sandpack-react';
|
import type { CodeEditorRef } from '@codesandbox/sandpack-react';
|
||||||
import type { ArtifactFiles, Artifact } from '~/common';
|
import type { ArtifactFiles, Artifact } from '~/common';
|
||||||
import { useEditArtifact, useGetStartupConfig } from '~/data-provider';
|
import { useEditArtifact, useGetStartupConfig } from '~/data-provider';
|
||||||
|
import { useEditorContext, useArtifactsContext } from '~/Providers';
|
||||||
import { sharedFiles, sharedOptions } from '~/utils/artifacts';
|
import { sharedFiles, sharedOptions } from '~/utils/artifacts';
|
||||||
import { useEditorContext } from '~/Providers';
|
|
||||||
|
|
||||||
const createDebouncedMutation = (
|
const createDebouncedMutation = (
|
||||||
callback: (params: {
|
callback: (params: {
|
||||||
|
|
@ -29,7 +29,7 @@ const CodeEditor = ({
|
||||||
editorRef,
|
editorRef,
|
||||||
}: {
|
}: {
|
||||||
fileKey: string;
|
fileKey: string;
|
||||||
readOnly: boolean;
|
readOnly?: boolean;
|
||||||
artifact: Artifact;
|
artifact: Artifact;
|
||||||
editorRef: React.MutableRefObject<CodeEditorRef>;
|
editorRef: React.MutableRefObject<CodeEditorRef>;
|
||||||
}) => {
|
}) => {
|
||||||
|
|
@ -112,33 +112,32 @@ const CodeEditor = ({
|
||||||
<SandpackCodeEditor
|
<SandpackCodeEditor
|
||||||
ref={editorRef}
|
ref={editorRef}
|
||||||
showTabs={false}
|
showTabs={false}
|
||||||
readOnly={readOnly}
|
|
||||||
showRunButton={false}
|
showRunButton={false}
|
||||||
showLineNumbers={true}
|
showLineNumbers={true}
|
||||||
showInlineErrors={true}
|
showInlineErrors={true}
|
||||||
|
readOnly={readOnly === true}
|
||||||
className="hljs language-javascript bg-black"
|
className="hljs language-javascript bg-black"
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ArtifactCodeEditor = memo(function ({
|
export const ArtifactCodeEditor = function ({
|
||||||
files,
|
files,
|
||||||
fileKey,
|
fileKey,
|
||||||
template,
|
template,
|
||||||
artifact,
|
artifact,
|
||||||
editorRef,
|
editorRef,
|
||||||
sharedProps,
|
sharedProps,
|
||||||
isSubmitting,
|
|
||||||
}: {
|
}: {
|
||||||
fileKey: string;
|
fileKey: string;
|
||||||
artifact: Artifact;
|
artifact: Artifact;
|
||||||
files: ArtifactFiles;
|
files: ArtifactFiles;
|
||||||
isSubmitting: boolean;
|
|
||||||
template: SandpackProviderProps['template'];
|
template: SandpackProviderProps['template'];
|
||||||
sharedProps: Partial<SandpackProviderProps>;
|
sharedProps: Partial<SandpackProviderProps>;
|
||||||
editorRef: React.MutableRefObject<CodeEditorRef>;
|
editorRef: React.MutableRefObject<CodeEditorRef>;
|
||||||
}) {
|
}) {
|
||||||
const { data: config } = useGetStartupConfig();
|
const { data: config } = useGetStartupConfig();
|
||||||
|
const { isSubmitting } = useArtifactsContext();
|
||||||
const options: typeof sharedOptions = useMemo(() => {
|
const options: typeof sharedOptions = useMemo(() => {
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return sharedOptions;
|
return sharedOptions;
|
||||||
|
|
@ -148,6 +147,10 @@ export const ArtifactCodeEditor = memo(function ({
|
||||||
bundlerURL: template === 'static' ? config.staticBundlerURL : config.bundlerURL,
|
bundlerURL: template === 'static' ? config.staticBundlerURL : config.bundlerURL,
|
||||||
};
|
};
|
||||||
}, [config, template]);
|
}, [config, template]);
|
||||||
|
const [readOnly, setReadOnly] = useState(isSubmitting ?? false);
|
||||||
|
useEffect(() => {
|
||||||
|
setReadOnly(isSubmitting ?? false);
|
||||||
|
}, [isSubmitting]);
|
||||||
|
|
||||||
if (Object.keys(files).length === 0) {
|
if (Object.keys(files).length === 0) {
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -164,12 +167,7 @@ export const ArtifactCodeEditor = memo(function ({
|
||||||
{...sharedProps}
|
{...sharedProps}
|
||||||
template={template}
|
template={template}
|
||||||
>
|
>
|
||||||
<CodeEditor
|
<CodeEditor fileKey={fileKey} artifact={artifact} editorRef={editorRef} readOnly={readOnly} />
|
||||||
editorRef={editorRef}
|
|
||||||
fileKey={fileKey}
|
|
||||||
readOnly={isSubmitting}
|
|
||||||
artifact={artifact}
|
|
||||||
/>
|
|
||||||
</StyledProvider>
|
</StyledProvider>
|
||||||
);
|
);
|
||||||
});
|
};
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,12 @@ import { useRef, useEffect } from 'react';
|
||||||
import * as Tabs from '@radix-ui/react-tabs';
|
import * as Tabs from '@radix-ui/react-tabs';
|
||||||
import type { SandpackPreviewRef, CodeEditorRef } from '@codesandbox/sandpack-react';
|
import type { SandpackPreviewRef, CodeEditorRef } from '@codesandbox/sandpack-react';
|
||||||
import type { Artifact } from '~/common';
|
import type { Artifact } from '~/common';
|
||||||
|
import { useEditorContext, useArtifactsContext } from '~/Providers';
|
||||||
import useArtifactProps from '~/hooks/Artifacts/useArtifactProps';
|
import useArtifactProps from '~/hooks/Artifacts/useArtifactProps';
|
||||||
import { useAutoScroll } from '~/hooks/Artifacts/useAutoScroll';
|
import { useAutoScroll } from '~/hooks/Artifacts/useAutoScroll';
|
||||||
import { ArtifactCodeEditor } from './ArtifactCodeEditor';
|
import { ArtifactCodeEditor } from './ArtifactCodeEditor';
|
||||||
import { useGetStartupConfig } from '~/data-provider';
|
import { useGetStartupConfig } from '~/data-provider';
|
||||||
import { ArtifactPreview } from './ArtifactPreview';
|
import { ArtifactPreview } from './ArtifactPreview';
|
||||||
import { useEditorContext } from '~/Providers';
|
|
||||||
import { cn } from '~/utils';
|
import { cn } from '~/utils';
|
||||||
|
|
||||||
export default function ArtifactTabs({
|
export default function ArtifactTabs({
|
||||||
|
|
@ -15,14 +15,13 @@ export default function ArtifactTabs({
|
||||||
isMermaid,
|
isMermaid,
|
||||||
editorRef,
|
editorRef,
|
||||||
previewRef,
|
previewRef,
|
||||||
isSubmitting,
|
|
||||||
}: {
|
}: {
|
||||||
artifact: Artifact;
|
artifact: Artifact;
|
||||||
isMermaid: boolean;
|
isMermaid: boolean;
|
||||||
isSubmitting: boolean;
|
|
||||||
editorRef: React.MutableRefObject<CodeEditorRef>;
|
editorRef: React.MutableRefObject<CodeEditorRef>;
|
||||||
previewRef: React.MutableRefObject<SandpackPreviewRef>;
|
previewRef: React.MutableRefObject<SandpackPreviewRef>;
|
||||||
}) {
|
}) {
|
||||||
|
const { isSubmitting } = useArtifactsContext();
|
||||||
const { currentCode, setCurrentCode } = useEditorContext();
|
const { currentCode, setCurrentCode } = useEditorContext();
|
||||||
const { data: startupConfig } = useGetStartupConfig();
|
const { data: startupConfig } = useGetStartupConfig();
|
||||||
const lastIdRef = useRef<string | null>(null);
|
const lastIdRef = useRef<string | null>(null);
|
||||||
|
|
@ -52,7 +51,6 @@ export default function ArtifactTabs({
|
||||||
artifact={artifact}
|
artifact={artifact}
|
||||||
editorRef={editorRef}
|
editorRef={editorRef}
|
||||||
sharedProps={sharedProps}
|
sharedProps={sharedProps}
|
||||||
isSubmitting={isSubmitting}
|
|
||||||
/>
|
/>
|
||||||
</Tabs.Content>
|
</Tabs.Content>
|
||||||
<Tabs.Content
|
<Tabs.Content
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@ export default function Artifacts() {
|
||||||
isMermaid,
|
isMermaid,
|
||||||
setActiveTab,
|
setActiveTab,
|
||||||
currentIndex,
|
currentIndex,
|
||||||
isSubmitting,
|
|
||||||
cycleArtifact,
|
cycleArtifact,
|
||||||
currentArtifact,
|
currentArtifact,
|
||||||
orderedArtifactIds,
|
orderedArtifactIds,
|
||||||
|
|
@ -116,7 +115,6 @@ export default function Artifacts() {
|
||||||
<ArtifactTabs
|
<ArtifactTabs
|
||||||
isMermaid={isMermaid}
|
isMermaid={isMermaid}
|
||||||
artifact={currentArtifact}
|
artifact={currentArtifact}
|
||||||
isSubmitting={isSubmitting}
|
|
||||||
editorRef={editorRef as React.MutableRefObject<CodeEditorRef>}
|
editorRef={editorRef as React.MutableRefObject<CodeEditorRef>}
|
||||||
previewRef={previewRef as React.MutableRefObject<SandpackPreviewRef>}
|
previewRef={previewRef as React.MutableRefObject<SandpackPreviewRef>}
|
||||||
/>
|
/>
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,6 @@ export default function useArtifacts() {
|
||||||
isMermaid,
|
isMermaid,
|
||||||
setActiveTab,
|
setActiveTab,
|
||||||
currentIndex,
|
currentIndex,
|
||||||
isSubmitting,
|
|
||||||
cycleArtifact,
|
cycleArtifact,
|
||||||
currentArtifact,
|
currentArtifact,
|
||||||
orderedArtifactIds,
|
orderedArtifactIds,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue