Skip to content

Commit 175ff29

Browse files
committed
Revert the api changes keeping types fixed
1 parent 23c955f commit 175ff29

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

src/components/Shortcuts.tsx

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -91,41 +91,43 @@ const Shortcuts: React.FC = () => {
9191
}, []);
9292

9393
const shortcutOptions = useMemo<[string, ShortcutsItem | ShortcutsItem[]][]>(() => {
94-
const options: [string, ShortcutsItem | ShortcutsItem[]][] = [];
95-
if (configs && configs.shortcuts) {
96-
Object.keys(configs.shortcuts).forEach(item => {
97-
if (Object.keys(DEFAULT_SHORTCUTS).includes(item)) {
98-
options.push([item, DEFAULT_SHORTCUTS[item]]);
99-
}
100-
});
101-
if (configs.shortcuts.custom && configs.shortcuts.custom.length > 0) {
102-
configs.shortcuts.custom.forEach(customConfig => {
103-
const text = customConfig.text;
104-
const start = dayjs(customConfig.period.start);
105-
const end = dayjs(customConfig.period.end);
106-
if (
107-
text &&
108-
start.isValid() &&
109-
end.isValid() &&
110-
(start.isBefore(end) || start.isSame(end))
111-
) {
112-
options.push([
94+
if (!configs?.shortcuts) {
95+
return Object.entries(DEFAULT_SHORTCUTS);
96+
}
97+
98+
return Object.entries(configs.shortcuts).flatMap(([key, customConfig]) => {
99+
if (Object.prototype.hasOwnProperty.call(DEFAULT_SHORTCUTS, key)) {
100+
return [[key, DEFAULT_SHORTCUTS[key]]];
101+
}
102+
103+
const { text, period } = customConfig as {
104+
text: string;
105+
period: { start: string; end: string };
106+
};
107+
if (!text || !period) {
108+
return [];
109+
}
110+
111+
const start = dayjs(period.start);
112+
const end = dayjs(period.end);
113+
114+
if (start.isValid() && end.isValid() && (start.isBefore(end) || start.isSame(end))) {
115+
return [
116+
[
117+
text,
118+
{
113119
text,
114-
{
115-
text,
116-
period: {
117-
start: start.format(DATE_FORMAT),
118-
end: end.format(DATE_FORMAT)
119-
}
120+
period: {
121+
start: start.format(DATE_FORMAT),
122+
end: end.format(DATE_FORMAT)
120123
}
121-
]);
122-
}
123-
});
124+
}
125+
]
126+
];
124127
}
125-
} else {
126-
return Object.entries(DEFAULT_SHORTCUTS);
127-
}
128-
return options;
128+
129+
return [];
130+
});
129131
}, [configs]);
130132

131133
const printItemText = useCallback((item: ShortcutsItem) => {

src/types/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ export interface Period {
77
end: string | null;
88
}
99

10+
interface CustomShortcuts {
11+
[key: string]: ShortcutsItem;
12+
}
13+
1014
interface DefaultShortcuts {
1115
today?: string;
1216
yesterday?: string;
1317
past?: (period: number) => string;
1418
currentMonth?: string;
1519
pastMonth?: string;
16-
custom?: ShortcutsItem[];
1720
}
1821
export interface Configs {
19-
shortcuts?: DefaultShortcuts;
22+
shortcuts?: DefaultShortcuts | CustomShortcuts;
2023
footer?: {
2124
cancel?: string;
2225
apply?: string;

0 commit comments

Comments
 (0)