Skip to content

Commit 4315c83

Browse files
committed
chore: document filters
1 parent 1f7e748 commit 4315c83

File tree

1 file changed

+55
-6
lines changed

1 file changed

+55
-6
lines changed

apps/client/pages/documents/index.tsx

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ import { getCookie } from "cookies-next";
33
import { Ellipsis } from "lucide-react";
44
import useTranslation from "next-translate/useTranslation";
55
import { useRouter } from "next/router";
6-
import { useEffect } from "react";
6+
import { useEffect, useState } from "react";
77
import { useQuery } from "react-query";
88
import { useUser } from "../../store/session";
9+
import {
10+
Select,
11+
SelectContent,
12+
SelectItem,
13+
SelectTrigger,
14+
SelectValue,
15+
} from "@/shadcn/ui/select";
16+
import { Input } from "@/shadcn/ui/input";
917

1018
function groupDocumentsByDate(notebooks) {
1119
const today = new Date();
@@ -66,6 +74,8 @@ async function fetchNotebooks(token) {
6674

6775
export default function NoteBooksIndex() {
6876
const { t } = useTranslation("peppermint");
77+
const [sortBy, setSortBy] = useState("updatedAt");
78+
const [searchQuery, setSearchQuery] = useState("");
6979

7080
const token = getCookie("session");
7181
const { data, status, error } = useQuery("getUsersNotebooks", () =>
@@ -94,13 +104,47 @@ export default function NoteBooksIndex() {
94104
});
95105
}
96106

107+
const sortedAndFilteredNotebooks = (notebooks) => {
108+
if (!notebooks) return [];
109+
110+
// First filter by search query
111+
let filtered = notebooks.filter((notebook) =>
112+
notebook.title.toLowerCase().includes(searchQuery.toLowerCase())
113+
);
114+
115+
// Then sort
116+
return filtered.sort((a, b) => {
117+
const dateA = new Date(a[sortBy]);
118+
const dateB = new Date(b[sortBy]);
119+
//@ts-ignore
120+
return dateB - dateA;
121+
});
122+
};
123+
97124
return (
98125
<div className="px-4 py-4 sm:px-6 lg:px-8">
99126
<div className="sm:flex sm:items-center">
100127
<div className="sm:flex-auto">
101128
<h1 className="text-base font-semibold leading-6 text-foreground">
102129
Documents
103130
</h1>
131+
<div className="flex items-center w-full justify-center flex-row space-x-2 flex-1 mr-2">
132+
<Input
133+
placeholder="Search documents..."
134+
value={searchQuery}
135+
onChange={(e) => setSearchQuery(e.target.value)}
136+
className="max-w-xs"
137+
/>
138+
<Select value={sortBy} onValueChange={setSortBy}>
139+
<SelectTrigger className="w-[180px]">
140+
<SelectValue placeholder="Sort by" />
141+
</SelectTrigger>
142+
<SelectContent>
143+
<SelectItem value="updatedAt">Last Updated</SelectItem>
144+
<SelectItem value="createdAt">Created Date</SelectItem>
145+
</SelectContent>
146+
</Select>
147+
</div>
104148
{/* <p className="mt-2 text-sm text-foreground">
105149
Documents can be private, shared with others, or public.
106150
</p> */}
@@ -118,13 +162,18 @@ export default function NoteBooksIndex() {
118162
</div>
119163
) : (
120164
<div className="flex flex-col w-full max-w-2xl justify-center space-y-4">
121-
<div className="flex flex-row justify-end mb-4">
122-
<Button variant="outline" size="sm" onClick={() => createNew()}>
123-
New Document
124-
</Button>
165+
<div className="flex flex-col mb-4">
166+
<div className="flex w-full justify-end">
167+
<Button variant="outline" size="sm" onClick={() => createNew()}>
168+
New Document
169+
</Button>
170+
</div>
125171
</div>
172+
126173
{data?.notebooks &&
127-
Object.entries(groupDocumentsByDate(data.notebooks)).map(
174+
Object.entries(
175+
groupDocumentsByDate(sortedAndFilteredNotebooks(data.notebooks))
176+
).map(
128177
([period, docs]) =>
129178
Array.isArray(docs) &&
130179
docs.length > 0 && (

0 commit comments

Comments
 (0)