import React from "react";
import { useUserContext } from "./UserContext";
import {
Avatar,
Box,
Button,
Checkbox,
Fieldset,
Flex,
Modal,
Paper,
TextInput,
Title,
} from "@mantine/core";
import { useDisclosure } from "@mantine/hooks";
import { Link, useLoaderData } from "react-router";
import { useForm } from "@mantine/form";
import { notifications } from "@mantine/notifications";
import { fetchHeaders } from "./fetch";
const RE_MASTODON_ACCOUNTS = /^(@(\w+)(@([\w\.]+))? )*(@\w+)(@[\w\.]+)?$/;
export const AuthorizeMastodonModal: React.FC<{
opened: boolean;
onClose: () => void;
}> = ({ opened, onClose }) => {
const [serverName, setServerName] = React.useState("rubber.social");
const [loading, setLoading] = React.useState(false);
const handleClick = React.useCallback(() => {
setLoading(true);
fetch(`/api/mastodon_oauth?server=${encodeURI(serverName)}`)
.then((response) => response.json())
.then((json) => {
if (json["url"]) {
window.location.href = json["url"];
}
});
}, []);
return (
setServerName(event.currentTarget.value)}
/>
);
};
type MastodonPreferencesForm = {
attn_list?: string;
post_public?: boolean;
};
export const profileLoader = async () =>
fetch(`/api/me`).then((response) => response.json());
export const Profile: React.FC = () => {
const { username, telegram_photo_url, mastodon_server, mastodon_username } =
useUserContext();
const { mastodon_attn_list, mastodon_post_public } = useLoaderData<{
mastodon_attn_list?: string;
mastodon_post_public?: boolean;
}>();
const [opened, { open, close }] = useDisclosure(false);
const mastodon_account = React.useMemo(
() => `@${mastodon_username}@${mastodon_server}`,
[mastodon_server, mastodon_username],
);
const [loading, setLoading] = React.useState(false);
const form = useForm({
mode: "uncontrolled",
initialValues: {
attn_list: mastodon_attn_list,
post_public: mastodon_post_public,
},
validate: {
attn_list: (value: string) =>
!value || RE_MASTODON_ACCOUNTS.test(value)
? null
: "Please enter a valid list of accounts",
},
});
const handleSubmit = React.useCallback(
form.onSubmit((values) => {
setLoading(true);
fetch(`/api/profile`, {
method: "POST",
headers: fetchHeaders(),
body: JSON.stringify(values),
})
.then((response) => {
if (response.ok) {
notifications.show({
title: "Success",
message: "Your preferences have been saved",
color: "green",
});
} else {
notifications.show({
title: "Error",
message: "There was a problem saving your preferences",
color: "red",
});
}
})
.finally(() => {
setLoading(false);
});
}),
[],
);
return (
<>
{username}
Return to dashboard
Mastodon
>
);
};