Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add artifacts UI for shared links #3940

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
feat: open artifact on select
  • Loading branch information
monotykamary committed Sep 25, 2024
commit 20da18bcd1fe88115e1fc0dfa4d755660c9b5e8a
3 changes: 3 additions & 0 deletions client/src/components/Share/ShareView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ function SharedView() {
useDocumentTitle(docTitle);

useEffect(() => {
// Ensure artifact panel is initially closed
setIsArtifactPanelOpen(false);

// Reset artifact panel state when component unmounts
return () => {
setIsArtifactPanelOpen(false);
Expand Down
26 changes: 14 additions & 12 deletions client/src/components/Share/SharedArtifacts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useRef, useState, useEffect } from 'react';
import { useRecoilValue } from 'recoil';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { RefreshCw } from 'lucide-react';
import * as Tabs from '@radix-ui/react-tabs';
import { SandpackPreviewRef } from '@codesandbox/sandpack-react';
Expand All @@ -8,6 +8,7 @@ import { CodeMarkdown, CopyCodeButton } from '../Artifacts/Code';
import { getFileExtension } from '~/utils/artifacts';
import { ArtifactPreview } from '../Artifacts/ArtifactPreview';
import { cn } from '~/utils';
import store from '~/store';

interface SharedArtifactsProps {
isOpen: boolean;
Expand All @@ -16,19 +17,22 @@ interface SharedArtifactsProps {

const SharedArtifacts: React.FC<SharedArtifactsProps> = ({ isOpen, onClose }) => {
const artifacts = useRecoilValue(artifactsState);
const currentArtifactId = useRecoilValue(store.currentArtifactId);
const setCurrentArtifactId = useSetRecoilState(store.currentArtifactId);
const previewRef = useRef<SandpackPreviewRef>(null);
const [isRefreshing, setIsRefreshing] = useState(false);
const [activeTab, setActiveTab] = useState('preview');
const [currentIndex, setCurrentIndex] = useState(0);

const orderedArtifactIds = artifacts ? Object.keys(artifacts) : [];
const currentArtifact = artifacts && orderedArtifactIds[currentIndex] ? artifacts[orderedArtifactIds[currentIndex]] : null;
const currentArtifact = artifacts && currentArtifactId ? artifacts[currentArtifactId] : null;
const currentIndex = orderedArtifactIds.indexOf(currentArtifactId ?? '');

useEffect(() => {
if (isOpen && orderedArtifactIds.length > 0) {
setCurrentIndex(0);
if (isOpen && orderedArtifactIds.length > 0 && !currentArtifactId) {
// If no artifact is selected, select the first one
setCurrentArtifactId(orderedArtifactIds[0]);
}
}, [isOpen, orderedArtifactIds]);
}, [isOpen, orderedArtifactIds, currentArtifactId, setCurrentArtifactId]);

if (!artifacts || Object.keys(artifacts).length === 0) {
return null;
Expand All @@ -44,15 +48,13 @@ const SharedArtifacts: React.FC<SharedArtifactsProps> = ({ isOpen, onClose }) =>
};

const cycleArtifact = (direction: 'prev' | 'next') => {
let newIndex;
if (direction === 'prev') {
setCurrentIndex((prevIndex) =>
prevIndex > 0 ? prevIndex - 1 : orderedArtifactIds.length - 1
);
newIndex = currentIndex > 0 ? currentIndex - 1 : orderedArtifactIds.length - 1;
} else {
setCurrentIndex((prevIndex) =>
prevIndex < orderedArtifactIds.length - 1 ? prevIndex + 1 : 0
);
newIndex = currentIndex < orderedArtifactIds.length - 1 ? currentIndex + 1 : 0;
}
setCurrentArtifactId(orderedArtifactIds[newIndex]);
};

const isMermaid = currentArtifact?.type === 'mermaid';
Expand Down