add context menu, add context menu to video component
continuous-integration/drone/push Build is failing Details

nextui
Guus van Meerveld 8 months ago
parent 0efd9ed1f8
commit ed53ae1ea1

@ -22,7 +22,8 @@
"react-dom": "^18", "react-dom": "^18",
"react-icons": "^5.0.1", "react-icons": "^5.0.1",
"use-debounce": "^10.0.0", "use-debounce": "^10.0.0",
"zod": "^3.22.4" "zod": "^3.22.4",
"zustand": "^4.5.2"
}, },
"devDependencies": { "devDependencies": {
"@tanstack/react-query-devtools": "^5.27.8", "@tanstack/react-query-devtools": "^5.27.8",

@ -0,0 +1,62 @@
import useContextMenuStore from "@/hooks/useContextMenuStore";
import { Component } from "@/typings/component";
import { Listbox, ListboxItem } from "@nextui-org/listbox";
import { useCallback, useEffect } from "react";
const Menu: Component = () => {
const shouldShow = useContextMenuStore((state) => state.show);
const menu = useContextMenuStore((state) => state.items);
const hide = useContextMenuStore((state) => state.hide);
const location = useContextMenuStore((state) => state.location);
const handleClick = useCallback(() => {
if (shouldShow) hide();
}, [hide, shouldShow]);
useEffect(() => {
window.addEventListener("click", handleClick);
return () => {
window.removeEventListener("click", handleClick);
};
}, [hide, shouldShow]);
return (
<div
style={{
top: location.y,
left: location.x,
display: shouldShow ? "block" : "none"
}}
className="bg-background border-small max-w-xs px-1 py-2 rounded-small border-default-200 absolute z-10"
>
<Listbox aria-label="Context Menu">
{menu.map((item) => (
<ListboxItem
onClick={() => {
if (item.onClick) {
item.onClick();
hide();
}
}}
showDivider={item.showDivider}
key={item.key}
href={item.href}
>
{item.title}
</ListboxItem>
))}
</Listbox>
</div>
);
};
export const ContextMenuProvider: Component = ({ children }) => {
return (
<>
{children}
<Menu />
</>
);
};

@ -3,13 +3,16 @@
import { NextUIProvider } from "@nextui-org/react"; import { NextUIProvider } from "@nextui-org/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { ContextMenuProvider } from "./ContextMenuProvider";
export function Providers({ children }: { children: React.ReactNode }) { export function Providers({ children }: { children: React.ReactNode }) {
const queryClient = new QueryClient(); const queryClient = new QueryClient();
return ( return (
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<ReactQueryDevtools initialIsOpen={false} /> <ReactQueryDevtools initialIsOpen={false} />
<NextUIProvider>{children}</NextUIProvider> <NextUIProvider>
<ContextMenuProvider>{children}</ContextMenuProvider>
</NextUIProvider>
</QueryClientProvider> </QueryClientProvider>
); );
} }

@ -42,7 +42,7 @@ export const Trending: Component = ({}) => {
refetch, refetch,
data data
} = useQuery({ } = useQuery({
queryKey: ["trending", region], queryKey: ["trending", region?.code],
queryFn: () => { queryFn: () => {
if (region === null) return; if (region === null) return;

@ -1,23 +1,22 @@
import useContextMenuStore from "@/hooks/useContextMenuStore";
import { Component } from "@/typings/component"; import { Component } from "@/typings/component";
import { Listbox, ListboxItem } from "@nextui-org/listbox"; import { ContextMenuItem } from "@/typings/contextMenu";
export interface ContextMenuItem {
title: string;
key: string;
href?: string;
onClick?: () => any;
}
export const ContextMenu: Component<{ menu: ContextMenuItem[] }> = ({ export const ContextMenu: Component<{ menu: ContextMenuItem[] }> = ({
menu menu,
children
}) => { }) => {
const showContextMenu = useContextMenuStore((state) => state.showContextMenu);
return ( return (
<Listbox aria-label="Context Menu"> <div
{menu.map((item) => ( onContextMenu={(e) => {
<ListboxItem onClick={item.onClick} key={item.key} href={item.href}> e.preventDefault();
{item.title}
</ListboxItem> showContextMenu(e.pageX, e.pageY, menu);
))} }}
</Listbox> >
{children}
</div>
); );
}; };

@ -6,19 +6,52 @@ import { Divider } from "@nextui-org/divider";
import Link from "next/link"; import Link from "next/link";
import formatViewCount from "@/utils/formatViewCount"; import formatViewCount from "@/utils/formatViewCount";
import formatDuration from "@/utils/formatDuration"; import formatDuration from "@/utils/formatDuration";
import { ContextMenu } from "./ContextMenu";
import formatUploadedTime from "@/utils/formatUploadedTime";
import { Tooltip } from "@nextui-org/tooltip";
import { ContextMenuItem } from "@/typings/contextMenu";
export const Video: Component<{ data: VideoPreview }> = ({ data: video }) => { export const Video: Component<{ data: VideoPreview }> = ({ data: video }) => {
const handleContextMenu = () => {}; const url = `/watch?v=${video.id}`;
const channelUrl = `/channel/${video.author.id}`;
const menuItems: ContextMenuItem[] = [
{ title: "Go to video", key: "gotoVideo", href: url },
{
title: "Copy video id",
key: "videoId",
onClick: () => {
navigator.clipboard.writeText(video.id);
},
showDivider: true
},
{
title: "Open thumbnail",
key: "thumbnail",
href: video.thumbnail
},
{
title: "Copy thumnail url",
key: "thumbnailUrl",
onClick: () => {
navigator.clipboard.writeText(video.thumbnail);
},
showDivider: true
},
{ title: "Go to channel", key: "gotoChannel", href: channelUrl },
{
title: "Copy channel id",
key: "channelId",
onClick: () => {
navigator.clipboard.writeText(video.author.id);
}
}
];
return ( return (
<Link href={`/watch?v=${video.id}`}> <Link href={url}>
<Card <ContextMenu menu={menuItems}>
radius="lg" <Card radius="lg">
onContextMenu={(e) => {
e.preventDefault();
handleContextMenu();
}}
>
<CardBody> <CardBody>
<Image <Image
alt={video.title} alt={video.title}
@ -41,9 +74,11 @@ export const Video: Component<{ data: VideoPreview }> = ({ data: video }) => {
<p className="text-small font-semibold tracking-tight text-default-400"> <p className="text-small font-semibold tracking-tight text-default-400">
{video.author.name} {video.author.name}
</p> </p>
<Tooltip showArrow content={video.uploaded.toLocaleString()}>
<p className="text-small tracking-tight text-default-400"> <p className="text-small tracking-tight text-default-400">
{video.uploaded.toLocaleDateString()} {formatUploadedTime(video.uploaded)}
</p> </p>
</Tooltip>
<p className="text-small tracking-tight text-default-400"> <p className="text-small tracking-tight text-default-400">
Views: {formatViewCount(video.views)} Views: {formatViewCount(video.views)}
@ -52,6 +87,7 @@ export const Video: Component<{ data: VideoPreview }> = ({ data: video }) => {
</div> </div>
</CardFooter> </CardFooter>
</Card> </Card>
</ContextMenu>
</Link> </Link>
); );
}; };

@ -0,0 +1,27 @@
import { ContextMenuItem } from "@/typings/contextMenu";
import { create } from "zustand";
interface Location {
x: number;
y: number;
}
interface ContextMenuStore {
show: boolean;
location: Location;
items: ContextMenuItem[];
showContextMenu: (x: number, y: number, items: ContextMenuItem[]) => void;
hide: () => void;
}
const useContextMenuStore = create<ContextMenuStore>((set) => ({
show: false,
location: { x: 0, y: 0 },
items: [],
showContextMenu(x, y, items) {
set({ show: true, location: { x, y }, items });
},
hide: () => set({ show: false })
}));
export default useContextMenuStore;

@ -0,0 +1,7 @@
export interface ContextMenuItem {
title: string;
key: string;
showDivider?: boolean;
href?: string;
onClick?: () => any;
}

@ -0,0 +1,7 @@
import { DateTime } from "luxon";
const formatUploadedTime = (uploaded: Date): string => {
return DateTime.fromJSDate(uploaded).toRelative() ?? "";
};
export default formatUploadedTime;

@ -6486,6 +6486,11 @@ use-sidecar@^1.1.2:
detect-node-es "^1.1.0" detect-node-es "^1.1.0"
tslib "^2.0.0" tslib "^2.0.0"
use-sync-external-store@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/use-sync-external-store/-/use-sync-external-store-1.2.0.tgz#7dbefd6ef3fe4e767a0cf5d7287aacfb5846928a"
integrity sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==
util-deprecate@^1.0.2: util-deprecate@^1.0.2:
version "1.0.2" version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
@ -6794,3 +6799,10 @@ zod@^3.22.4:
version "3.22.4" version "3.22.4"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff" resolved "https://registry.yarnpkg.com/zod/-/zod-3.22.4.tgz#f31c3a9386f61b1f228af56faa9255e845cf3fff"
integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg== integrity sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==
zustand@^4.5.2:
version "4.5.2"
resolved "https://registry.yarnpkg.com/zustand/-/zustand-4.5.2.tgz#fddbe7cac1e71d45413b3682cdb47b48034c3848"
integrity sha512-2cN1tPkDVkwCy5ickKrI7vijSjPksFRfqS6237NzT0vqSsztTNnQdHw9mmN7uBdk3gceVXU0a+21jFzFzAc9+g==
dependencies:
use-sync-external-store "1.2.0"

Loading…
Cancel
Save