-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_index.tsx
208 lines (193 loc) · 6.65 KB
/
_index.tsx
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
import { parseWithZod } from "@conform-to/zod";
import { Anchor, Card, Container, Divider, Group, Stack } from "@mantine/core";
import { data, Link, redirect, useNavigation } from "react-router";
import { getQuery, withQuery } from "ufo";
import Fa6SolidMagnifyingGlass from "~icons/fa6-solid/magnifying-glass";
import { Notes } from "../components/note/Notes";
import { SearchForm } from "../feature/search/components/SearchForm";
import { SearchPagination } from "../feature/search/components/SearchPagination";
import { noteSearchParamSchema } from "../feature/search/validation";
import {
getTopicsApiV1DataTopicsGet,
searchApiV1DataSearchGet,
} from "../generated/api/client";
import type { SearchedNote, Topic } from "../generated/api/schemas";
import type { Route } from "./+types/_index";
export const meta: Route.MetaFunction = () => {
return [
{ title: "BirdXplorer" },
{
name: "description",
content:
"BirdXplorer is software that helps users explore community notes data on X (formerly known as Twitter).",
},
{
name: "robots",
content: "noindex, nofollow",
},
];
};
export const links: Route.LinksFunction = () => {
return [
{
rel: "canonical",
href: "https://birdxplorer.code4japan.org",
},
];
};
export const loader = async (args: Route.LoaderArgs) => {
const rawSearchParams = getQuery(args.request.url);
const searchQuery =
await noteSearchParamSchema.safeParseAsync(rawSearchParams);
if (!searchQuery.success) {
return data(
{
data: {
searchQuery: null,
searchResults: {
data: [],
meta: {
next: null,
prev: null,
},
},
topics: [],
},
error: searchQuery.error.errors,
},
{
status: 400,
statusText: "Bad Request",
},
);
}
const [topics, response] = await Promise.all([
// TODO: Topics を毎回 fetch するのは無駄なので、ハードナビゲーション時に fetch してブラウザ側で状態管理するように変更する
getTopicsApiV1DataTopicsGet(),
searchApiV1DataSearchGet(searchQuery.data),
]);
return {
data: {
searchQuery: searchQuery.data,
searchResults: response.data,
topics: topics.data.data,
},
error: null,
};
};
export default function Index({
actionData,
loaderData,
}: Route.ComponentProps) {
const isLoadingSearchResults = useNavigation().state !== "idle";
const {
topics,
searchQuery,
searchResults: { data: notes, meta: paginationMeta },
} = loaderData.data;
return (
<>
<header className="border-b border-gray-300">
<Container className="p-4" size="lg">
<h1 className="text-2xl font-bold">BirdXplorer</h1>
</Container>
</header>
<main>
<Container className="p-4" size="md">
<div className="grid grid-cols-1 gap-4 md:grid-cols-3 md:gap-8">
<div className="md:col-span-1">
<h2 className="sr-only">コミュニティノートを検索する</h2>
<SearchForm
defaultValue={searchQuery ?? undefined}
lastResult={actionData}
topics={
// react-router の型がうまく機能せず topics が unknown になったため
topics as Topic[]
}
/>
</div>
<Divider className="md:hidden" />
<div className="size-full md:col-span-2">
<h2 className="sr-only">コミュニティノートの検索結果</h2>
<Stack className="size-full">
{notes.length > 0 ? (
<>
{searchQuery && (
<SearchPagination
className="ms-auto me-0"
currentQuery={searchQuery}
loading={isLoadingSearchResults}
meta={paginationMeta}
visibleItemCount={notes.length}
/>
)}
<Group gap="lg">
<Notes
notes={
// react-router の型がうまく機能せず notes[number].topics が unknown になったため
notes as SearchedNote[]
}
/>
</Group>
{searchQuery && (
<SearchPagination
className="ms-auto me-0"
currentQuery={searchQuery}
loading={isLoadingSearchResults}
meta={paginationMeta}
visibleItemCount={notes.length}
/>
)}
</>
) : (
<Card
className="grid size-full place-content-center"
padding="lg"
radius="md"
w="100%"
withBorder
>
<div className="flex flex-col items-center justify-center gap-4 text-zinc-600">
<Fa6SolidMagnifyingGlass className="text-4xl text-current" />
<span className="text-center text-lg font-semibold text-balance">
コミュニティノートが見つかりませんでした
</span>
</div>
</Card>
)}
</Stack>
</div>
</div>
</Container>
</main>
<footer className="sticky top-full border-t border-zinc-300">
<Container className="flex justify-center p-4 md:justify-end" size="lg">
<p className="inline-flex flex-col items-center justify-center gap-2 text-sm font-semibold text-zinc-700 md:flex-row md:gap-4">
<span>© 2025 Code for Japan</span>
<Anchor
c="pink"
component={Link}
size="sm"
target="_blank"
to="https://www.code4japan.org/"
underline="hover"
>
一般社団法人 コード・フォー・ジャパン
</Anchor>
</p>
</Container>
</footer>
</>
);
}
export const action = async ({ request }: Route.ActionArgs) => {
const formData = await request.formData();
const submission = parseWithZod(formData, {
schema: noteSearchParamSchema,
});
if (submission.status !== "success") {
return submission.reply();
}
const destination = withQuery("/", submission.value);
return redirect(destination);
};