Skip to content

Commit

Permalink
feat: Save / Load result functionality
Browse files Browse the repository at this point in the history
Resolves #30 and #24
  • Loading branch information
DarkPurple141 authored Dec 18, 2020
2 parents 2ff8fa1 + 012ce71 commit ca22500
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@
"build": "yarn build:clean && yarn typescript:build",
"prepublishOnly": "yarn build"
}
}
}
34 changes: 34 additions & 0 deletions src/panel/file-system.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { hiddenFileAnchorId } from '../selectors';
import { RunContext } from './machine';

export function saveFile(storyName: string, current: RunContext) {
const anchor = document.getElementById(hiddenFileAnchorId);

if (anchor) {
anchor.setAttribute(
'href',
'data:text/json,' + encodeURIComponent(JSON.stringify(current, null, 2)),
);
anchor.setAttribute('download', `${storyName}.json`);
anchor.click();
}
}

export function readFile(
e: React.FormEvent<HTMLInputElement>,
callback: (context: RunContext, storyFile: string) => void,
) {
const reader = new FileReader();
const { files } = e.currentTarget;

if (files && files.length) {
reader.readAsText(files[0]);
reader.onload = ({ target }) => {
if (target) {
const context: RunContext = JSON.parse(target.result as string);
const [storyName] = files[0].name.split('.json');
callback(context, storyName);
}
};
}
}
34 changes: 34 additions & 0 deletions src/panel/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type MachineEvents =
| { type: 'FINISH'; results: TaskGroupResult[] }
| { type: 'PIN' }
| { type: 'UNPIN' }
| { type: 'SAVE' }
| { type: 'LOAD_FROM_FILE'; storyName: string; pinned: Nullable<RunContext> }
| { type: 'SET_VALUES'; copies: number; samples: number };

export type RunContext = {
Expand Down Expand Up @@ -134,6 +136,38 @@ const machine: MachineType = Machine<MachineContext, MachineSchema, MachineEvent
};
}),
},
SAVE: {
internal: true,
target: 'idle',
// Only allow saving when there are results
cond: (context): boolean => {
return context.current.results != null;
},
actions: assign((context) => {
return {
...context,
message: 'Result saved',
};
}),
},
LOAD_FROM_FILE: {
internal: true,
target: 'idle',
actions: assign(
(context, event): MachineContext => {
const message: Nullable<string> = event.pinned
? `Loaded pinned result: ${event.storyName}`
: null;
return {
...context,
message,
pinned: event.pinned,
storyName: event.storyName,
current: event.pinned || context.current,
};
},
),
},
UNPIN: {
internal: true,
target: 'idle',
Expand Down
55 changes: 33 additions & 22 deletions src/panel/task-result/parts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export const Section = styled.div`
border-bottom-left-radius: var(--result-border-radius);
border-bottom-right-radius: var(--result-border-radius);
}
` as any;
`;

export const Heading = styled.h4`
font-weight: bold;
` as any;
`;

export const Content = styled.div`` as any;
export const Content = styled.div``;

// @ts-ignore
export const Note = styled.div`
Expand All @@ -32,46 +32,57 @@ export const Note = styled.div`
margin-right: 1ch;
content: 'ℹ️';
}
` as any;
`;

export const ResultValue = styled.code`
/* font-family: 'Courier'; */
` as any;
`;

export const ResultScale = styled.code`
/* slightly smaller margin that other elements */
margin-left: var(--halfGrid);
` as any;
`;

export const ValueLozenge = styled.code<{
hasWarningIcon?: boolean;
type: 'positive' | 'negative' | 'warning' | 'info' | 'faint' | 'raw';
width?: 'fill' | 'inherit';
}>`
${({ hasWarningIcon = true }) =>
!hasWarningIcon &&
`&:before {
content: '⚠️';
margin-right: 1ch;
}`};
padding: calc(var(--grid) / 2) var(--grid);
border-radius: var(--result-border-radius);
color: ${(props) => props.theme.color.light};
font-weight: bold;
font-size: small;
background-color: ${({ type, theme }) =>
type === 'positive'
? theme.color.positive
: type === 'negative'
? theme.color.negative
: type === 'warning'
? theme.color.warning
: type === 'info'
? theme.color.seafoam
: type === 'faint'
? theme.color.medium
: theme.color.purple};
` as any;
background-color: ${({ type, theme }) => {
switch (type) {
case 'positive':
return theme.color.positive;
case 'negative':
return theme.color.negative;
case 'faint':
return theme.color.medium;
case 'warning':
return theme.color.warning;
case 'info':
return theme.color.seafoam;
default:
return theme.color.purple;
}
}};
`;

export const Table = styled.table`
width: 100%;
` as any;
`;

export const TitleCell = styled.td`` as any;
export const TitleCell = styled.td``;

export const ValueCell = styled.td`
text-align: right;
` as any;
`;
8 changes: 4 additions & 4 deletions src/panel/task-result/timed-result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { ExpandingResult } from './expanding-result';
import * as Parts from './parts';
import getChange from '../../util/get-change';

const warningIcon = <span>⚠️</span>;

function getDiff({ result, pinned }: { result: TimedResult; pinned: Nullable<TimedResult> }) {
if (!pinned) {
return 0;
Expand Down Expand Up @@ -79,8 +77,10 @@ function Variance({ result }: { result: TimedResult }) {
<tr>
<Parts.TitleCell>Standard deviation</Parts.TitleCell>
<Parts.ValueCell>
<Parts.ValueLozenge type={wasStable ? 'positive' : 'negative'}>
{wasStable ? null : warningIcon}
<Parts.ValueLozenge
hasWarningIcon={wasStable}
type={wasStable ? 'positive' : 'negative'}
>
{toFixed(result.variance.standardDeviation)}
</Parts.ValueLozenge>
</Parts.ValueCell>
Expand Down
Loading

0 comments on commit ca22500

Please sign in to comment.