umami/src/app/(main)/settings/websites/[id]/ShareUrl.tsx

92 lines
2.5 KiB
TypeScript
Raw Normal View History

import {
Form,
FormRow,
2023-01-06 06:56:36 +00:00
FormButtons,
Flexbox,
TextField,
2023-01-06 06:56:36 +00:00
SubmitButton,
Button,
Toggle,
} from 'react-basics';
2023-12-06 03:22:14 +00:00
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
2023-01-06 06:56:36 +00:00
import { getRandomChars } from 'next-basics';
import useApi from 'components/hooks/useApi';
import useMessages from 'components/hooks/useMessages';
2023-12-06 03:22:14 +00:00
import SettingsContext from '../../SettingsContext';
2020-08-08 03:36:57 +00:00
2023-01-06 06:56:36 +00:00
const generateId = () => getRandomChars(16);
2023-12-06 03:22:14 +00:00
export function ShareUrl({ websiteId, data, onSave }) {
2023-12-06 09:26:58 +00:00
const ref = useRef(null);
const { shareUrl, websitesUrl } = useContext(SettingsContext);
2023-03-22 21:05:55 +00:00
const { formatMessage, labels, messages } = useMessages();
const { name, shareId } = data;
const [id, setId] = useState(shareId);
2023-01-06 06:56:36 +00:00
const { post, useMutation } = useApi();
2023-12-03 11:07:03 +00:00
const { mutate, error } = useMutation({
2023-12-06 09:26:58 +00:00
mutationFn: (data: any) => post(`${websitesUrl}/${websiteId}`, data),
2023-12-03 11:07:03 +00:00
});
const url = useMemo(
2023-12-06 03:22:14 +00:00
() => `${shareUrl}${process.env.basePath}/share/${id}/${encodeURIComponent(name)}`,
2023-09-29 12:29:22 +00:00
[id, name],
);
2023-12-03 11:07:03 +00:00
const handleSubmit = async (data: any) => {
mutate(data, {
onSuccess: async () => {
onSave(data);
ref.current.reset(data);
},
});
};
const handleGenerate = () => {
const id = generateId();
ref.current.setValue('shareId', id, {
shouldValidate: true,
shouldDirty: true,
});
setId(id);
};
2023-12-03 11:07:03 +00:00
const handleCheck = (checked: boolean) => {
const data = { shareId: checked ? generateId() : null };
mutate(data, {
onSuccess: async () => {
onSave(data);
},
});
setId(data.shareId);
};
useEffect(() => {
if (id && id !== shareId) {
ref.current.setValue('shareId', id);
}
}, [id, shareId]);
2020-08-08 03:36:57 +00:00
return (
2023-01-25 15:42:46 +00:00
<>
<Toggle checked={Boolean(id)} onChecked={handleCheck} style={{ marginBottom: 30 }}>
{formatMessage(labels.enableShareUrl)}
</Toggle>
{id && (
2023-01-25 15:42:46 +00:00
<Form key={websiteId} ref={ref} onSubmit={handleSubmit} error={error} values={data}>
<FormRow>
2023-01-25 15:42:46 +00:00
<p>{formatMessage(messages.shareUrl)}</p>
2023-01-06 06:56:36 +00:00
<Flexbox gap={10}>
<TextField value={url} readOnly allowCopy />
2023-01-25 15:42:46 +00:00
<Button onClick={handleGenerate}>{formatMessage(labels.regenerate)}</Button>
2023-01-06 06:56:36 +00:00
</Flexbox>
</FormRow>
<FormButtons>
2023-01-25 15:42:46 +00:00
<SubmitButton variant="primary">{formatMessage(labels.save)}</SubmitButton>
</FormButtons>
2023-01-25 15:42:46 +00:00
</Form>
)}
2023-01-25 15:42:46 +00:00
</>
2020-08-08 03:36:57 +00:00
);
}
2023-04-21 15:00:42 +00:00
export default ShareUrl;