/
$id.tsx
335 lines (295 loc) · 10.2 KB
/
$id.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { FormEvent, useRef, useState } from "react";
import {
type RouteHandle,
type LoaderFunction,
type ActionFunction,
json,
useParams,
Form,
useActionData,
useLoaderData,
redirect,
unstable_parseMultipartFormData,
} from "remix";
import { useLocation } from "react-router-dom";
import invariant from "tiny-invariant";
import slugify from "@sindresorhus/slugify";
import { type UploadHandler } from "@remix-run/node/formData";
import { type PutObjectCommandInput } from "@aws-sdk/client-s3";
import cuid from "cuid";
import { requireUser, User } from "~/utils/session.server";
import { useFirstRender } from "~/hooks/use-first-render";
import { Lang } from "/types";
import { defaultLang } from "/config";
import { internalServerError } from "~/utils/error-responses";
export const handle: RouteHandle = {
hydrate: true,
};
type LoaderData = {
user: User;
csrf: string;
};
type ActionData = Record<string, string>;
export const loader: LoaderFunction = async ({ request, params }) => {
const user = await requireUser(request, params);
const lang = (params.lang ?? defaultLang) as Lang;
const { DTKLT_API } = process.env;
if (!DTKLT_API) {
throw internalServerError(lang, "Missing environment variable `DTKLT_API`");
}
const csrf = await fetch(`${process.env.DTKLT_API}/csrf`).catch((e) => {
if (e.code !== "ECONNREFUSED") {
console.error(e);
}
});
const csrfToken = csrf?.headers.get("X-CSRF-Token");
return json(
{ user, csrf: csrfToken },
{
headers: {
"Set-Cookie": csrf?.headers.get("Set-Cookie") ?? "",
},
},
);
};
const storageKey = (id: string) => `content_${id}`;
export default function Admin() {
const { id } = useParams<"id">();
invariant(id, "Missing $id");
const { user, csrf } = useLoaderData<LoaderData>();
const actionData = useActionData<ActionData>();
const location = useLocation();
const [slug, setSlug] = useState("");
const isFirstRender = useFirstRender();
const form = useRef<HTMLFormElement>(null);
// useEffect(() => {
// // localStorage.setItem(storageKey(id), JSON.stringify(state));
// // setTimeout(() => {
// // const stored =
// // JSON.parse(localStorage.getItem(storageKey(id)) ?? JSON.stringify(emptyState)) || initialState;
// // dispatch(resetAction(stored));
// // }, 10);
// }, [id, state, initialState]);
// useEffect(() => {
// const newSlug = slugify(state.title);
// setSlug(newSlug);
// if (!isFirstRender) {
// const newUrl = location.pathname.replace(id, newSlug);
// window.history.replaceState({}, "", newUrl);
// }
// }, [state.title]);
const onFormChange = (
// note: not sure tho 🤔
e: FormEvent<HTMLFormElement> & { target: HTMLInputElement & { form: HTMLFormElement } },
) => {
if (!form.current) return;
const data = new FormData(form.current);
const dataObject = Object.fromEntries(data.entries());
localStorage.setItem(storageKey(id), JSON.stringify(dataObject));
const slugInput = form.current.elements.namedItem("slug##S") as HTMLInputElement;
if (slugInput) slugInput.value = slugify(data.get("title##S")?.toString() ?? "");
};
return (
<>
<div className="max-w-prose">
<div>
<p>{actionData?.error}</p>
</div>
<Form method="post" ref={form} onChange={onFormChange} encType="multipart/form-data">
<fieldset className="space-y-8">
<div>
<label>
<div>Title</div>
<div>
<input
name="title##S"
type="text"
className="w-full bg-zinc-300 text-black placeholder:text-zinc-500 dark:bg-zinc-700 dark:text-zinc-200 dark:placeholder:text-zinc-400 px-4 py-2 mt-1 rounded-md"
/>
</div>
</label>
</div>
<div>
<label>
<div>Content</div>
<div>
<textarea
name="content##S"
className="w-full bg-zinc-300 text-black placeholder:text-zinc-500 dark:bg-zinc-700 dark:text-zinc-200 dark:placeholder:text-zinc-400 px-4 py-2 mt-1 rounded-md"
/>
</div>
</label>
</div>
<div>
<label>
<div>Description</div>
<div>
<input
name="description##S"
type="text"
className="w-full bg-zinc-300 text-black placeholder:text-zinc-500 dark:bg-zinc-700 dark:text-zinc-200 dark:placeholder:text-zinc-400 px-4 py-2 mt-1 rounded-md"
/>
</div>
</label>
</div>
<div>
<label>
<div>Slug (read-only for now)</div>
<div>
<input
name="slug##S"
type="text"
value={slug}
readOnly
className="w-full bg-zinc-300 text-black placeholder:text-zinc-500 dark:bg-zinc-700 dark:text-zinc-200 dark:placeholder:text-zinc-400 px-4 py-2 mt-1 rounded-md"
/>
</div>
</label>
</div>
<input type="file" name="files" multiple />
<ul>
{actionData?.files?.length > 0 &&
actionData?.files?.map((f) => <li>{f.filename}</li>)}
</ul>
<input type="hidden" name="id" value={id} />
<input type="hidden" name="csrf" value={csrf} />
<div className="flex justify-between">
<div className="flex shrink grow-0 mt-auto">
<button type="reset" className="w-full bg-none dark:text-zinc-500 px-4 py-2 mt-1">
Reset
</button>
</div>
<div>
<label>
{/* <div>Status</div> */}
<div>
<select
name="status##S"
defaultValue="draft"
className="w-full bg-zinc-300 text-black placeholder:text-zinc-500 dark:bg-zinc-700 dark:text-zinc-200 dark:placeholder:text-zinc-400 px-4 py-2 mt-1 rounded-md"
>
<option value="draft">Draft</option>
<option value="published">Published</option>
</select>
</div>
</label>
</div>
<div className="flex shrink grow-0 mt-auto">
<button
type="submit"
className="w-full bg-zinc-300 text-black dark:bg-zinc-700 dark:text-zinc-200 px-4 py-2 mt-1 rounded-md"
>
Save
</button>
</div>
</div>
</fieldset>
</Form>
</div>
{/* <div className="bg-zinc-600">
<PlateEditor />
</div>
<RteEditor value={value} onChange={onChange} /> */}
</>
);
}
const uploadHandler: UploadHandler = async ({ name, filename, mimetype, encoding, stream }) => {
if (name !== "files") {
stream.resume();
return;
}
const key = cuid();
const params: PutObjectCommandInput = {
Bucket: process.env.S3_BUCKET ?? "", // "assets-canrau-com",
Key: key,
Body: stream,
ContentType: mimetype,
ContentEncoding: encoding,
Metadata: {
filename: filename,
},
};
try {
const { S3Client, Upload, getApplyMd5BodyChecksumPlugin } = await import("~/utils.server");
const client = new S3Client({
endpoint: process.env.S3_ENDPOINT ?? "",
region: process.env.S3_REGION ?? "",
// bucketEndpoint: true, might work when setting up cloudflare? oh nevermind as backblaze won't accept dots
credentials: {
accessKeyId: process.env.S3_ACCESS_KEY_ID ?? "",
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY ?? "",
},
});
// via https://github.com/aws/aws-sdk-js-v3/issues/2673#issuecomment-991455219
client.middlewareStack.use(getApplyMd5BodyChecksumPlugin(client.config));
const upload = new Upload({
client,
params,
// tags: [{ Key: "source", Value: "cms_upload" }], // optional tags
// queueSize: 4, // optional concurrency configuration
// partSize: "5MB", // optional size of each part
// leavePartsOnError: false, // optional manually handle dropped parts
});
upload.on("httpUploadProgress", (progress) => {
console.log({ progress });
});
await upload.done();
} catch (e) {
console.log(e);
}
return JSON.stringify({ filename, key });
};
type UploadFile = {
key: string;
filename: string;
};
export const action: ActionFunction = async ({ request, params }) => {
const lang = (params.lang ?? "en") as Lang;
const user = await requireUser(request, params);
const form = await unstable_parseMultipartFormData(request, uploadHandler);
const fileJsons = form.getAll("files");
const files = fileJsons.map(
(str) => JSON.parse((str as unknown as string) ?? "{}") as UploadFile,
);
console.log({ files });
const id = form.get("id");
const csrf = form.get("csrf");
if (!id) {
console.log({ error: "Missing ID" });
return { error: "Missing ID" };
}
if (!csrf) {
console.log({ error: "Missing CSRF token" });
return { error: "Missing CSRF token" };
}
// const title = form.get("title");
// const content = form.get("content");
form.delete("id");
form.delete("csrf");
return { files };
const fields = Object.fromEntries(form.entries());
const res = await fetch(`${process.env.DTKLT_API}/put`, {
method: "PUT",
headers: {
"X-CSRF-Token": csrf?.toString() ?? "",
cookie: request.headers.get("cookie") ?? "",
Authorization: `Bearer ${user.token}`,
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify({
id,
bucket: "article",
fields: {
...fields,
"lang##S": lang,
"created##N": Date.now().toString(),
"author##S": user.id,
},
}),
});
// const result: Record<string, string> = {};
if (!res.ok) {
return { error: "Error saving document 😢" };
}
return redirect(`${lang}/admin`);
};