Skip to content

Commit 175907d

Browse files
authored
Add React components for showing model alert results (#3495)
1 parent a8a63a4 commit 175907d

4 files changed

Lines changed: 172 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { AnalysisAlert } from "../../variant-analysis/shared/analysis-result";
2+
import type { ModeledMethod } from "../modeled-method";
3+
4+
export interface ModelAlerts {
5+
model: ModeledMethod;
6+
alerts: AnalysisAlert[];
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { Meta, StoryFn } from "@storybook/react";
2+
3+
import { ModelAlertsResults as ModelAlertsResultsComponent } from "../../view/model-alerts/ModelAlertsResults";
4+
import { createSinkModeledMethod } from "../../../test/factories/model-editor/modeled-method-factories";
5+
import { createMockAnalysisAlert } from "../../../test/factories/variant-analysis/shared/analysis-alert";
6+
7+
export default {
8+
title: "Model Alerts/Model Alerts Results",
9+
component: ModelAlertsResultsComponent,
10+
} as Meta<typeof ModelAlertsResultsComponent>;
11+
12+
const Template: StoryFn<typeof ModelAlertsResultsComponent> = (args) => (
13+
<ModelAlertsResultsComponent {...args} />
14+
);
15+
16+
export const ModelAlertsResults = Template.bind({});
17+
ModelAlertsResults.args = {
18+
modelAlerts: {
19+
model: createSinkModeledMethod(),
20+
alerts: [createMockAnalysisAlert()],
21+
},
22+
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { styled } from "styled-components";
2+
import type { ModelAlerts } from "../../model-editor/model-alerts/model-alerts";
3+
import { Codicon } from "../common";
4+
import { useState } from "react";
5+
import { VSCodeBadge } from "@vscode/webview-ui-toolkit/react";
6+
import { formatDecimal } from "../../common/number";
7+
import AnalysisAlertResult from "../variant-analysis/AnalysisAlertResult";
8+
import { MethodName } from "../model-editor/MethodName";
9+
import { ModelDetails } from "./ModelDetails";
10+
11+
// This will ensure that these icons have a className which we can use in the TitleContainer
12+
const ExpandCollapseCodicon = styled(Codicon)``;
13+
14+
const TitleContainer = styled.button`
15+
display: flex;
16+
gap: 0.5em;
17+
align-items: center;
18+
width: 100%;
19+
20+
color: var(--vscode-editor-foreground);
21+
background-color: transparent;
22+
border: none;
23+
cursor: pointer;
24+
25+
&:disabled {
26+
cursor: default;
27+
28+
${ExpandCollapseCodicon} {
29+
color: var(--vscode-disabledForeground);
30+
}
31+
}
32+
`;
33+
34+
const ModelTypeText = styled.span`
35+
font-size: 0.85em;
36+
color: var(--vscode-descriptionForeground);
37+
`;
38+
39+
const ModelDetailsContainer = styled.div`
40+
padding-top: 10px;
41+
`;
42+
43+
const AlertsContainer = styled.ul`
44+
list-style-type: none;
45+
margin: 1em 0 0;
46+
padding: 0.5em 0 0 0;
47+
`;
48+
49+
const Alert = styled.li`
50+
margin-bottom: 1em;
51+
background-color: var(--vscode-notifications-background);
52+
`;
53+
54+
interface Props {
55+
modelAlerts: ModelAlerts;
56+
}
57+
58+
export const ModelAlertsResults = ({
59+
modelAlerts,
60+
}: Props): React.JSX.Element => {
61+
const [isExpanded, setExpanded] = useState(true);
62+
return (
63+
<div>
64+
<TitleContainer onClick={() => setExpanded(!isExpanded)}>
65+
{isExpanded && (
66+
<ExpandCollapseCodicon name="chevron-down" label="Collapse" />
67+
)}
68+
{!isExpanded && (
69+
<ExpandCollapseCodicon name="chevron-right" label="Expand" />
70+
)}
71+
<VSCodeBadge>{formatDecimal(modelAlerts.alerts.length)}</VSCodeBadge>
72+
<MethodName {...modelAlerts.model}></MethodName>
73+
<ModelTypeText>{modelAlerts.model.type}</ModelTypeText>
74+
</TitleContainer>
75+
{isExpanded && (
76+
<>
77+
<ModelDetailsContainer>
78+
<ModelDetails model={modelAlerts.model} />
79+
</ModelDetailsContainer>
80+
<AlertsContainer>
81+
{modelAlerts.alerts.map((r, i) => (
82+
<Alert key={i}>
83+
<AnalysisAlertResult alert={r} />
84+
</Alert>
85+
))}
86+
</AlertsContainer>
87+
</>
88+
)}
89+
</div>
90+
);
91+
};
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { styled } from "styled-components";
2+
import {
3+
modeledMethodSupportsInput,
4+
modeledMethodSupportsKind,
5+
modeledMethodSupportsOutput,
6+
} from "../../model-editor/modeled-method";
7+
import type { ModeledMethod } from "../../model-editor/modeled-method";
8+
9+
const DetailsContainer = styled.div`
10+
display: flex;
11+
`;
12+
13+
const Detail = styled.span`
14+
display: flex;
15+
margin-right: 30px;
16+
`;
17+
18+
const Label = styled.span`
19+
color: var(--vscode-descriptionForeground);
20+
margin-right: 10px;
21+
`;
22+
23+
const Value = styled.span``;
24+
25+
export const ModelDetails = ({ model }: { model: ModeledMethod }) => {
26+
return (
27+
<DetailsContainer>
28+
<Detail>
29+
<Label>Model type:</Label>
30+
<Value>{model.type}</Value>
31+
</Detail>
32+
{modeledMethodSupportsInput(model) && (
33+
<Detail>
34+
<Label>Input:</Label>
35+
<Value>{model.input}</Value>
36+
</Detail>
37+
)}
38+
{modeledMethodSupportsOutput(model) && (
39+
<Detail>
40+
<Label>Output:</Label>
41+
<Value>{model.output}</Value>
42+
</Detail>
43+
)}
44+
{modeledMethodSupportsKind(model) && (
45+
<Detail>
46+
<Label>Kind:</Label>
47+
<Value>{model.kind}</Value>
48+
</Detail>
49+
)}
50+
</DetailsContainer>
51+
);
52+
};

0 commit comments

Comments
 (0)