import { useVirtualizer } from '@tanstack/react-virtual';
import type { HttpResponse } from '@yaakapp-internal/models';
import type { ServerSentEvent } from '@yaakapp-internal/sse';
import classNames from 'classnames';
import { motion } from 'framer-motion';
import React, { Fragment, useMemo, useRef, useState } from 'react';
import { useResponseBodyEventSource } from '../../hooks/useResponseBodyEventSource';
import { isJSON } from '../../lib/contentType';
import { tryFormatJson } from '../../lib/formatters';
import { Button } from '../core/Button';
import { Editor } from '../core/Editor';
import { Icon } from '../core/Icon';
import { InlineCode } from '../core/InlineCode';
import { Separator } from '../core/Separator';
import { SplitLayout } from '../core/SplitLayout';
import { HStack, VStack } from '../core/Stacks';
interface Props {
response: HttpResponse;
}
export function EventStreamViewer({ response }: Props) {
return (
);
}
function ActualEventStreamViewer({ response }: Props) {
const [showLarge, setShowLarge] = useState(false);
const [showingLarge, setShowingLarge] = useState(false);
const [activeEventIndex, setActiveEventIndex] = useState(null);
const events = useResponseBodyEventSource(response);
const activeEvent = useMemo(
() => (activeEventIndex == null ? null : events.data?.[activeEventIndex]),
[activeEventIndex, events],
);
const language = useMemo<'text' | 'json'>(() => {
if (!activeEvent?.data) return 'text';
return isJSON(activeEvent?.data) ? 'json' : 'text';
}, [activeEvent?.data]);
return (
(
)}
secondSlot={
activeEvent
? () => (
Message Received
{!showLarge && activeEvent.data.length > 1000 * 1000 ? (
Message previews larger than 1MB are hidden
) : (
)}
)
: null
}
/>
);
}
function EventStreamEventsVirtual({
events,
activeEventIndex,
setActiveEventIndex,
}: {
events: ServerSentEvent[];
activeEventIndex: number | null;
setActiveEventIndex: (eventId: number | null) => void;
}) {
// The scrollable element for your list
const parentRef = useRef(null);
// The virtualizer
const rowVirtualizer = useVirtualizer({
count: events.length,
getScrollElement: () => parentRef.current,
estimateSize: () => 28, // react-virtual requires a height, so we'll give it one
});
return (