-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathActionButtons.tsx
More file actions
261 lines (235 loc) · 7.79 KB
/
ActionButtons.tsx
File metadata and controls
261 lines (235 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
"use client";
import { track } from "@vercel/analytics";
import { Info } from "lucide-react";
import Link from "next/link";
import { useRouter, useSearchParams } from "next/navigation";
import { type KeyboardEvent, useState } from "react";
import { Button as Btn } from "@/components/ui/button";
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@/components/ui/combobox";
import { Input } from "@/components/ui/input";
import { Item, ItemContent, ItemTitle } from "@/components/ui/item";
import { SEARCH_PARAMETERS } from "@/constants";
import { useDois } from "@/data/fetch";
import { cn } from "@/lib/utils";
import type { Entity } from "@/types";
export default function ActionButtons(props: { entity: Entity }) {
return (
<ButtonsGrid>
<FilterByRegistrationYear entity={props.entity} />
<FilterByResourceType entity={props.entity} />
<FilterByQuery entity={props.entity} />
<a
href="https://support.datacite.org/docs/queries"
target="_blank"
rel="noopener"
className="text-datacite-blue-light text-[0.8em] flex items-center gap-1 -col-start-2"
>
<Info size={"1em"} /> How can I use filter queries?
</a>
</ButtonsGrid>
);
}
function Button(props: React.ComponentProps<typeof Btn>) {
return (
<Btn
{...props}
variant="outline"
className={cn("text-xs px-6 py-2 w-full h-full", props.className)}
/>
);
}
function ButtonsGrid(props: React.ComponentProps<"div">) {
return (
<div
{...props}
className="w-full grid grid-cols-4 auto-rows-min md:grid-cols-[repeat(2,1fr)_2fr] gap-x-2 gap-y-1 justify-items-end"
/>
);
}
function FilterByRegistrationYear(props: { entity: Entity }) {
const router = useRouter();
const searchParams = useSearchParams();
const [open, setOpen] = useState(false);
const { isPending, isError, data, error } = useDois(props.entity);
if (isError) return `Error: ${error}`;
const value =
data?.registrationYears.find(
(y) => y.id === searchParams.get(SEARCH_PARAMETERS.REGISTRATION_YEAR),
) || null;
function onItemClick() {
track("filters", {
on: "registration year",
action: "filter clicked",
});
setOpen(false);
}
return (
<Combobox
items={data?.registrationYears ?? []}
itemToStringValue={(item) => item.id}
itemToStringLabel={(item) => item.title}
value={value}
open={open}
onOpenChange={setOpen}
disabled={isPending}
>
<ComboboxInput
placeholder="Filter by Registration Year"
showClear={!!value}
onClear={() => {
const params = new URLSearchParams(searchParams.toString());
params.delete(SEARCH_PARAMETERS.REGISTRATION_YEAR);
router.push(`/${props.entity.id}?${params.toString()}`);
}}
className="text-xs bg-white w-full h-full rounded-[60px] px-2"
/>
<ComboboxContent>
<ComboboxEmpty>No years found.</ComboboxEmpty>
<ComboboxList>
{(item) => {
const params = new URLSearchParams(searchParams.toString());
params.set(SEARCH_PARAMETERS.REGISTRATION_YEAR, item.id);
if (
searchParams.get(SEARCH_PARAMETERS.REGISTRATION_YEAR) === item.id
)
params.delete(SEARCH_PARAMETERS.REGISTRATION_YEAR);
const href = `/${props.entity.id}?${params.toString()}`;
return (
<ComboboxItem key={item.id} value={item.id} onClick={onItemClick}>
<Link href={href} prefetch className="size-full">
<Item size="sm" className="px-0 py-0.5">
<ItemContent className="gap-0">
<ItemTitle>{item.title}</ItemTitle>
</ItemContent>
</Item>
</Link>
</ComboboxItem>
);
}}
</ComboboxList>
</ComboboxContent>
</Combobox>
);
}
function FilterByResourceType(props: { entity: Entity }) {
const router = useRouter();
const searchParams = useSearchParams();
const [open, setOpen] = useState(false);
const { isPending, isError, data, error } = useDois(props.entity);
if (isError) return `Error: ${error}`;
const value =
data?.resourceTypeData.find(
(rt) => rt.id === searchParams.get(SEARCH_PARAMETERS.RESOURCE_TYPE),
) || null;
function onItemClick() {
track("filters", {
on: "resource type",
action: "filter clicked",
});
setOpen(false);
}
return (
<Combobox
items={data?.resourceTypeData ?? []}
itemToStringValue={(item) => item.id}
itemToStringLabel={(item) => item.type}
value={value}
open={open}
onOpenChange={setOpen}
disabled={isPending}
>
<ComboboxInput
placeholder="Filter by Resource Type"
showClear={!!value}
onClear={() => {
const params = new URLSearchParams(searchParams.toString());
params.delete(SEARCH_PARAMETERS.RESOURCE_TYPE);
router.push(`/${props.entity.id}?${params.toString()}`);
}}
className="text-xs bg-white w-full h-full rounded-[60px] px-2"
/>
<ComboboxContent>
<ComboboxEmpty>No resource types found.</ComboboxEmpty>
<ComboboxList>
{(item) => {
const params = new URLSearchParams(searchParams.toString());
params.set(SEARCH_PARAMETERS.RESOURCE_TYPE, item.id);
if (searchParams.get(SEARCH_PARAMETERS.RESOURCE_TYPE) === item.id)
params.delete(SEARCH_PARAMETERS.RESOURCE_TYPE);
const href = `/${props.entity.id}?${params.toString()}`;
return (
<ComboboxItem key={item.id} value={item.id} onClick={onItemClick}>
<Link href={href} prefetch className="size-full">
<Item size="sm" className="px-0 py-0.5">
<ItemContent className="gap-0">
<ItemTitle>{item.type}</ItemTitle>
</ItemContent>
</Item>
</Link>
</ComboboxItem>
);
}}
</ComboboxList>
</ComboboxContent>
</Combobox>
);
}
function FilterByQuery(props: { entity: Entity }) {
const router = useRouter();
const searchParams = useSearchParams();
const [query, setQuery] = useState(
searchParams.get(SEARCH_PARAMETERS.QUERY) || "",
);
const params = new URLSearchParams(searchParams.toString());
params.set(SEARCH_PARAMETERS.QUERY, query);
if (!query.trim()) params.delete(SEARCH_PARAMETERS.QUERY);
const href = `/${props.entity.id}?${params.toString()}`;
const disabled = !query.trim();
const trackQuery = () =>
track("filters", {
on: "query",
action: "query submitted",
});
function onKeyDown(e: KeyboardEvent<HTMLInputElement>) {
if (e.key === "Enter") {
e.preventDefault();
trackQuery();
router.push(href);
}
}
return (
<div className="max-md:col-span-2 flex size-full pl-6">
<Input
title="Filter using query string syntax"
placeholder="Filter using query string syntax…"
className="text-xs bg-white px-6 py-2 h-full rounded-l-[60px] border-r-0"
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyDown={onKeyDown}
/>
<Button
type="submit"
variant="outline"
className={cn(
"rounded-l-none rounded-r-[60px] border-l-0 w-min",
!disabled &&
"bg-datacite-blue-light border-datacite-blue-light text-white hover:bg-datacite-blue-light/90 hover:text-white",
)}
disabled={disabled}
asChild={!disabled}
onClick={trackQuery}
>
<Link href={href} prefetch>
Filter by Query
</Link>
</Button>
</div>
);
}