Add unshare functionality

This commit is contained in:
1ilit 2024-11-23 11:18:26 +04:00
parent ce85bb6680
commit 8eb88cc15b
3 changed files with 29 additions and 10 deletions

View File

@ -336,7 +336,7 @@ export default function Modal({
case MODAL.LANGUAGE: case MODAL.LANGUAGE:
return <Language />; return <Language />;
case MODAL.SHARE: case MODAL.SHARE:
return <Share title={title} />; return <Share title={title} setModal={setModal} />;
default: default:
return <></>; return <></>;
} }

View File

@ -8,13 +8,15 @@ import {
useDiagram, useDiagram,
useEnums, useEnums,
useNotes, useNotes,
useSaveState,
useTransform, useTransform,
useTypes, useTypes,
} from "../../../hooks"; } from "../../../hooks";
import { databases } from "../../../data/databases"; import { databases } from "../../../data/databases";
import { octokit } from "../../../data/octokit"; import { octokit } from "../../../data/octokit";
import { MODAL, State } from "../../../data/constants";
export default function Share({ title }) { export default function Share({ title, setModal }) {
const { t } = useTranslation(); const { t } = useTranslation();
const { gistId, setGistId } = useContext(IdContext); const { gistId, setGistId } = useContext(IdContext);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
@ -24,6 +26,7 @@ export default function Share({ title }) {
const { types } = useTypes(); const { types } = useTypes();
const { enums } = useEnums(); const { enums } = useEnums();
const { transform } = useTransform(); const { transform } = useTransform();
const { setSaveState } = useSaveState();
const url = const url =
window.location.origin + window.location.pathname + "?shareId=" + gistId; window.location.origin + window.location.pathname + "?shareId=" + gistId;
@ -51,6 +54,22 @@ export default function Share({ title }) {
transform, transform,
]); ]);
const unshare = useCallback(async () => {
try {
await octokit.request(`DELETE /gists/${gistId}`, {
gist_id: gistId,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
});
setGistId("");
setModal(MODAL.NONE);
setSaveState(State.SAVING);
} catch (e) {
console.error(e);
}
}, [gistId, setGistId, setModal, setSaveState]);
const updateGist = useCallback(async () => { const updateGist = useCallback(async () => {
setLoading(true); setLoading(true);
try { try {
@ -136,17 +155,16 @@ export default function Share({ title }) {
<div> <div>
<div className="flex gap-3"> <div className="flex gap-3">
<Input value={url} size="large" /> <Input value={url} size="large" />
<Button </div>
size="large" <div className="text-xs mt-2">{t("share_info")}</div>
theme="solid" <div className="flex gap-2 mt-3">
icon={<IconLink />} <Button block onClick={unshare}>
onClick={copyLink} {t("unshare")}
> </Button>
<Button block theme="solid" icon={<IconLink />} onClick={copyLink}>
{t("copy_link")} {t("copy_link")}
</Button> </Button>
</div> </div>
<hr className="opacity-20 mt-3 mb-1" />
<div className="text-xs">{t("share_info")}</div>
</div> </div>
); );
} }

View File

@ -237,6 +237,7 @@ const en = {
didnt_find_diagram: "Oops! Didn't find the diagram.", didnt_find_diagram: "Oops! Didn't find the diagram.",
unsigned: "Unsigned", unsigned: "Unsigned",
share: "Share", share: "Share",
unshare: "Unshare",
copy_link: "Copy link", copy_link: "Copy link",
readme: "README", readme: "README",
failed_to_load: "Failed to load. Make sure the link is correct.", failed_to_load: "Failed to load. Make sure the link is correct.",