-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathreports.ts
62 lines (57 loc) · 1.62 KB
/
reports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { useMemo } from 'react';
import { q } from '../../shared/query';
import {
type CustomReportData,
type CustomReportEntity,
} from '../../types/models';
import { useLiveQuery } from '../query-hooks';
function toJS(rows: CustomReportData[]) {
const reports: CustomReportEntity[] = rows.map(row => {
const report: CustomReportEntity = {
id: row.id,
name: row.name,
startDate: row.start_date,
endDate: row.end_date,
isDateStatic: row.date_static === 1,
dateRange: row.date_range,
mode: row.mode,
groupBy: row.group_by,
interval: row.interval,
balanceType: row.balance_type,
showEmpty: row.show_empty === 1,
showOffBudget: row.show_offbudget === 1,
showHiddenCategories: row.show_hidden === 1,
includeCurrentInterval: row.include_current === 1,
showUncategorized: row.show_uncategorized === 1,
graphType: row.graph_type,
conditions: row.conditions,
conditionsOp: row.conditions_op ?? 'and',
data: row.metadata,
};
return report;
});
return reports;
}
export function useReports() {
const queryData = useLiveQuery<CustomReportData[]>(
() => q('custom_reports').select('*'),
[],
);
// Sort reports by alphabetical order
function sort(reports: CustomReportEntity[]) {
return reports.sort((a, b) =>
a.name && b.name
? a.name.trim().localeCompare(b.name.trim(), undefined, {
ignorePunctuation: true,
})
: 0,
);
}
return useMemo(
() => ({
isLoading: queryData === null,
data: sort(toJS(queryData || [])),
}),
[queryData],
);
}