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

Add alt text to system icons #157

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-moles-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@envyjs/webui': patch
---

Add system name as system icon alt text
3 changes: 3 additions & 0 deletions packages/webui/src/components/ui/TraceRequestData.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Story = StoryObj<typeof meta>;

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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down
43 changes: 32 additions & 11 deletions packages/webui/src/components/ui/TraceRequestData.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,32 @@ describe('TraceRequestData', () => {
});

it('should render without error', () => {
render(<TraceRequestData iconPath="icon.jpg" path="/" />);
render(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/" />);
});

describe('iconPath', () => {
it('should render icon as image', () => {
const { getByTestId } = render(<TraceRequestData iconPath="icon.jpg" path="/" />);
const { getByTestId } = render(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/" />);

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(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/" />);

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(<TraceRequestData iconPath="icon.jpg" path="/foo/bar" />);
const { queryByTestId } = render(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/foo/bar" />);

const host = queryByTestId('item-hostname');

Expand All @@ -39,7 +48,7 @@ describe('TraceRequestData', () => {

it('should render host if `hostName` is supplied', () => {
const { getByTestId } = render(
<TraceRequestData iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" />,
<TraceRequestData systemName="Default" iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" />,
);

const host = getByTestId('item-hostname');
Expand All @@ -50,7 +59,7 @@ describe('TraceRequestData', () => {

it('should render host next to image', () => {
const { getByTestId } = render(
<TraceRequestData iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" />,
<TraceRequestData systemName="Default" iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" />,
);

const host = getByTestId('item-hostname');
Expand All @@ -62,7 +71,7 @@ describe('TraceRequestData', () => {

describe('path', () => {
it('should render path', () => {
const { getByTestId } = render(<TraceRequestData iconPath="icon.jpg" path="/foo/bar" />);
const { getByTestId } = render(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/foo/bar" />);

const path = getByTestId('item-path');

Expand All @@ -71,7 +80,7 @@ describe('TraceRequestData', () => {
});

it('should render path next to image if `hostName` is not supplied', () => {
const { getByTestId } = render(<TraceRequestData iconPath="icon.jpg" path="/foo/bar" />);
const { getByTestId } = render(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/foo/bar" />);

const path = getByTestId('item-path');
const image = getByTestId('item-image');
Expand All @@ -81,7 +90,7 @@ describe('TraceRequestData', () => {

it('should render path next to host if `hostName` is supplied', () => {
const { getByTestId } = render(
<TraceRequestData iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" />,
<TraceRequestData systemName="Default" iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" />,
);

const path = getByTestId('item-path');
Expand All @@ -93,7 +102,7 @@ describe('TraceRequestData', () => {

describe('data', () => {
it('should not render data if `data` is not supplied', () => {
const { queryByTestId } = render(<TraceRequestData iconPath="icon.jpg" path="/foo/bar" />);
const { queryByTestId } = render(<TraceRequestData systemName="Default" iconPath="icon.jpg" path="/foo/bar" />);

const data = queryByTestId('item-data');

Expand All @@ -102,7 +111,13 @@ describe('TraceRequestData', () => {

it('should render data if `data` is supplied', () => {
const { getByTestId } = render(
<TraceRequestData iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" data="This is some data" />,
<TraceRequestData
systemName="Default"
iconPath="icon.jpg"
hostName="www.example.com"
path="/foo/bar"
data="This is some data"
/>,
);

const data = getByTestId('item-data');
Expand All @@ -112,7 +127,13 @@ describe('TraceRequestData', () => {

it('should render data after request detail', () => {
const { getByTestId } = render(
<TraceRequestData iconPath="icon.jpg" hostName="www.example.com" path="/foo/bar" data="This is some data" />,
<TraceRequestData
systemName="Default"
iconPath="icon.jpg"
hostName="www.example.com"
path="/foo/bar"
data="This is some data"
/>,
);

const data = getByTestId('item-data');
Expand Down
5 changes: 3 additions & 2 deletions packages/webui/src/components/ui/TraceRequestData.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<span data-test-id="item-request" className="flex flex-row items-center">
<img data-test-id="item-image" src={iconPath} alt="" className="flex-0 inline-block w-4 h-4 mr-1.5" />
<img data-test-id="item-image" src={iconPath} alt={systemName} className="flex-0 inline-block w-4 h-4 mr-1.5" />
{hostName && (
<span data-test-id="item-hostname" className="font-semibold pr-1">
{hostName}
Expand Down
14 changes: 11 additions & 3 deletions packages/webui/src/systems/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { pathAndQuery } from '@/utils';

import { getDefaultSystem, getRegisteredSystems } from './registration';

function callOrFallback<T>(trace: Trace, fnName: keyof Omit<System<unknown>, 'name' | 'isMatch' | 'getData'>): T {
function getSystemByTrace(trace: Trace): System<unknown> | 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<T>(trace: Trace, fnName: keyof Omit<System<unknown>, 'name' | 'isMatch' | 'getData'>): T {
const system = getSystemByTrace(trace);
const defaultSystem = getDefaultSystem();
const data = system?.getData?.(trace) ?? null;

let value = null;
Expand All @@ -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');
}
Expand All @@ -42,6 +49,7 @@ export function ListDataComponent({ trace }: SystemDetailProps): React.ReactNode
const [path, qs] = pathAndQuery(trace);
return (
<TraceRequestData
systemName={getSystemName(trace)}
iconPath={getIconUri(trace)}
hostName={trace.http?.host}
path={path}
Expand Down
27 changes: 24 additions & 3 deletions packages/webui/src/systems/systems.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getIconUri,
getRequestBody,
getResponseBody,
getSystemName,
} from '.';

jest.mock('@/systems/registration');
Expand Down Expand Up @@ -53,7 +54,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('Foo');
});

it('should return correct value for `getIconUri`', () => {
const result = getIconUri(trace);
expect(result).toEqual('foo_icon');
});
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
});
Expand Down Expand Up @@ -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');
});
Expand Down