2022-05-06 10:25:10 +02:00
|
|
|
import {
|
|
|
|
Accordion,
|
|
|
|
Button,
|
2022-10-16 00:08:37 +02:00
|
|
|
Checkbox,
|
2022-10-16 20:21:35 +02:00
|
|
|
Col,
|
2022-05-06 10:25:10 +02:00
|
|
|
Grid,
|
|
|
|
NumberInput,
|
|
|
|
PasswordInput,
|
|
|
|
Select,
|
2022-10-10 22:14:23 +02:00
|
|
|
Stack,
|
2022-05-06 10:25:10 +02:00
|
|
|
Text,
|
|
|
|
TextInput,
|
|
|
|
} from "@mantine/core";
|
|
|
|
import { useForm, yupResolver } from "@mantine/form";
|
|
|
|
import { useModals } from "@mantine/modals";
|
2022-10-16 20:21:35 +02:00
|
|
|
import moment from "moment";
|
2022-05-06 10:25:10 +02:00
|
|
|
import * as yup from "yup";
|
|
|
|
import shareService from "../../services/share.service";
|
2022-10-09 22:30:32 +02:00
|
|
|
import { ShareSecurity } from "../../types/share.type";
|
2022-10-15 00:14:46 +02:00
|
|
|
|
2022-10-16 00:08:37 +02:00
|
|
|
const PreviewExpiration = ({ form }: { form: any }) => {
|
|
|
|
const value = form.values.never_expires
|
|
|
|
? "never"
|
|
|
|
: form.values.expiration_num + form.values.expiration_unit;
|
|
|
|
if (value === "never") return "This share will never expire.";
|
2022-10-15 00:14:46 +02:00
|
|
|
|
2022-10-16 00:08:37 +02:00
|
|
|
const expirationDate = moment()
|
|
|
|
.add(
|
|
|
|
value.split("-")[0],
|
|
|
|
value.split("-")[1] as moment.unitOfTime.DurationConstructor
|
|
|
|
)
|
|
|
|
.toDate();
|
2022-10-15 00:14:46 +02:00
|
|
|
|
2022-10-16 20:21:35 +02:00
|
|
|
return `This share will expire on ${moment(expirationDate).format("LLL")}`;
|
2022-10-16 00:08:37 +02:00
|
|
|
};
|
2022-05-06 10:25:10 +02:00
|
|
|
|
|
|
|
const CreateUploadModalBody = ({
|
|
|
|
uploadCallback,
|
|
|
|
}: {
|
|
|
|
uploadCallback: (
|
|
|
|
id: string,
|
2022-10-09 22:30:32 +02:00
|
|
|
expiration: string,
|
|
|
|
security: ShareSecurity
|
2022-05-06 10:25:10 +02:00
|
|
|
) => void;
|
|
|
|
}) => {
|
|
|
|
const modals = useModals();
|
|
|
|
const validationSchema = yup.object().shape({
|
2022-10-09 22:30:32 +02:00
|
|
|
link: yup
|
|
|
|
.string()
|
|
|
|
.required()
|
|
|
|
.min(3)
|
2022-10-13 23:23:33 +02:00
|
|
|
.max(50)
|
2022-10-09 22:30:32 +02:00
|
|
|
.matches(new RegExp("^[a-zA-Z0-9_-]*$"), {
|
|
|
|
message: "Can only contain letters, numbers, underscores and hyphens",
|
|
|
|
}),
|
|
|
|
password: yup.string().min(3).max(30),
|
|
|
|
maxViews: yup.number().min(1),
|
2022-05-06 10:25:10 +02:00
|
|
|
});
|
|
|
|
const form = useForm({
|
|
|
|
initialValues: {
|
|
|
|
link: "",
|
2022-10-09 22:30:32 +02:00
|
|
|
|
2022-05-06 10:25:10 +02:00
|
|
|
password: undefined,
|
2022-10-09 22:30:32 +02:00
|
|
|
maxViews: undefined,
|
2022-10-16 00:08:37 +02:00
|
|
|
expiration_num: 1,
|
|
|
|
expiration_unit: "-days",
|
|
|
|
never_expires: false,
|
2022-05-06 10:25:10 +02:00
|
|
|
},
|
2022-10-10 22:14:23 +02:00
|
|
|
validate: yupResolver(validationSchema),
|
2022-05-06 10:25:10 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={form.onSubmit(async (values) => {
|
2022-10-09 22:30:32 +02:00
|
|
|
if (!(await shareService.isShareIdAvailable(values.link))) {
|
|
|
|
form.setFieldError("link", "This link is already in use");
|
2022-05-06 10:25:10 +02:00
|
|
|
} else {
|
2022-10-16 00:08:37 +02:00
|
|
|
const expiration = form.values.never_expires
|
|
|
|
? "never"
|
|
|
|
: form.values.expiration_num + form.values.expiration_unit;
|
|
|
|
uploadCallback(values.link, expiration, {
|
2022-10-09 22:30:32 +02:00
|
|
|
password: values.password,
|
|
|
|
maxViews: values.maxViews,
|
|
|
|
});
|
2022-05-06 10:25:10 +02:00
|
|
|
modals.closeAll();
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
>
|
2022-10-10 22:14:23 +02:00
|
|
|
<Stack align="stretch">
|
2022-05-06 10:25:10 +02:00
|
|
|
<Grid align={form.errors.link ? "center" : "flex-end"}>
|
|
|
|
<Col xs={9}>
|
|
|
|
<TextInput
|
|
|
|
variant="filled"
|
|
|
|
label="Link"
|
|
|
|
placeholder="myAwesomeShare"
|
|
|
|
{...form.getInputProps("link")}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
<Col xs={3}>
|
|
|
|
<Button
|
|
|
|
variant="outline"
|
|
|
|
onClick={() =>
|
|
|
|
form.setFieldValue(
|
|
|
|
"link",
|
|
|
|
Buffer.from(Math.random().toString(), "utf8")
|
|
|
|
.toString("base64")
|
|
|
|
.substr(10, 7)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
>
|
|
|
|
Generate
|
|
|
|
</Button>
|
|
|
|
</Col>
|
|
|
|
</Grid>
|
2022-10-12 22:59:04 +02:00
|
|
|
|
2022-05-06 10:25:10 +02:00
|
|
|
<Text
|
2022-10-16 00:08:37 +02:00
|
|
|
italic
|
2022-05-06 10:25:10 +02:00
|
|
|
size="xs"
|
|
|
|
sx={(theme) => ({
|
|
|
|
color: theme.colors.gray[6],
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{window.location.origin}/share/
|
|
|
|
{form.values.link == "" ? "myAwesomeShare" : form.values.link}
|
|
|
|
</Text>
|
2022-10-16 00:08:37 +02:00
|
|
|
<Grid align={form.errors.link ? "center" : "flex-end"}>
|
|
|
|
<Col xs={6}>
|
|
|
|
<NumberInput
|
|
|
|
min={1}
|
|
|
|
max={99999}
|
|
|
|
precision={0}
|
|
|
|
variant="filled"
|
|
|
|
label="Expiration"
|
|
|
|
placeholder="n"
|
|
|
|
disabled={form.values.never_expires}
|
|
|
|
{...form.getInputProps("expiration_num")}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
<Col xs={6}>
|
|
|
|
<Select
|
|
|
|
disabled={form.values.never_expires}
|
|
|
|
{...form.getInputProps("expiration_unit")}
|
|
|
|
data={[
|
|
|
|
// Set the label to singular if the number is 1, else plural
|
|
|
|
{
|
|
|
|
value: "-minutes",
|
|
|
|
label:
|
|
|
|
"Minute" + (form.values.expiration_num == 1 ? "" : "s"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "-hours",
|
|
|
|
label: "Hour" + (form.values.expiration_num == 1 ? "" : "s"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "-days",
|
|
|
|
label: "Day" + (form.values.expiration_num == 1 ? "" : "s"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "-weeks",
|
|
|
|
label: "Week" + (form.values.expiration_num == 1 ? "" : "s"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "-months",
|
|
|
|
label: "Month" + (form.values.expiration_num == 1 ? "" : "s"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: "-years",
|
|
|
|
label: "Year" + (form.values.expiration_num == 1 ? "" : "s"),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
</Grid>
|
|
|
|
<Checkbox
|
|
|
|
label="Never Expires"
|
|
|
|
{...form.getInputProps("never_expires")}
|
2022-05-06 10:25:10 +02:00
|
|
|
/>
|
2022-10-15 00:14:46 +02:00
|
|
|
|
2022-10-16 00:08:37 +02:00
|
|
|
{/* Preview expiration date text */}
|
|
|
|
<Text
|
|
|
|
italic
|
|
|
|
size="xs"
|
|
|
|
sx={(theme) => ({
|
|
|
|
color: theme.colors.gray[6],
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{PreviewExpiration({ form })}
|
|
|
|
</Text>
|
2022-10-15 00:14:46 +02:00
|
|
|
|
2022-05-06 10:25:10 +02:00
|
|
|
<Accordion>
|
2022-10-11 08:55:30 +02:00
|
|
|
<Accordion.Item value="security" sx={{ borderBottom: "none" }}>
|
|
|
|
<Accordion.Control>Security options</Accordion.Control>
|
|
|
|
<Accordion.Panel>
|
|
|
|
<Stack align="stretch">
|
|
|
|
<PasswordInput
|
|
|
|
variant="filled"
|
|
|
|
placeholder="No password"
|
|
|
|
label="Password protection"
|
|
|
|
{...form.getInputProps("password")}
|
|
|
|
/>
|
|
|
|
<NumberInput
|
|
|
|
min={1}
|
|
|
|
type="number"
|
|
|
|
variant="filled"
|
|
|
|
placeholder="No limit"
|
|
|
|
label="Maximal views"
|
|
|
|
{...form.getInputProps("maxViews")}
|
|
|
|
/>
|
|
|
|
</Stack>
|
|
|
|
</Accordion.Panel>
|
2022-05-06 10:25:10 +02:00
|
|
|
</Accordion.Item>
|
|
|
|
</Accordion>
|
|
|
|
<Button type="submit">Share</Button>
|
2022-10-10 22:14:23 +02:00
|
|
|
</Stack>
|
2022-05-06 10:25:10 +02:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default CreateUploadModalBody;
|