Implement Share button in HackOptionsMenu

This commit is contained in:
Jared Schoeny 2025-10-30 12:48:57 -10:00
parent 2bb33483ff
commit 8609ab451a

View File

@ -1,7 +1,6 @@
"use client";
import React from "react";
import { useRouter } from "next/navigation";
import { FiMoreVertical } from "react-icons/fi";
import { Menu, MenuButton, MenuItem, MenuItems, MenuSeparator } from "@headlessui/react";
@ -11,8 +10,6 @@ interface HackOptionsMenuProps {
}
export default function HackOptionsMenu({ slug, canEdit }: HackOptionsMenuProps) {
const router = useRouter();
return (
<Menu as="div" className="relative">
<MenuButton
@ -29,8 +26,24 @@ export default function HackOptionsMenu({ slug, canEdit }: HackOptionsMenuProps)
>
<MenuItem
as="button"
onClick={() => {
// TODO: Implement share
onClick={async () => {
try {
const url = window.location.href;
const title = document?.title || "Check this out";
if (navigator.share) {
await navigator.share({ title, url });
} else {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(url);
alert("Link copied to clipboard");
}
}
} catch (e) {
// Ignore if user cancels share; otherwise log
if (!(e instanceof Error) || e.name !== "AbortError") {
console.error(e);
}
}
}}
className="block w-full px-3 py-2 text-left text-sm data-focus:bg-black/5 dark:data-focus:bg-white/10">
Share