refactor: remove artifactIdsState

This commit is contained in:
Danny Avila 2024-08-23 22:32:34 -04:00
parent 2ac9fd3aed
commit bedc91adcd
4 changed files with 9 additions and 41 deletions

View file

@ -4,7 +4,7 @@ import { visit } from 'unist-util-visit';
import { useSetRecoilState, useRecoilValue } from 'recoil';
import type { Pluggable } from 'unified';
import type { Artifact } from '~/common';
import { artifactsState, artifactIdsState } from '~/store/artifacts';
import { artifactsState } from '~/store/artifacts';
import ArtifactButton from './ArtifactButton';
import { logger } from '~/utils';
@ -45,7 +45,6 @@ export function Artifact({
node: unknown;
}) {
const setArtifacts = useSetRecoilState(artifactsState);
const setArtifactIds = useSetRecoilState(artifactIdsState);
const currentArtifacts = useRecoilValue(artifactsState);
const [artifact, setArtifact] = useState<Artifact | null>(null);
@ -91,28 +90,9 @@ export function Artifact({
};
});
setArtifactIds((prevIds) => {
if (!prevIds.includes(artifactKey)) {
const newIds = [...prevIds, artifactKey];
const definedIds = newIds.filter((id) => currentArtifacts[id] != null);
return definedIds.sort(
(a, b) => currentArtifacts[b].lastUpdateTime - currentArtifacts[a].lastUpdateTime,
);
}
return prevIds;
});
setArtifact(currentArtifact);
});
}, [
props.children,
props.title,
props.type,
props.identifier,
setArtifacts,
setArtifactIds,
currentArtifacts,
]);
}, [props.type, props.title, setArtifacts, props.children, props.identifier]);
useEffect(() => {
updateArtifact();

View file

@ -61,11 +61,12 @@ export function ArtifactPreview({
}
export default function Artifacts() {
const { isSubmitting, latestMessage } = useChatContext();
const { isSubmitting, latestMessage, conversation } = useChatContext();
const conversationId = useMemo(() => conversation?.conversationId ?? null, [conversation]);
const [isVisible, setIsVisible] = useState(false);
const [activeTab, setActiveTab] = useState('preview');
const artifacts = useRecoilValue(store.artifactsState);
const [artifacts, setArtifacts] = useRecoilState(store.artifactsState);
const [currentArtifactId, setCurrentArtifactId] = useRecoilState(store.currentArtifactId);
const orderedArtifactIds = useMemo(() => {

View file

@ -22,9 +22,9 @@ export default function Presentation({
useSidePanel?: boolean;
}) {
const { data: startupConfig } = useGetStartupConfig();
const artifacts = useRecoilValue(store.artifactsState);
const codeArtifacts = useRecoilValue(store.codeArtifacts);
const hideSidePanel = useRecoilValue(store.hideSidePanel);
const codeBlockIds = useRecoilValue(store.artifactIdsState);
const interfaceConfig = useMemo(
() => startupConfig?.interface ?? defaultInterface,
[startupConfig],
@ -95,7 +95,9 @@ export default function Presentation({
defaultLayout={defaultLayout}
defaultCollapsed={defaultCollapsed}
fullPanelCollapse={fullCollapse}
artifacts={codeArtifacts && codeBlockIds.length > 0 ? <Artifacts /> : null}
artifacts={
codeArtifacts != null && Object.keys(artifacts).length > 0 ? <Artifacts /> : null
}
>
<main className="flex h-full flex-col" role="main">
{children}

View file

@ -17,21 +17,6 @@ export const artifactsState = atom<Record<string, Artifact>>({
] as const,
});
export const artifactIdsState = atom<string[]>({
key: 'artifactIdsState',
default: [],
effects: [
({ onSet, node }) => {
onSet(async (newValue) => {
logger.log('artifacts', 'Recoil Effect: Setting artifactIdsState', {
key: node.key,
newValue,
});
});
},
] as const,
});
export const currentArtifactId = atom<string | null>({
key: 'currentArtifactId',
default: null,