Files
outline/app/menus/ShareMenu.tsx
Tom Moor 15b1069bcc chore: Move to Typescript (#2783)
This PR moves the entire project to Typescript. Due to the ~1000 ignores this will lead to a messy codebase for a while, but the churn is worth it – all of those ignore comments are places that were never type-safe previously.

closes #1282
2021-11-29 06:40:55 -08:00

84 lines
2.2 KiB
TypeScript

import { observer } from "mobx-react";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { useMenuState } from "reakit/Menu";
import Share from "~/models/Share";
import ContextMenu from "~/components/ContextMenu";
import MenuItem from "~/components/ContextMenu/MenuItem";
import OverflowMenuButton from "~/components/ContextMenu/OverflowMenuButton";
import CopyToClipboard from "~/components/CopyToClipboard";
import useStores from "~/hooks/useStores";
import useToasts from "~/hooks/useToasts";
type Props = {
share: Share;
};
function ShareMenu({ share }: Props) {
const menu = useMenuState({
modal: true,
});
const { shares, policies } = useStores();
const { showToast } = useToasts();
const { t } = useTranslation();
const history = useHistory();
const can = policies.abilities(share.id);
const handleGoToDocument = React.useCallback(
(ev: React.SyntheticEvent) => {
ev.preventDefault();
history.push(share.documentUrl);
},
[history, share]
);
const handleRevoke = React.useCallback(
async (ev: React.SyntheticEvent) => {
ev.preventDefault();
try {
await shares.revoke(share);
showToast(t("Share link revoked"), {
type: "info",
});
} catch (err) {
showToast(err.message, {
type: "error",
});
}
},
[t, shares, share, showToast]
);
const handleCopy = React.useCallback(() => {
showToast(t("Share link copied"), {
type: "info",
});
}, [t, showToast]);
return (
<>
<OverflowMenuButton aria-label={t("Show menu")} {...menu} />
<ContextMenu {...menu} aria-label={t("Share options")}>
<CopyToClipboard text={share.url} onCopy={handleCopy}>
<MenuItem {...menu}>{t("Copy link")}</MenuItem>
</CopyToClipboard>
<MenuItem {...menu} onClick={handleGoToDocument}>
{t("Go to document")}
</MenuItem>
{can.revoke && (
<>
<hr />
<MenuItem {...menu} onClick={handleRevoke}>
{t("Revoke link")}
</MenuItem>
</>
)}
</ContextMenu>
</>
);
}
export default observer(ShareMenu);