Skip to content

Commit

Permalink
enhance: context menu on custom reports page (#3776)
Browse files Browse the repository at this point in the history
* enhance: context menu on custom reports page

* chore: release note

* chore: lint

* chore: use both feature flags

* chore: use both feature flags

* chore: pr feedback

* fix: changing name with context menu
  • Loading branch information
UnderKoen authored Dec 29, 2024
1 parent e6aeea6 commit 5c577aa
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 20 deletions.
67 changes: 47 additions & 20 deletions packages/desktop-client/src/components/reports/ReportCard.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, {
useRef,
useState,
type ComponentProps,
type ReactNode,
type CSSProperties,
} from 'react';

import { useContextMenu } from '../../hooks/useContextMenu';
import { useIsInViewport } from '../../hooks/useIsInViewport';
import { useNavigate } from '../../hooks/useNavigate';
import { theme } from '../../style';
Expand All @@ -19,6 +19,7 @@ import { NON_DRAGGABLE_AREA_CLASS_NAME } from './constants';

type ReportCardProps = {
isEditing?: boolean;
disableClick?: boolean;
to?: string;
children: ReactNode;
menuItems?: ComponentProps<typeof Menu>['items'];
Expand All @@ -29,6 +30,7 @@ type ReportCardProps = {

export function ReportCard({
isEditing,
disableClick,
to,
menuItems,
onMenuSelect,
Expand Down Expand Up @@ -95,7 +97,7 @@ export function ReportCard({
<Layout {...layoutProps}>
<View
role="button"
onClick={isEditing ? undefined : () => navigate(to)}
onClick={isEditing || disableClick ? undefined : () => navigate(to)}
style={{
height: '100%',
width: '100%',
Expand All @@ -119,10 +121,21 @@ type LayoutProps = {

function Layout({ children, isEditing, menuItems, onMenuSelect }: LayoutProps) {
const triggerRef = useRef(null);
const [menuOpen, setMenuOpen] = useState(false);
const viewRef = useRef(null);

const {
setMenuOpen,
menuOpen,
handleContextMenu,
resetPosition,
position,
asContextMenu,
} = useContextMenu();

return (
<View
ref={viewRef}
onContextMenu={handleContextMenu}
style={{
display: 'block',
height: '100%',
Expand All @@ -135,32 +148,46 @@ function Layout({ children, isEditing, menuItems, onMenuSelect }: LayoutProps) {
},
}}
>
{menuItems && isEditing && (
<View
className={[
menuOpen ? undefined : 'hover-visible',
NON_DRAGGABLE_AREA_CLASS_NAME,
].join(' ')}
style={{
position: 'absolute',
top: 7,
right: 3,
zIndex: 1,
}}
>
<MenuButton ref={triggerRef} onPress={() => setMenuOpen(true)} />
{menuItems && (
<>
{isEditing && (
<View
className={[
menuOpen ? undefined : 'hover-visible',
NON_DRAGGABLE_AREA_CLASS_NAME,
].join(' ')}
style={{
position: 'absolute',
top: 7,
right: 3,
zIndex: 1,
}}
>
<MenuButton
ref={triggerRef}
onPress={() => {
resetPosition();
setMenuOpen(true);
}}
/>
</View>
)}

<Popover
triggerRef={triggerRef}
isOpen={menuOpen}
triggerRef={asContextMenu ? viewRef : triggerRef}
isOpen={Boolean(menuOpen)}
onOpenChange={() => setMenuOpen(false)}
isNonModal
placement={asContextMenu ? 'bottom start' : 'bottom end'}
{...position}
>
<Menu
className={NON_DRAGGABLE_AREA_CLASS_NAME}
onMenuSelect={onMenuSelect}
items={menuItems}
/>
</Popover>
</View>
</>
)}

{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export function CashFlowCard({
return (
<ReportCard
isEditing={isEditing}
disableClick={nameMenuOpen}
to={`/reports/cash-flow/${widgetId}`}
menuItems={[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function CustomReportListCardsInner({
return (
<ReportCard
isEditing={isEditing}
disableClick={nameMenuOpen}
to={`/reports/custom/${report.id}`}
menuItems={[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function MarkdownCard({
return (
<ReportCard
isEditing={isEditing}
disableClick={isVisibleTextArea}
menuItems={[
{
type: Menu.label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export function NetWorthCard({
return (
<ReportCard
isEditing={isEditing}
disableClick={nameMenuOpen}
to={`/reports/net-worth/${widgetId}`}
menuItems={[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function SpendingCard({
return (
<ReportCard
isEditing={isEditing}
disableClick={nameMenuOpen}
to={`/reports/spending/${widgetId}`}
menuItems={[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function SummaryCard({
return (
<ReportCard
isEditing={isEditing}
disableClick={nameMenuOpen}
to={`/reports/summary/${widgetId}`}
menuItems={[
{
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3776.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [UnderKoen]
---

Add context menus to custom reports dashboard

0 comments on commit 5c577aa

Please sign in to comment.