Files
quality_analyse/web/src/sourceRecordsContext.ts
T

25 lines
1.3 KiB
TypeScript

import type { MetricRow, QueryParams, TrendMetricRow, ViewKind } from './types';
/** Freeze the clicked metric's filters and release together for every detail/export request. */
export function freezeSourceRecordsQuery(query: QueryParams, releaseId: string, view: ViewKind, row: MetricRow | null, month?: number): QueryParams {
if (!releaseId) throw new Error('指标数据尚未就绪,请重新查询后打开来源明细。');
const frozen = JSON.parse(JSON.stringify(query)) as QueryParams;
frozen.releaseId = releaseId;
const periodRow = row as TrendMetricRow | null;
const period = view === 'overall' ? String(periodRow?.period ?? periodRow?.month ?? '') : '';
const match = period.match(/^(\d{4})(?:-(?:Q([1-4])|(0?[1-9]|1[0-2])))?$/);
if (match || month) {
delete frozen.month; delete frozen.quarter; delete frozen.monthStart; delete frozen.monthEnd;
if (match) {
frozen.year = Number(match[1]);
if (match[2]) frozen.quarter = Number(match[2]);
if (match[3]) frozen.month = Number(match[3]);
} else if (month) frozen.month = month;
}
if (row && view !== 'overall') {
const dimension = view === 'projectGroup' ? 'projectGroup' : view;
frozen[dimension] = row.key === '__UNASSIGNED__' || !row[dimension] && !row.key?.trim() ? '__UNASSIGNED__' : row[dimension] || row.key;
}
return frozen;
}