diff --git a/.changeset/chilled-moles-obey.md b/.changeset/chilled-moles-obey.md new file mode 100644 index 0000000..4743c10 --- /dev/null +++ b/.changeset/chilled-moles-obey.md @@ -0,0 +1,5 @@ +--- +'@envyjs/webui': patch +--- + +Add system name as system icon alt text diff --git a/packages/webui/src/components/ui/TraceRequestData.stories.tsx b/packages/webui/src/components/ui/TraceRequestData.stories.tsx index bd70dcc..315c945 100644 --- a/packages/webui/src/components/ui/TraceRequestData.stories.tsx +++ b/packages/webui/src/components/ui/TraceRequestData.stories.tsx @@ -26,6 +26,7 @@ type Story = StoryObj; export const RestRequest: Story = { args: { + systemName: new DefaultSystem().name, iconPath: new DefaultSystem().getIconUri(), path: '/api/v1/trace/1234?name=test&options=br|hk|desc', data: 'name=test&options=br|hk|desc', @@ -35,6 +36,7 @@ export const RestRequest: Story = { export const GraphqlRequest: Story = { args: { + systemName: new GraphqlSystem().name, iconPath: new GraphqlSystem().getIconUri(), path: '/graphql', data: 'Query', @@ -44,6 +46,7 @@ export const GraphqlRequest: Story = { export const SanityRequest: Story = { args: { + systemName: new SanitySystem().name, iconPath: new SanitySystem().getIconUri(), path: '/api/v1/trace/1234', data: 'type: Product', diff --git a/packages/webui/src/components/ui/TraceRequestData.test.tsx b/packages/webui/src/components/ui/TraceRequestData.test.tsx index 98cf3dd..25d476f 100644 --- a/packages/webui/src/components/ui/TraceRequestData.test.tsx +++ b/packages/webui/src/components/ui/TraceRequestData.test.tsx @@ -14,23 +14,32 @@ describe('TraceRequestData', () => { }); it('should render without error', () => { - render(); + render(); }); describe('iconPath', () => { it('should render icon as image', () => { - const { getByTestId } = render(); + const { getByTestId } = render(); const image = getByTestId('item-image'); expect(image).toBeVisible(); expect(image).toHaveAttribute('src', 'icon.jpg'); }); + + it('should render system name as image alt text', () => { + const { getByTestId } = render(); + + const image = getByTestId('item-image'); + + expect(image).toBeVisible(); + expect(image).toHaveAttribute('alt', 'Default'); + }); }); describe('hostName', () => { it('should not render host if `hostName` is not supplied', () => { - const { queryByTestId } = render(); + const { queryByTestId } = render(); const host = queryByTestId('item-hostname'); @@ -39,7 +48,7 @@ describe('TraceRequestData', () => { it('should render host if `hostName` is supplied', () => { const { getByTestId } = render( - , + , ); const host = getByTestId('item-hostname'); @@ -50,7 +59,7 @@ describe('TraceRequestData', () => { it('should render host next to image', () => { const { getByTestId } = render( - , + , ); const host = getByTestId('item-hostname'); @@ -62,7 +71,7 @@ describe('TraceRequestData', () => { describe('path', () => { it('should render path', () => { - const { getByTestId } = render(); + const { getByTestId } = render(); const path = getByTestId('item-path'); @@ -71,7 +80,7 @@ describe('TraceRequestData', () => { }); it('should render path next to image if `hostName` is not supplied', () => { - const { getByTestId } = render(); + const { getByTestId } = render(); const path = getByTestId('item-path'); const image = getByTestId('item-image'); @@ -81,7 +90,7 @@ describe('TraceRequestData', () => { it('should render path next to host if `hostName` is supplied', () => { const { getByTestId } = render( - , + , ); const path = getByTestId('item-path'); @@ -93,7 +102,7 @@ describe('TraceRequestData', () => { describe('data', () => { it('should not render data if `data` is not supplied', () => { - const { queryByTestId } = render(); + const { queryByTestId } = render(); const data = queryByTestId('item-data'); @@ -102,7 +111,13 @@ describe('TraceRequestData', () => { it('should render data if `data` is supplied', () => { const { getByTestId } = render( - , + , ); const data = getByTestId('item-data'); @@ -112,7 +127,13 @@ describe('TraceRequestData', () => { it('should render data after request detail', () => { const { getByTestId } = render( - , + , ); const data = getByTestId('item-data'); diff --git a/packages/webui/src/components/ui/TraceRequestData.tsx b/packages/webui/src/components/ui/TraceRequestData.tsx index 8f80af0..4c072d7 100644 --- a/packages/webui/src/components/ui/TraceRequestData.tsx +++ b/packages/webui/src/components/ui/TraceRequestData.tsx @@ -1,20 +1,21 @@ import useApplication from '@/hooks/useApplication'; export type TraceRequestDataProps = { + systemName: string; iconPath: string; hostName?: string; path: string; data?: string; }; -export default function TraceRequestData({ iconPath, hostName, path, data }: TraceRequestDataProps) { +export default function TraceRequestData({ systemName, iconPath, hostName, path, data }: TraceRequestDataProps) { const { selectedTraceId } = useApplication(); const pathValue = !!selectedTraceId ? `.../${path.split('/').splice(-1, 1).join('/')}` : path; return ( <> - + {systemName} {hostName && ( {hostName} diff --git a/packages/webui/src/systems/index.tsx b/packages/webui/src/systems/index.tsx index fa65fda..77da942 100644 --- a/packages/webui/src/systems/index.tsx +++ b/packages/webui/src/systems/index.tsx @@ -6,11 +6,14 @@ import { pathAndQuery } from '@/utils'; import { getDefaultSystem, getRegisteredSystems } from './registration'; -function callOrFallback(trace: Trace, fnName: keyof Omit, 'name' | 'isMatch' | 'getData'>): T { +function getSystemByTrace(trace: Trace): System | null { const systems = getRegisteredSystems(); - const defaultSystem = getDefaultSystem(); + return systems.find(x => x.isMatch(trace)) ?? null; +} - const system = systems.find(x => x.isMatch(trace)); +function callOrFallback(trace: Trace, fnName: keyof Omit, 'name' | 'isMatch' | 'getData'>): T { + const system = getSystemByTrace(trace); + const defaultSystem = getDefaultSystem(); const data = system?.getData?.(trace) ?? null; let value = null; @@ -24,6 +27,10 @@ type SystemDetailProps = { trace: Trace; }; +export function getSystemName(trace: Trace): string { + return getSystemByTrace(trace)?.name ?? getDefaultSystem().name; +} + export function getIconUri(trace: Trace | null): string { return callOrFallback(trace as Trace, 'getIconUri'); } @@ -42,6 +49,7 @@ export function ListDataComponent({ trace }: SystemDetailProps): React.ReactNode const [path, qs] = pathAndQuery(trace); return ( { }, } as Trace; - it('should return correct value for `getSystemIconPath`', () => { + it('should return correct value for `getSystemName`', () => { + const result = getSystemName(trace); + expect(result).toEqual('Foo'); + }); + + it('should return correct value for `getIconUri`', () => { const result = getIconUri(trace); expect(result).toEqual('foo_icon'); }); @@ -101,6 +107,11 @@ describe('Systems', () => { }, } as Trace; + it('should return correct value for `getSystemName`', () => { + const result = getSystemName(trace); + expect(result).toEqual('Bar'); + }); + it('should return correct value for `getIconUri`', () => { const result = getIconUri(trace); expect(result).toEqual('bar_icon'); @@ -149,7 +160,12 @@ describe('Systems', () => { }, } as Trace; - it('should return correct value for `getSystemIconPath`', () => { + it('should return correct value for `getSystemName`', () => { + const result = getSystemName(trace); + expect(result).toEqual('Fallbacks'); + }); + + it('should return correct value for `getIconUri`', () => { const result = getIconUri(trace); expect(result).toEqual('default_icon'); }); @@ -195,7 +211,12 @@ describe('Systems', () => { }, } as Trace; - it('should return correct value for `getSystemIconPath`', () => { + it('should return correct value for `getSystemName`', () => { + const result = getSystemName(trace); + expect(result).toEqual('Default'); + }); + + it('should return correct value for `getIconUri`', () => { const result = getIconUri(trace); expect(result).toEqual('default_icon'); });