Skip to content

Commit

Permalink
Handle date parsing gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Dec 23, 2024
1 parent 43ce119 commit bebfec1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
14 changes: 14 additions & 0 deletions apps/web/components/dashboard/search/QueryExplainerTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export default function QueryExplainerTooltip({
<TableCell>{matcher.listName}</TableCell>
</TableRow>
);
case "dateAfter":
return (
<TableRow>
<TableCell>Created After</TableCell>
<TableCell>{matcher.dateAfter.toDateString()}</TableCell>
</TableRow>
);
case "dateBefore":
return (
<TableRow>
<TableCell>Created Before</TableCell>
<TableCell>{matcher.dateBefore.toDateString()}</TableCell>
</TableRow>
);
case "favourited":
return (
<TableRow>
Expand Down
44 changes: 30 additions & 14 deletions packages/shared/searchQueryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,37 @@ MATCHER.setPattern(
matcher: { type: "listName", listName: toks[1] },
};
case "after:":
return {
text: "",
matcher: {
type: "dateAfter",
dateAfter: z.coerce.date().parse(toks[1]),
},
};
try {
return {
text: "",
matcher: {
type: "dateAfter",
dateAfter: z.coerce.date().parse(toks[1]),
},
};
} catch (e) {
return {
// If parsing the date fails, emit it as pure text
text: toks[0].text + toks[1],
matcher: undefined,
};
}
case "before:":
return {
text: "",
matcher: {
type: "dateBefore",
dateBefore: z.coerce.date().parse(toks[1]),
},
};
try {
return {
text: "",
matcher: {
type: "dateBefore",
dateBefore: z.coerce.date().parse(toks[1]),
},
};
} catch (e) {
return {
// If parsing the date fails, emit it as pure text
text: toks[0].text + toks[1],
matcher: undefined,
};
}
default:
// If the token is not known, emit it as pure text
return {
Expand Down

0 comments on commit bebfec1

Please sign in to comment.