revite/src/context/intermediate/modals/Input.tsx

177 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-07-05 11:23:23 +01:00
import { useHistory } from "react-router";
2021-06-19 18:46:05 +01:00
import { ulid } from "ulid";
2021-07-05 11:23:23 +01:00
2021-06-19 18:46:05 +01:00
import { Text } from "preact-i18n";
2021-07-05 11:23:23 +01:00
import { useContext, useState } from "preact/hooks";
import InputBox from "../../../components/ui/InputBox";
2021-06-19 18:46:05 +01:00
import Modal from "../../../components/ui/Modal";
2021-07-05 11:23:23 +01:00
import Overline from "../../../components/ui/Overline";
2021-06-19 18:46:05 +01:00
import { Children } from "../../../types/Preact";
import { AppContext } from "../../revoltjs/RevoltClient";
2021-07-05 11:23:23 +01:00
import { takeError } from "../../revoltjs/util";
2021-06-19 18:46:05 +01:00
interface Props {
2021-07-05 11:23:23 +01:00
onClose: () => void;
question: Children;
field?: Children;
defaultValue?: string;
callback: (value: string) => Promise<void>;
2021-06-19 18:46:05 +01:00
}
export function InputModal({
2021-07-05 11:23:23 +01:00
onClose,
question,
field,
defaultValue,
callback,
2021-06-19 18:46:05 +01:00
}: Props) {
2021-07-05 11:23:23 +01:00
const [processing, setProcessing] = useState(false);
const [value, setValue] = useState(defaultValue ?? "");
const [error, setError] = useState<undefined | string>(undefined);
2021-06-19 18:46:05 +01:00
2021-07-05 11:23:23 +01:00
return (
<Modal
visible={true}
title={question}
disabled={processing}
actions={[
{
confirmation: true,
text: <Text id="app.special.modals.actions.ok" />,
onClick: () => {
setProcessing(true);
callback(value)
.then(onClose)
.catch((err) => {
setError(takeError(err));
setProcessing(false);
});
},
},
{
text: <Text id="app.special.modals.actions.cancel" />,
onClick: onClose,
},
]}
onClose={onClose}>
<form>
{field ? (
<Overline error={error} block>
{field}
</Overline>
) : (
error && <Overline error={error} type="error" block />
)}
<InputBox
value={value}
onChange={(e) => setValue(e.currentTarget.value)}
/>
</form>
</Modal>
);
2021-06-19 18:46:05 +01:00
}
type SpecialProps = { onClose: () => void } & (
2021-07-05 11:23:23 +01:00
| {
type:
| "create_group"
| "create_server"
| "set_custom_status"
| "add_friend";
}
| { type: "create_role"; server: string; callback: (id: string) => void }
);
2021-06-19 18:46:05 +01:00
export function SpecialInputModal(props: SpecialProps) {
2021-07-05 11:23:23 +01:00
const history = useHistory();
const client = useContext(AppContext);
2021-06-19 18:46:05 +01:00
2021-07-05 11:23:23 +01:00
const { onClose } = props;
switch (props.type) {
case "create_group": {
return (
<InputModal
onClose={onClose}
question={<Text id="app.main.groups.create" />}
field={<Text id="app.main.groups.name" />}
callback={async (name) => {
const group = await client.channels.createGroup({
name,
nonce: ulid(),
users: [],
});
2021-06-19 18:46:05 +01:00
2021-07-05 11:23:23 +01:00
history.push(`/channel/${group._id}`);
}}
/>
);
}
case "create_server": {
return (
<InputModal
onClose={onClose}
question={<Text id="app.main.servers.create" />}
field={<Text id="app.main.servers.name" />}
callback={async (name) => {
const server = await client.servers.createServer({
name,
nonce: ulid(),
});
2021-06-19 18:46:05 +01:00
2021-07-05 11:23:23 +01:00
history.push(`/server/${server._id}`);
}}
/>
);
}
case "create_role": {
return (
<InputModal
onClose={onClose}
question={
<Text id="app.settings.permissions.create_role" />
}
field={<Text id="app.settings.permissions.role_name" />}
callback={async (name) => {
const role = await client.servers.createRole(
props.server,
name,
);
props.callback(role.id);
}}
/>
);
}
case "set_custom_status": {
return (
<InputModal
onClose={onClose}
question={<Text id="app.context_menu.set_custom_status" />}
field={<Text id="app.context_menu.custom_status" />}
defaultValue={client.user?.status?.text}
callback={(text) =>
client.users.editUser({
status: {
...client.user?.status,
text: text.trim().length > 0 ? text : undefined,
},
})
}
/>
);
}
case "add_friend": {
return (
<InputModal
onClose={onClose}
question={"Add Friend"}
callback={(username) => client.users.addFriend(username)}
/>
);
}
default:
return null;
}
2021-06-19 18:46:05 +01:00
}