mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-02-11 12:04:24 +01:00
feat: refresh button
This commit is contained in:
parent
e39a3bafec
commit
6a8d4e43db
2 changed files with 44 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ import React, { useMemo, memo } from 'react';
|
||||||
import { Sandpack } from '@codesandbox/sandpack-react';
|
import { Sandpack } from '@codesandbox/sandpack-react';
|
||||||
import { removeNullishValues } from 'librechat-data-provider';
|
import { removeNullishValues } from 'librechat-data-provider';
|
||||||
import { SandpackPreview, SandpackProvider } from '@codesandbox/sandpack-react/unstyled';
|
import { SandpackPreview, SandpackProvider } from '@codesandbox/sandpack-react/unstyled';
|
||||||
|
import type { SandpackPreviewRef } from '@codesandbox/sandpack-react/unstyled';
|
||||||
import type { Artifact } from '~/common';
|
import type { Artifact } from '~/common';
|
||||||
import {
|
import {
|
||||||
sharedFiles,
|
sharedFiles,
|
||||||
|
|
@ -14,9 +15,11 @@ import {
|
||||||
export const ArtifactPreview = memo(function ({
|
export const ArtifactPreview = memo(function ({
|
||||||
showEditor = false,
|
showEditor = false,
|
||||||
artifact,
|
artifact,
|
||||||
|
previewRef,
|
||||||
}: {
|
}: {
|
||||||
showEditor?: boolean;
|
showEditor?: boolean;
|
||||||
artifact: Artifact;
|
artifact: Artifact;
|
||||||
|
previewRef: React.MutableRefObject<SandpackPreviewRef>;
|
||||||
}) {
|
}) {
|
||||||
const files = useMemo(() => {
|
const files = useMemo(() => {
|
||||||
return removeNullishValues({ [getArtifactFilename(artifact.type ?? '')]: artifact.content });
|
return removeNullishValues({ [getArtifactFilename(artifact.type ?? '')]: artifact.content });
|
||||||
|
|
@ -56,7 +59,12 @@ export const ArtifactPreview = memo(function ({
|
||||||
{...sharedProps}
|
{...sharedProps}
|
||||||
template={template}
|
template={template}
|
||||||
>
|
>
|
||||||
<SandpackPreview showOpenInCodeSandbox={false} showRefreshButton={false} tabIndex={0} />
|
<SandpackPreview
|
||||||
|
showOpenInCodeSandbox={false}
|
||||||
|
showRefreshButton={false}
|
||||||
|
tabIndex={0}
|
||||||
|
ref={previewRef}
|
||||||
|
/>
|
||||||
</SandpackProvider>
|
</SandpackProvider>
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
|
import { useRef, useState } from 'react';
|
||||||
|
import { RefreshCw } from 'lucide-react';
|
||||||
import * as Tabs from '@radix-ui/react-tabs';
|
import * as Tabs from '@radix-ui/react-tabs';
|
||||||
|
import { SandpackPreviewRef } from '@codesandbox/sandpack-react';
|
||||||
import useArtifacts from '~/hooks/Artifacts/useArtifacts';
|
import useArtifacts from '~/hooks/Artifacts/useArtifacts';
|
||||||
import { CodeMarkdown, CopyCodeButton } from './Code';
|
import { CodeMarkdown, CopyCodeButton } from './Code';
|
||||||
import { getFileExtension } from '~/utils/artifacts';
|
import { getFileExtension } from '~/utils/artifacts';
|
||||||
|
|
@ -6,6 +9,9 @@ import { ArtifactPreview } from './ArtifactPreview';
|
||||||
import { cn } from '~/utils';
|
import { cn } from '~/utils';
|
||||||
|
|
||||||
export default function Artifacts() {
|
export default function Artifacts() {
|
||||||
|
const previewRef = useRef<SandpackPreviewRef>();
|
||||||
|
const [isRefreshing, setIsRefreshing] = useState(false);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
isVisible,
|
isVisible,
|
||||||
activeTab,
|
activeTab,
|
||||||
|
|
@ -21,6 +27,15 @@ export default function Artifacts() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleRefresh = () => {
|
||||||
|
setIsRefreshing(true);
|
||||||
|
const client = previewRef.current?.getClient();
|
||||||
|
if (client != null) {
|
||||||
|
client.dispatch({ type: 'refresh' });
|
||||||
|
}
|
||||||
|
setTimeout(() => setIsRefreshing(false), 750);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Tabs.Root value={activeTab} onValueChange={setActiveTab} asChild>
|
<Tabs.Root value={activeTab} onValueChange={setActiveTab} asChild>
|
||||||
{/* Main Parent */}
|
{/* Main Parent */}
|
||||||
|
|
@ -50,6 +65,22 @@ export default function Artifacts() {
|
||||||
<h3 className="truncate text-sm text-text-primary">{currentArtifact.title}</h3>
|
<h3 className="truncate text-sm text-text-primary">{currentArtifact.title}</h3>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center">
|
<div className="flex items-center">
|
||||||
|
{/* Refresh button */}
|
||||||
|
{activeTab === 'preview' && (
|
||||||
|
<button
|
||||||
|
className={`mr-2 text-text-secondary transition-transform duration-500 ease-in-out ${
|
||||||
|
isRefreshing ? 'rotate-180' : ''
|
||||||
|
}`}
|
||||||
|
onClick={handleRefresh}
|
||||||
|
disabled={isRefreshing}
|
||||||
|
aria-label="Refresh"
|
||||||
|
>
|
||||||
|
<RefreshCw
|
||||||
|
size={16}
|
||||||
|
className={`transform ${isRefreshing ? 'animate-spin' : ''}`}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
<Tabs.List className="mr-2 inline-flex h-7 rounded-full border border-border-medium bg-surface-tertiary">
|
<Tabs.List className="mr-2 inline-flex h-7 rounded-full border border-border-medium bg-surface-tertiary">
|
||||||
<Tabs.Trigger
|
<Tabs.Trigger
|
||||||
value="preview"
|
value="preview"
|
||||||
|
|
@ -90,7 +121,10 @@ export default function Artifacts() {
|
||||||
/>
|
/>
|
||||||
</Tabs.Content>
|
</Tabs.Content>
|
||||||
<Tabs.Content value="preview" className="flex-grow overflow-auto bg-white">
|
<Tabs.Content value="preview" className="flex-grow overflow-auto bg-white">
|
||||||
<ArtifactPreview artifact={currentArtifact} />
|
<ArtifactPreview
|
||||||
|
artifact={currentArtifact}
|
||||||
|
previewRef={previewRef as React.MutableRefObject<SandpackPreviewRef>}
|
||||||
|
/>
|
||||||
</Tabs.Content>
|
</Tabs.Content>
|
||||||
{/* Footer */}
|
{/* Footer */}
|
||||||
<div className="flex items-center justify-between border-t border-border-medium bg-surface-primary-alt p-2 text-sm text-text-secondary">
|
<div className="flex items-center justify-between border-t border-border-medium bg-surface-primary-alt p-2 text-sm text-text-secondary">
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue