feat: implement time filter

This commit is contained in:
Derrick Hammer 2023-12-23 07:10:18 -05:00
parent 0c404bd3b1
commit 917511a449
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 34 additions and 4 deletions

View File

@ -32,6 +32,7 @@ const SearchBar = ({ sites }: { sites: SiteList }) => {
const [dirtyInput, setDirtyInput] = useState(false);
const [results, setResults] = useState<SearchResult[]>([]);
const [selectedSite, setSelectedSite] = useState(null);
const [selectedTime, setSelectedTime] = useState<string | null>(null);
const fetcher = useFetcher({ key: "seach" });
@ -58,6 +59,12 @@ const SearchBar = ({ sites }: { sites: SiteList }) => {
newSearchParams.set("site", selectedSite);
}
if (selectedTime) {
const timestampInMilliseconds = Date.parse(selectedTime); // Date.parse returns the timestamp in milliseconds
const timestamp = timestampInMilliseconds / 1000;
newSearchParams.set("time", timestamp.toString());
}
fetcher.load(`/api/search?${newSearchParams}`);
}
@ -71,7 +78,7 @@ const SearchBar = ({ sites }: { sites: SiteList }) => {
useEffect(() => {
doSearch();
}, [selectedSite]);
}, [selectedSite, selectedTime]);
const isActive = results.length > 0 || dirtyInput;
@ -159,7 +166,10 @@ const SearchBar = ({ sites }: { sites: SiteList }) => {
{/* Dropdown component should be here */}
<SitesCombobox siteList={sites} onSiteSelect={setSelectedSite} />
{/* Dropdown component should be here */}
<Select defaultValue={"0"}>
<Select
defaultValue={"0"}
onValueChange={(v) => setSelectedTime(v)}
>
<SelectTrigger className="hover:bg-muted w-auto">
<SelectValue placeholder="Time ago" />
</SelectTrigger>

View File

@ -12,7 +12,17 @@ export const prisma = (globalForPrisma.prisma || new PrismaClient()).$extends({
// Index the created record in MeiliSearch
const index = search.index("articles");
await index.addDocuments([createdRecord]);
const timestampInMilliseconds = Date.parse(
createdRecord.createdAt as any
); // Date.parse returns the timestamp in milliseconds
const timestamp = timestampInMilliseconds / 1000;
await index.addDocuments([
{
...createdRecord,
createdTimestamp: timestamp,
},
]);
return createdRecord;
},

View File

@ -13,9 +13,19 @@ export async function loader({ request }: LoaderFunctionArgs) {
}
const site = searchParams.get("site");
const time = searchParams.get("time");
let filters = [];
if (site) {
filters.push(`siteKey = ${site}`);
}
if (time) {
filters.push(`createdTimestamp >= ${parseInt(time).toString()}`);
}
const results = await search.index("articles").search(query, {
filter: site ? `siteKey = ${site}` : undefined,
filter: filters.length ? filters.join(" AND ") : undefined,
});
return results.hits.map((item) => {