import {PlusIcon} from "@heroicons/react/outline"; import {useEffect, useState} from "react"; import {SubmitHandler, useForm} from "react-hook-form" import Button from "../../../components/Button"; import LineEdit from "../../../components/Input"; import {ApiKeyData} from "../../../core/ApiKey"; import {Metric, MetricData} from "../../../core/Metric"; import {TagData} from "../../../core/Tag"; import {View} from "../../../core/View"; import {useModal} from "../../../services/ModalService"; function clone(x: T): T { return JSON.parse(JSON.stringify(x)) } interface OrganizeDashboardFormProps { dashboard: View | null metrics: Metric[] close: any onSave: (newDashboardConfig: DashboardView) => void } type DashboardViewCharts = { name: string order: number metrics: { metricId: string color: string type: string alwaysUseLastValue: boolean }[] }[] export type DashboardView = { metrics: { metricId: string, name: string order: number, show: boolean color: string }[] charts: DashboardViewCharts } const optionsCss = "f-r items-center gap-2 bg-neutral-500 rounded-md" export function OrganizeDashboardForm(props: OrganizeDashboardFormProps) { const [showGraphs, setShowGraphs] = useState(true) const [metricsOrder, setMetricsOrder] = useState() useEffect(() => { const defaultDashboard: DashboardView = { metrics: props.metrics.map((metric, index) => ({ metricId: metric.id, name: metric.name, order: index, show: false, color: "#dddddd" })), charts: [] } let x: DashboardView = defaultDashboard; if (props.dashboard) { x = clone(props.dashboard.config) props.metrics.forEach((metric, index)=> { if (!x.metrics.find(m => m.metricId === metric.id)) { x.metrics.push({ metricId: metric.id, name: metric.name, order: index, show: false, color: "#dddddd" }) } }) x.metrics = x.metrics.filter(metric => props.metrics.find(m => m.id === metric.metricId)) } x.metrics.sort((a, b) => a.order - b.order) setMetricsOrder(x) }, []) if (!metricsOrder) { return
Loading...
} return (
{!showGraphs && metricsOrder.metrics.map((metric, index) => (
{metric.name}
Order { const value = parseInt(valueString) const newMetricsOrder = clone(metricsOrder) newMetricsOrder.metrics[index].order = value setMetricsOrder(newMetricsOrder) }} />
Color { const checked = valueString.target.value const newMetricsOrder = clone(metricsOrder) newMetricsOrder.metrics[index].color = checked setMetricsOrder(newMetricsOrder) }} />
{ const checked = valueString.target.checked const newMetricsOrder = clone(metricsOrder) newMetricsOrder.metrics[index].show = checked setMetricsOrder(newMetricsOrder) }} />
))} {showGraphs && { const newMetricsOrder = clone(metricsOrder) newMetricsOrder.charts = newCharts setMetricsOrder(newMetricsOrder) }} /> }
) } interface OrganizeDashboardChartsProps { charts: DashboardViewCharts metrics: Metric[] close: any onChange: (charts: DashboardViewCharts) => void } function OrganizeCharts({metrics, charts, onChange, close}: OrganizeDashboardChartsProps) { const addChart = () => { const newCharts = clone(charts) newCharts.push({ name: "New chart", order: 1, metrics: [] }) onChange(newCharts) } const deleteChart = (chartIndex: number) => { const newCharts = clone(charts) newCharts.splice(chartIndex, 1) onChange(newCharts) } const setChart = (chartIndex: number, name: string, order: number) => { const newCharts = clone(charts) newCharts[chartIndex].name = name newCharts[chartIndex].order = order onChange(newCharts) } const addMetricToChart = (chartIndex: number) => { const metric: Metric = metrics[0] const newCharts = clone(charts) newCharts[chartIndex].metrics.push({ metricId: metric.id, color: "red", type: "line", alwaysUseLastValue: true }) onChange(newCharts) } const setMetricOnChart = (chartIndex: number, metricIndex: number, newMetric: {metricId: string, type: string, color: string, alwaysUseLastValue: boolean}) => { const newCharts = clone(charts) newCharts[chartIndex].metrics[metricIndex] = newMetric onChange(newCharts) } const deleteMetricOnChart = (chartIndex: number, metricIndex: number) => { const newCharts = clone(charts) newCharts[chartIndex].metrics.splice(metricIndex, 1) onChange(newCharts) } return (
{chart.metrics.map((selectedMetric, selectedMetricIndex) =>
Always use last value { setMetricOnChart(chartIndex, selectedMetricIndex, {...selectedMetric, alwaysUseLastValue: event.target.checked}) }} />
{ setMetricOnChart(chartIndex, selectedMetricIndex, {...selectedMetric, color: event.target.value}) }} />
)}
))} ) }