Skip to content

Commit 653b684

Browse files
committed
Fix custom shortcuts types
1 parent 5221164 commit 653b684

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

src/components/Shortcuts.tsx

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

9393
const shortcutOptions = useMemo<[string, ShortcutsItem | ShortcutsItem[]][]>(() => {
94-
let options;
95-
if (configs?.shortcuts && configs?.shortcuts) {
96-
const formatConfig = Object.keys(configs.shortcuts).map(item => {
94+
const options: [string, ShortcutsItem | ShortcutsItem[]][] = [];
95+
if (configs && configs.shortcuts) {
96+
Object.keys(configs.shortcuts).forEach(item => {
9797
if (Object.keys(DEFAULT_SHORTCUTS).includes(item)) {
98-
return [item, DEFAULT_SHORTCUTS[item]];
98+
options.push([item, DEFAULT_SHORTCUTS[item]]);
9999
} else {
100-
// using | makes this fail in typecheck as [string] is no longer recognised?
101-
if (configs.shortcuts && configs?.shortcuts[item]) {
102-
const customConfig = configs?.shortcuts[item];
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-
return [
113-
item,
114-
{
100+
if (
101+
configs.shortcuts &&
102+
configs.shortcuts.custom &&
103+
configs.shortcuts.custom.length > 0
104+
) {
105+
configs.shortcuts.custom.forEach(customConfig => {
106+
const text = customConfig.text;
107+
const start = dayjs(customConfig.period.start);
108+
const end = dayjs(customConfig.period.end);
109+
if (
110+
text &&
111+
start.isValid() &&
112+
end.isValid() &&
113+
(start.isBefore(end) || start.isSame(end))
114+
) {
115+
options.push([
115116
text,
116-
period: {
117-
start: start.format(DATE_FORMAT),
118-
end: end.format(DATE_FORMAT)
117+
{
118+
text,
119+
period: {
120+
start: start.format(DATE_FORMAT),
121+
end: end.format(DATE_FORMAT)
122+
}
119123
}
120-
}
121-
];
122-
} else {
123-
return undefined;
124-
}
124+
]);
125+
}
126+
});
125127
}
126-
return undefined;
127128
}
128129
});
129-
options = formatConfig?.filter(item => !!item);
130130
} else {
131-
options = Object.entries(DEFAULT_SHORTCUTS);
131+
return Object.entries(DEFAULT_SHORTCUTS);
132132
}
133-
return options as [string, ShortcutsItem | ShortcutsItem[]][];
133+
return options;
134134
}, [configs]);
135135

136136
const printItemText = useCallback((item: ShortcutsItem) => {
@@ -140,7 +140,7 @@ const Shortcuts: React.FC = () => {
140140
return shortcutOptions?.length ? (
141141
<div className="md:border-b mb-3 lg:mb-0 lg:border-r lg:border-b-0 border-gray-300 dark:border-gray-700 pr-1">
142142
<ul className="w-full tracking-wide flex flex-wrap lg:flex-col pb-1 lg:pb-0">
143-
{shortcutOptions?.map(([key, item], index: number) =>
143+
{shortcutOptions.map(([key, item], index: number) =>
144144
Array.isArray(item) ? (
145145
item.map((item, index) => (
146146
<ItemTemplate key={index} item={item}>

src/types/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ export interface Period {
55
end: string | null;
66
}
77

8+
interface DefaultShortcuts {
9+
today?: string;
10+
yesterday?: string;
11+
past?: (period: number) => string;
12+
currentMonth?: string;
13+
pastMonth?: string;
14+
custom?: ShortcutsItem[];
15+
}
816
export interface Configs {
9-
shortcuts?: {
10-
today?: string;
11-
yesterday?: string;
12-
past?: (period: number) => string;
13-
currentMonth?: string;
14-
pastMonth?: string;
15-
} & { [key: string]: ShortcutsItem };
17+
shortcuts?: DefaultShortcuts;
1618
footer?: {
1719
cancel?: string;
1820
apply?: string;
1921
};
2022
}
2123

2224
export interface ShortcutsItem {
23-
text?: string;
25+
text: string;
2426
daysNumber?: number;
25-
period?: {
26-
start: string;
27-
end: string;
27+
period: {
28+
start: Date | string;
29+
end: Date | string;
2830
};
2931
}
3032

0 commit comments

Comments
 (0)