Skip to content

Commit

Permalink
address edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
esarafianou committed Dec 19, 2024
1 parent a2d4e94 commit d2a6df9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
14 changes: 12 additions & 2 deletions webapp/src/components/retrospective_post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import {Metric, MetricType} from 'src/types/playbook';

import {RunMetricData} from 'src/types/playbook_run';

import {
isArrayOf,
isMetric,
isMetricData,
safeJSONParse,
} from 'src/utils';

import {ClockOutline, DollarSign, PoundSign} from './backstage/playbook_edit/styles';
import {metricToString} from './backstage/playbook_edit/metrics/shared';

Expand All @@ -33,8 +40,11 @@ export const RetrospectivePost = (props: Props) => {

const mdText = (text: string) => messageHtmlToComponent(formatText(text, markdownOptions), true, messageHtmlToComponentOptions);

const metricsConfigs: Array<Metric> = props.post.props?.metricsConfigs ? JSON.parse(props.post.props.metricsConfigs) : [];
const metricsData: Array<RunMetricData> = props.post.props?.metricsData ? JSON.parse(props.post.props.metricsData) : [];
const parsedMetricsConfigs = safeJSONParse<unknown>(props.post.props?.metricsConfigs);
const parsedMetricsData = safeJSONParse<unknown>(props.post.props?.metricsData);

const metricsConfigs: Array<Metric> = isArrayOf(parsedMetricsConfigs, isMetric) ? parsedMetricsConfigs : [];
const metricsData: Array<RunMetricData> = isArrayOf(parsedMetricsData, isMetricData) ? parsedMetricsData : [];

return (
<>
Expand Down
41 changes: 39 additions & 2 deletions webapp/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useState} from 'react';

import {PlaybookRun} from 'src/types/playbook_run';
import {Checklist} from 'src/types/playbook';
import {PlaybookRun, RunMetricData} from 'src/types/playbook_run';
import {Checklist, Metric} from 'src/types/playbook';

let idCounter = 0;

Expand Down Expand Up @@ -132,3 +132,40 @@ export function runCallsSlashCommand(command: string, channelId: string, teamId:
},
}, window.origin);
}

export function safeJSONParse<T>(value: unknown): T | null {
if (!value || typeof value !== 'string') {
return null;
}

try {
return JSON.parse(value) as T;
} catch {
return null;
}
}

export function isArrayOf<T>(value: unknown, typeGuard: (value: unknown) => value is T): value is T[] {
return Array.isArray(value) && value.every(typeGuard);
}

export function isMetric(value: unknown): value is Metric {
if (!value || typeof value !== 'object') {
return false;
}

const metric = value as Metric;
return typeof metric.id === 'string' &&
typeof metric.title === 'string' &&
typeof metric.type === 'string';
}

export function isMetricData(value: unknown): value is RunMetricData {
if (!value || typeof value !== 'object') {
return false;
}

const metricData = value as RunMetricData;
return typeof metricData.metric_config_id === 'string' &&
(typeof metricData.value === 'string' || typeof metricData.value === 'number');
}

0 comments on commit d2a6df9

Please sign in to comment.