2018-09-30 04:24:07 +00:00
|
|
|
// @flow
|
2021-01-14 06:00:25 +00:00
|
|
|
import { observer } from "mobx-react";
|
2020-06-20 20:59:15 +00:00
|
|
|
import * as React from "react";
|
2021-01-14 06:00:25 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
import { useMenuState } from "reakit/Menu";
|
2020-08-09 05:53:59 +00:00
|
|
|
import Document from "models/Document";
|
|
|
|
import Revision from "models/Revision";
|
2021-01-14 06:00:25 +00:00
|
|
|
import ContextMenu from "components/ContextMenu";
|
|
|
|
import MenuItem from "components/ContextMenu/MenuItem";
|
|
|
|
import OverflowMenuButton from "components/ContextMenu/OverflowMenuButton";
|
|
|
|
import Separator from "components/ContextMenu/Separator";
|
2020-06-20 20:59:15 +00:00
|
|
|
import CopyToClipboard from "components/CopyToClipboard";
|
2021-07-20 09:06:10 +00:00
|
|
|
import useToasts from "hooks/useToasts";
|
2020-06-20 20:59:15 +00:00
|
|
|
import { documentHistoryUrl } from "utils/routeHelpers";
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2021-01-14 06:00:25 +00:00
|
|
|
type Props = {|
|
2018-09-30 04:24:07 +00:00
|
|
|
document: Document,
|
|
|
|
revision: Revision,
|
2021-01-14 06:00:25 +00:00
|
|
|
iconColor?: string,
|
2018-09-30 04:24:07 +00:00
|
|
|
className?: string,
|
2021-01-14 06:00:25 +00:00
|
|
|
|};
|
|
|
|
|
|
|
|
function RevisionMenu({ document, revision, className, iconColor }: Props) {
|
2021-07-20 09:06:10 +00:00
|
|
|
const { showToast } = useToasts();
|
2021-01-14 06:00:25 +00:00
|
|
|
const menu = useMenuState({ modal: true });
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const history = useHistory();
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2021-01-14 06:00:25 +00:00
|
|
|
const handleRestore = React.useCallback(
|
|
|
|
async (ev: SyntheticEvent<>) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
await document.restore({ revisionId: revision.id });
|
2021-07-20 09:06:10 +00:00
|
|
|
showToast(t("Document restored"), { type: "success" });
|
2021-01-14 06:00:25 +00:00
|
|
|
history.push(document.url);
|
|
|
|
},
|
2021-07-20 09:06:10 +00:00
|
|
|
[history, showToast, t, document, revision]
|
2021-01-14 06:00:25 +00:00
|
|
|
);
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2021-01-14 06:00:25 +00:00
|
|
|
const handleCopy = React.useCallback(() => {
|
2021-07-20 09:06:10 +00:00
|
|
|
showToast(t("Link copied"), { type: "info" });
|
|
|
|
}, [showToast, t]);
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2021-01-14 06:00:25 +00:00
|
|
|
const url = `${window.location.origin}${documentHistoryUrl(
|
|
|
|
document,
|
|
|
|
revision.id
|
|
|
|
)}`;
|
2018-09-30 04:24:07 +00:00
|
|
|
|
2021-01-14 06:00:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<OverflowMenuButton
|
2020-05-22 05:22:04 +00:00
|
|
|
className={className}
|
2021-01-14 06:00:25 +00:00
|
|
|
iconColor={iconColor}
|
2021-01-23 03:31:30 +00:00
|
|
|
aria-label={t("Show menu")}
|
2021-01-14 06:00:25 +00:00
|
|
|
{...menu}
|
|
|
|
/>
|
|
|
|
<ContextMenu {...menu} aria-label={t("Revision options")}>
|
|
|
|
<MenuItem {...menu} onClick={handleRestore}>
|
2020-11-30 04:04:58 +00:00
|
|
|
{t("Restore version")}
|
2021-01-14 06:00:25 +00:00
|
|
|
</MenuItem>
|
|
|
|
<Separator />
|
|
|
|
<CopyToClipboard text={url} onCopy={handleCopy}>
|
|
|
|
<MenuItem {...menu}>{t("Copy link")}</MenuItem>
|
2018-09-30 04:24:07 +00:00
|
|
|
</CopyToClipboard>
|
2021-01-14 06:00:25 +00:00
|
|
|
</ContextMenu>
|
|
|
|
</>
|
|
|
|
);
|
2018-09-30 04:24:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:00:25 +00:00
|
|
|
export default observer(RevisionMenu);
|