@genspectrum/dashboard-components 0.19.9 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,55 +45,81 @@ export class DateRangeOptionChangedEvent extends CustomEvent<DateRangeValue> {
45
45
  }
46
46
  }
47
47
 
48
- const today = new Date();
48
+ type DateRangeOptionPresets = {
49
+ last2Weeks: DateRangeOption;
50
+ lastMonth: DateRangeOption;
51
+ last2Months: DateRangeOption;
52
+ last3Months: DateRangeOption;
53
+ last6Months: DateRangeOption;
54
+ lastYear: DateRangeOption;
55
+ allTimes: DateRangeOption;
56
+ };
49
57
 
50
- const twoWeeksAgo = new Date();
51
- twoWeeksAgo.setDate(today.getDate() - 14);
58
+ let dateRangeOptionsPresetsCacheDate: string | null = null;
59
+ let dateRangeOptionPresetsCache: DateRangeOptionPresets | null = null;
52
60
 
53
- const lastMonth = new Date(today);
54
- lastMonth.setMonth(today.getMonth() - 1);
61
+ /**
62
+ * Presets for the `gs-date-range-filter` component that can be used as `dateRangeOptions`.
63
+ */
64
+ export const dateRangeOptionPresets = (): DateRangeOptionPresets => {
65
+ const today = new Date();
66
+ const todayString = new Date().toISOString().slice(0, 10);
55
67
 
56
- const last2Months = new Date(today);
57
- last2Months.setMonth(today.getMonth() - 2);
68
+ if (
69
+ dateRangeOptionPresetsCache === null ||
70
+ dateRangeOptionsPresetsCacheDate === null ||
71
+ dateRangeOptionsPresetsCacheDate !== todayString
72
+ ) {
73
+ dateRangeOptionsPresetsCacheDate = todayString;
58
74
 
59
- const last3Months = new Date(today);
60
- last3Months.setMonth(today.getMonth() - 3);
75
+ const twoWeeksAgo = new Date();
76
+ twoWeeksAgo.setDate(today.getDate() - 14);
61
77
 
62
- const last6Months = new Date(today);
63
- last6Months.setMonth(today.getMonth() - 6);
78
+ const lastMonth = new Date(today);
79
+ lastMonth.setMonth(today.getMonth() - 1);
64
80
 
65
- const lastYear = new Date(today);
66
- lastYear.setFullYear(today.getFullYear() - 1);
81
+ const last2Months = new Date(today);
82
+ last2Months.setMonth(today.getMonth() - 2);
67
83
 
68
- /**
69
- * Presets for the `gs-date-range-filter` component that can be used as `dateRangeOptions`.
70
- */
71
- export const dateRangeOptionPresets = {
72
- last2Weeks: {
73
- label: 'Last 2 weeks',
74
- dateFrom: toYYYYMMDD(twoWeeksAgo),
75
- },
76
- lastMonth: {
77
- label: 'Last month',
78
- dateFrom: toYYYYMMDD(lastMonth),
79
- },
80
- last2Months: {
81
- label: 'Last 2 months',
82
- dateFrom: toYYYYMMDD(last2Months),
83
- },
84
- last3Months: {
85
- label: 'Last 3 months',
86
- dateFrom: toYYYYMMDD(last3Months),
87
- },
88
- last6Months: {
89
- label: 'Last 6 months',
90
- dateFrom: toYYYYMMDD(last6Months),
91
- },
92
- lastYear: {
93
- label: 'Last year',
94
- dateFrom: toYYYYMMDD(lastYear),
95
- },
96
- allTimes: {
97
- label: 'All times',
98
- },
99
- } satisfies Record<string, DateRangeOption>;
84
+ const last3Months = new Date(today);
85
+ last3Months.setMonth(today.getMonth() - 3);
86
+
87
+ const last6Months = new Date(today);
88
+ last6Months.setMonth(today.getMonth() - 6);
89
+
90
+ const lastYear = new Date(today);
91
+ lastYear.setFullYear(today.getFullYear() - 1);
92
+
93
+ dateRangeOptionPresetsCache = {
94
+ last2Weeks: {
95
+ label: 'Last 2 weeks',
96
+ dateFrom: toYYYYMMDD(twoWeeksAgo),
97
+ },
98
+ lastMonth: {
99
+ label: 'Last month',
100
+ dateFrom: toYYYYMMDD(lastMonth),
101
+ },
102
+ last2Months: {
103
+ label: 'Last 2 months',
104
+ dateFrom: toYYYYMMDD(last2Months),
105
+ },
106
+ last3Months: {
107
+ label: 'Last 3 months',
108
+ dateFrom: toYYYYMMDD(last3Months),
109
+ },
110
+ last6Months: {
111
+ label: 'Last 6 months',
112
+ dateFrom: toYYYYMMDD(last6Months),
113
+ },
114
+ lastYear: {
115
+ label: 'Last year',
116
+ dateFrom: toYYYYMMDD(lastYear),
117
+ },
118
+ allTimes: {
119
+ label: 'All times',
120
+ },
121
+ };
122
+ }
123
+
124
+ return dateRangeOptionPresetsCache;
125
+ };
@@ -59,14 +59,14 @@ const meta: Meta<Required<DateRangeFilterProps>> = {
59
59
  },
60
60
  args: {
61
61
  dateRangeOptions: [
62
- dateRangeOptionPresets.lastMonth,
63
- dateRangeOptionPresets.last3Months,
64
- dateRangeOptionPresets.allTimes,
62
+ dateRangeOptionPresets().lastMonth,
63
+ dateRangeOptionPresets().last3Months,
64
+ dateRangeOptionPresets().allTimes,
65
65
  { label: '2021', dateFrom: '2021-01-01', dateTo: '2021-12-31' },
66
66
  customDateRange,
67
67
  ],
68
68
  earliestDate: '1970-01-01',
69
- value: dateRangeOptionPresets.lastMonth.label,
69
+ value: dateRangeOptionPresets().lastMonth.label,
70
70
  lapisDateField: 'aDateColumn',
71
71
  width: '100%',
72
72
  placeholder: 'Date range',