toot-script-condivisione-su.../src/pages/index.astro

105 lines
2.6 KiB
Plaintext
Raw Permalink Normal View History

---
2023-03-18 02:11:18 +01:00
/*!
2023-09-02 16:17:15 +02:00
* This file is part of Share₂Fedi
* https://github.com/kytta/share2fedi
*
* SPDX-FileCopyrightText: © 2023 Nikita Karamov <me@kytta.dev>
* SPDX-License-Identifier: AGPL-3.0-only
2023-03-18 02:11:18 +01:00
*/
2023-03-27 19:22:41 +02:00
import Layout from "@layouts/layout.astro";
2023-09-02 18:00:47 +02:00
import InstanceSelect from "@components/instance-select.astro";
2023-09-02 19:02:17 +02:00
import { getUrlDomain } from "@lib/url";
import type { Detection } from "./api/detect/[domain]";
const searchParameters = new URL(Astro.request.url).searchParams;
2023-09-02 19:02:17 +02:00
let text: string | null = searchParameters.get("text");
let instance: string | null = searchParameters.get("instance");
const errors = { text: "", instance: "" };
if (Astro.request.method === "POST") {
const formData = await Astro.request.formData();
text = formData.get("text") as string | null;
instance = formData.get("instance") as string | null;
if (typeof text !== "string" || text.length === 0) {
errors.text += "Please enter post text. ";
}
let detection: Detection | undefined;
if (typeof instance !== "string" || instance.length === 0) {
errors.instance += "Please enter instance domain. ";
} else {
instance = getUrlDomain(instance);
const detectResponse = await fetch(
new URL(`/api/detect/${instance}`, Astro.url),
);
const detectJson = await detectResponse.json();
if (detectJson.error) {
errors.instance += detectJson.error + " ";
} else {
detection = detectJson;
}
}
const hasErrors = Object.values(errors).some(Boolean);
if (!hasErrors && detection !== undefined) {
const { domain, endpoint, params } = detection;
const publishUrl = new URL(endpoint, `https://${domain}/`);
publishUrl.search = new URLSearchParams([
[params.text, text as string],
]).toString();
// eslint-disable-next-line unicorn/prefer-module
return Astro.redirect(publishUrl.toString(), 303);
}
}
---
2023-09-02 19:22:30 +02:00
<Layout>
2023-09-02 18:00:47 +02:00
<form
id="form"
method="POST"
>
2024-02-12 19:24:08 +01:00
<label>
<span data-translate="postText">Post text</span>
2023-09-02 18:00:47 +02:00
<textarea
name="text"
id="text"
rows="7"
placeholder="Whats on your mind?"
required
2023-09-02 19:02:17 +02:00
aria-invalid={Boolean(errors.text)}
aria-errormessage={errors.text ? "text-error" : undefined}
2023-09-02 18:00:47 +02:00
data-translate="postTextPlaceholder"
data-translate-attribute="placeholder"
2023-09-02 19:02:17 +02:00
>{text}</textarea
2023-09-02 18:00:47 +02:00
>
2023-09-02 19:02:17 +02:00
{
errors.text && (
<p
class="error"
id="text-error"
aria-live="assertive"
>
{errors.text}
</p>
)
}
2023-09-02 18:00:47 +02:00
</label>
2023-09-02 19:02:17 +02:00
<InstanceSelect
{instance}
errors={errors.instance}
/>
2023-09-02 18:00:47 +02:00
<input
type="submit"
value="Publish"
class="mt1r"
2023-09-02 18:00:47 +02:00
data-translate="publish"
data-translate-attribute="value"
/>
</form>
2023-03-27 19:17:35 +02:00
</Layout>