fix: decouple dynamic report from global page date filters
The dynamic report now relies exclusively on its own panel filters instead of inheriting the global period selector date range. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
04ec221808
commit
2ae7fb301c
5 changed files with 7 additions and 47 deletions
|
|
@ -12,11 +12,9 @@ interface DynamicReportProps {
|
|||
config: PivotConfig;
|
||||
result: PivotResult;
|
||||
onConfigChange: (config: PivotConfig) => void;
|
||||
dateFrom?: string;
|
||||
dateTo?: string;
|
||||
}
|
||||
|
||||
export default function DynamicReport({ config, result, onConfigChange, dateFrom, dateTo }: DynamicReportProps) {
|
||||
export default function DynamicReport({ config, result, onConfigChange }: DynamicReportProps) {
|
||||
const { t } = useTranslation();
|
||||
const [viewMode, setViewMode] = useState<ViewMode>("table");
|
||||
const [fullscreen, setFullscreen] = useState(false);
|
||||
|
|
@ -101,8 +99,6 @@ export default function DynamicReport({ config, result, onConfigChange, dateFrom
|
|||
<DynamicReportPanel
|
||||
config={config}
|
||||
onChange={onConfigChange}
|
||||
dateFrom={dateFrom}
|
||||
dateTo={dateTo}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -10,11 +10,9 @@ const ALL_MEASURES: PivotMeasureId[] = ["periodic", "ytd"];
|
|||
interface DynamicReportPanelProps {
|
||||
config: PivotConfig;
|
||||
onChange: (config: PivotConfig) => void;
|
||||
dateFrom?: string;
|
||||
dateTo?: string;
|
||||
}
|
||||
|
||||
export default function DynamicReportPanel({ config, onChange, dateFrom, dateTo }: DynamicReportPanelProps) {
|
||||
export default function DynamicReportPanel({ config, onChange }: DynamicReportPanelProps) {
|
||||
const { t } = useTranslation();
|
||||
const [menuTarget, setMenuTarget] = useState<{ id: string; type: "field" | "measure"; x: number; y: number } | null>(null);
|
||||
const [filterValues, setFilterValues] = useState<Record<string, string[]>>({});
|
||||
|
|
@ -36,12 +34,12 @@ export default function DynamicReportPanel({ config, onChange, dateFrom, dateTo
|
|||
useEffect(() => {
|
||||
for (const fieldId of filterFieldIds) {
|
||||
if (!filterValues[fieldId]) {
|
||||
getDynamicFilterValues(fieldId as PivotFieldId, dateFrom, dateTo).then((vals) => {
|
||||
getDynamicFilterValues(fieldId as PivotFieldId).then((vals) => {
|
||||
setFilterValues((prev) => ({ ...prev, [fieldId]: vals }));
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [filterFieldIds.join(","), dateFrom, dateTo]);
|
||||
}, [filterFieldIds.join(",")]);
|
||||
|
||||
// Close menu on outside click
|
||||
useEffect(() => {
|
||||
|
|
|
|||
|
|
@ -191,8 +191,7 @@ export function useReports() {
|
|||
dispatch({ type: "SET_PIVOT_RESULT", payload: { rows: [], columnValues: [], dimensionLabels: {} } });
|
||||
break;
|
||||
}
|
||||
const { dateFrom, dateTo } = computeDateRange(period, customFrom, customTo);
|
||||
const data = await getDynamicReportData(pivotCfg, dateFrom, dateTo);
|
||||
const data = await getDynamicReportData(pivotCfg);
|
||||
if (fetchId !== fetchIdRef.current) return;
|
||||
dispatch({ type: "SET_PIVOT_RESULT", payload: data });
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -138,8 +138,6 @@ export default function ReportsPage() {
|
|||
config={state.pivotConfig}
|
||||
result={state.pivotResult}
|
||||
onConfigChange={setPivotConfig}
|
||||
dateFrom={dateFrom}
|
||||
dateTo={dateTo}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
|
|
|||
|
|
@ -168,8 +168,6 @@ function needsCategoryJoin(fields: PivotFieldId[]): boolean {
|
|||
|
||||
export async function getDynamicReportData(
|
||||
config: PivotConfig,
|
||||
dateFrom?: string,
|
||||
dateTo?: string,
|
||||
): Promise<PivotResult> {
|
||||
const db = await getDb();
|
||||
|
||||
|
|
@ -202,17 +200,6 @@ export async function getDynamicReportData(
|
|||
const params: unknown[] = [];
|
||||
let paramIndex = 1;
|
||||
|
||||
if (dateFrom) {
|
||||
whereClauses.push(`t.date >= $${paramIndex}`);
|
||||
params.push(dateFrom);
|
||||
paramIndex++;
|
||||
}
|
||||
if (dateTo) {
|
||||
whereClauses.push(`t.date <= $${paramIndex}`);
|
||||
params.push(dateTo);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
// Apply filter values (include / exclude)
|
||||
for (const fieldId of filterFields) {
|
||||
const entry = config.filters[fieldId];
|
||||
|
|
@ -330,37 +317,19 @@ export async function getDynamicReportData(
|
|||
|
||||
export async function getDynamicFilterValues(
|
||||
fieldId: PivotFieldId,
|
||||
dateFrom?: string,
|
||||
dateTo?: string,
|
||||
): Promise<string[]> {
|
||||
const db = await getDb();
|
||||
const def = FIELD_SQL[fieldId];
|
||||
const useCatJoin = needsCategoryJoin([fieldId]);
|
||||
|
||||
const whereClauses: string[] = [];
|
||||
const params: unknown[] = [];
|
||||
let paramIndex = 1;
|
||||
|
||||
if (dateFrom) {
|
||||
whereClauses.push(`t.date >= $${paramIndex}`);
|
||||
params.push(dateFrom);
|
||||
paramIndex++;
|
||||
}
|
||||
if (dateTo) {
|
||||
whereClauses.push(`t.date <= $${paramIndex}`);
|
||||
params.push(dateTo);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
||||
const joinSQL = useCatJoin
|
||||
? `LEFT JOIN categories c ON t.category_id = c.id
|
||||
LEFT JOIN categories parent_cat ON c.parent_id = parent_cat.id`
|
||||
: "";
|
||||
|
||||
const rows = await db.select<Array<{ val: string }>>(
|
||||
`SELECT DISTINCT ${def.select} AS val FROM transactions t ${joinSQL} ${whereSQL} ORDER BY val`,
|
||||
params,
|
||||
`SELECT DISTINCT ${def.select} AS val FROM transactions t ${joinSQL} ORDER BY val`,
|
||||
[],
|
||||
);
|
||||
return rows.map((r) => r.val);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue