import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; import React, { FC, useEffect, useState } from 'react'; import { AddonPanel } from 'storybook/internal/components'; import { STORY_CHANGED } from 'storybook/internal/core-events'; import { addons } from 'storybook/manager-api'; import { AxiosMockHandlersConfig, EVENTS, type ListEntry, TYPES } from '../types'; import { ConfigHeader } from './ConfigHeader'; import { List } from './List'; export type Props = { active: boolean; }; export const Addon: FC = ({ active }) => { const [entries, setEntries] = useState([]); const [config, setConfig] = useState([]); const onRequest = (data: AxiosRequestConfig) => setEntries(entries => [...entries, { type: TYPES.REQ, data }]); const onResponse = (data: AxiosResponse) => setEntries(entries => [...entries, { type: TYPES.RES, data }]); const onResponseError = (data: AxiosError) => setEntries(entries => [...entries, { type: TYPES.RES_ERR, data }]); const onStoryChanged = () => { setEntries([]); setConfig([]); }; const onConfigChanged = (data: AxiosMockHandlersConfig[]) => { setConfig(data); }; // biome-ignore lint/correctness/useExhaustiveDependencies : ignoring for now useEffect(() => { addons.getChannel().addListener(STORY_CHANGED, onStoryChanged); addons.getChannel().addListener(EVENTS.MOCK_CONFIG, onConfigChanged); addons.getChannel().addListener(EVENTS.REQUEST, onRequest); addons.getChannel().addListener(EVENTS.RESPONSE, onResponse); addons.getChannel().addListener(EVENTS.RESPONSE_ERROR, onResponseError); return () => { addons.getChannel().removeListener(EVENTS.MOCK_CONFIG, onConfigChanged); addons.getChannel().removeListener(STORY_CHANGED, onStoryChanged); addons.getChannel().removeListener(EVENTS.REQUEST, onRequest); addons.getChannel().removeListener(EVENTS.RESPONSE, onResponse); addons.getChannel().removeListener(EVENTS.RESPONSE_ERROR, onResponseError); }; }, [onRequest, onResponse, onResponseError]); return (
{config.length > 0 && } {entries.length === 0 ? (
No requests made yet
) : ( <> )}
); };