-
Notifications
You must be signed in to change notification settings - Fork 198
/
PagesManager.tsx
302 lines (287 loc) · 9.53 KB
/
PagesManager.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { Fragment, useMemo } from "react"
import { getPageVisibility } from "~/lib/page-helpers"
import { useDate } from "~/hooks/useDate"
import { TabItem, Tabs } from "../ui/Tabs"
import { Menu } from "@headlessui/react"
import { cn } from "~/lib/utils"
import { PageVisibilityEnum } from "~/lib/types"
import { DashboardMain } from "./DashboardMain"
import { useRouter } from "next/router"
import Link from "next/link"
import { EmptyState } from "../ui/EmptyState"
import { useGetPagesBySite } from "~/queries/page"
import { setStorage } from "~/lib/storage"
import { useQueryClient } from "@tanstack/react-query"
import { Button } from "../ui/Button"
import { UniLink } from "../ui/UniLink"
import { nanoid } from "nanoid"
import { Tooltip } from "../ui/Tooltip"
import { Trans, useTranslation } from "next-i18next"
import { readFiles } from "~/lib/read-files"
import { PagesManagerMenu } from "./PagesManagerMenu"
export const PagesManager: React.FC<{
isPost: boolean
}> = ({ isPost }) => {
const router = useRouter()
const subdomain = router.query.subdomain as string
const visibility = useMemo<PageVisibilityEnum>(
() =>
router.query.visibility
? (router.query.visibility as PageVisibilityEnum)
: PageVisibilityEnum.All,
[router.query.visibility],
)
const { t } = useTranslation(["dashboard", "site"])
const date = useDate()
const pages = useGetPagesBySite({
type: isPost ? "post" : "page",
site: subdomain!,
take: 100,
visibility,
})
const tabItems: TabItem[] = [
{
value: PageVisibilityEnum.All,
text: `All ${isPost ? "Posts" : "Pages"}`,
},
{
value: PageVisibilityEnum.Published,
text: "Published",
},
{
value: PageVisibilityEnum.Draft,
text: "Draft",
},
{
value: PageVisibilityEnum.Scheduled,
text: "Scheduled",
},
{
value: PageVisibilityEnum.Crossbell,
text: "Others on Crossbell",
},
].map((item) => ({
text: item.text,
onClick: () => {
const newQuery: Record<string, any> = {
...router.query,
visibility: item.value,
}
if (item.value === PageVisibilityEnum.All) {
delete newQuery["visibility"]
}
const search = new URLSearchParams(newQuery).toString()
router.push({
search,
})
},
active: item.value === visibility,
}))
const getPageEditLink = (page: { id: string }) => {
return `/dashboard/${subdomain}/editor?id=${page.id}&type=${
isPost ? "post" : "page"
}`
}
const queryClient = useQueryClient()
const importFile = () => {
const input = document.createElement("input")
input.type = "file"
input.accept = ".md"
input.addEventListener("change", async (e: any) => {
const file = (await readFiles(e.target?.files))?.[0]
if (file) {
const id = nanoid()
const key = `draft-${subdomain}-local-${id}`
setStorage(key, {
date: +new Date(),
values: {
content: file.content,
published: false,
publishedAt: file.date_published,
slug: file.slug,
tags: file.tags?.join?.(", "),
title: file.title,
},
isPost: isPost,
})
queryClient.invalidateQueries(["getPagesBySite", subdomain])
router.push(
`/dashboard/${subdomain}/editor?id=local-${id}&type=${
isPost ? "post" : "page"
}`,
)
}
})
input.click()
}
const title = isPost ? "Posts" : "Pages"
const description = isPost ? (
<>
<p>
<Trans i18nKey="posts description" ns="dashboard">
Posts are entries listed in reverse chronological order on your site.
Think of them as articles or updates that you share to offer up new
content to your readers.{" "}
<UniLink className="underline" href={t("link post-vs-page") || ""}>
Post vs. Page
</UniLink>
</Trans>
</p>
</>
) : (
<>
<p>
<Trans i18nKey="pages description" ns="dashboard">
Pages are static and are not affected by date. Think of them as more
permanent fixtures of your site — an About page, and a Contact page
are great examples of this.{" "}
<UniLink
className="underline"
href="https://wordpress.com/support/post-vs-page/"
>
Post vs. Page
</UniLink>
</Trans>
</p>
<p>
<Trans i18nKey="pages add" ns="dashboard">
After you create a page, you can{" "}
<UniLink
className="underline"
href={`/dashboard/${subdomain}/settings/navigation`}
>
add it to your site's navigation menu
</UniLink>{" "}
so your visitors can find it.
</Trans>
</p>
</>
)
let currentLength = 0
return (
<DashboardMain className="max-w-screen-lg">
<header className="mb-8">
<div className="flex justify-between items-center mb-4">
<h2 className="text-2xl font-bold">{t(title)}</h2>
<div className="flex justify-center items-center space-x-4">
<Button
className={cn(`space-x-2 inline-flex`)}
onClick={() =>
router.push(
`/dashboard/${subdomain}/editor?type=${
isPost ? "post" : "page"
}`,
)
}
>
<span className="i-mingcute:add-line inline-block"></span>
<span>{t(`New ${isPost ? "Post" : "Page"}`)}</span>
</Button>
<span className="hidden sm:inline-flex">
<Tooltip
label={t("Import markdown file with front matter supported")}
placement="bottom"
>
<Button className={cn(`space-x-2`)} onClick={importFile}>
<span className="i-mingcute:file-import-line inline-block"></span>
<span>{t("Import")}</span>
</Button>
</Tooltip>
</span>
</div>
</div>
<div className="text-sm text-zinc-500 leading-relaxed">
{description}
</div>
</header>
<Tabs items={tabItems} />
<div className="-mt-3">
{!pages.data?.pages?.[0].total && (
<EmptyState resource={isPost ? "posts" : "pages"} />
)}
{pages.data?.pages.map((page) =>
page.list?.map((page) => {
currentLength++
return (
<Link
key={page.id}
href={getPageEditLink(page)}
className="group relative hover:bg-zinc-100 rounded-lg py-3 px-3 transition-colors -mx-3 flex justify-between"
>
<div className="min-w-0">
{page.title ? (
<div className="flex items-center">
<span>{page.title}</span>
</div>
) : (
<div className="text-zinc-500 text-xs mt-1 truncate">
<span>{page.summary?.content}</span>
</div>
)}
<div className="text-zinc-400 text-xs mt-1">
<span className="capitalize">
{t(getPageVisibility(page))}
</span>
<span className="mx-2">·</span>
<span>
{getPageVisibility(page) === PageVisibilityEnum.Draft
? date.formatDate(page.date_updated)
: date.formatDate(page.date_published)}
</span>
</div>
</div>
<div className="w-10 flex-shrink-0">
<Menu>
{({ open, close }) => (
<>
<Menu.Button as={Fragment}>
<button
className={cn(
`text-gray-400 relative z-10 w-8 h-8 rounded inline-flex group-hover:visible justify-center items-center`,
open ? `bg-gray-200` : `hover:bg-gray-200`,
)}
onClick={(e) => {
e.stopPropagation()
}}
>
<i className="i-mingcute:more-1-line text-2xl" />
</button>
</Menu.Button>
<PagesManagerMenu
isPost={isPost}
page={page}
onClick={close}
/>
</>
)}
</Menu>
</div>
</Link>
)
}),
)}
{pages.hasNextPage && (
<Button
className="w-full bg-zinc-50 rounded-lg mt-4"
variant="text"
onClick={pages.fetchNextPage as () => void}
isLoading={pages.isFetchingNextPage}
>
{t("load more", {
name: t(
isPost
? "post"
: "page" +
((pages.data?.pages?.[0].total || 0) - currentLength > 1
? "s"
: ""),
),
count: (pages.data?.pages?.[0].total || 0) - currentLength,
ns: "site",
})}
</Button>
)}
</div>
</DashboardMain>
)
}