Skip to content

Commit

Permalink
fix(GoalTableList): move open preview click handler inside `<GoalTabl…
Browse files Browse the repository at this point in the history
…eList />`
  • Loading branch information
LamaEats committed Feb 29, 2024
1 parent 2581e17 commit 77746e5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 86 deletions.
23 changes: 3 additions & 20 deletions src/components/DashboardPage/DashboardPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { MouseEventHandler, useCallback, useEffect, useMemo } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import { ListView, nullable } from '@taskany/bricks';
import { TreeViewElement } from '@taskany/bricks/harmony';

Expand Down Expand Up @@ -61,7 +61,7 @@ export const DashboardPage = ({ user, ssrTime, defaultPresetFallback }: External

useFMPMetric(!!data);

const { setPreview, preview, on } = useGoalPreview();
const { setPreview, on } = useGoalPreview();

useEffect(() => {
const unsubUpdate = on('on:goal:update', () => {
Expand All @@ -78,23 +78,6 @@ export const DashboardPage = ({ user, ssrTime, defaultPresetFallback }: External
};
}, [on, utils.project.getUserProjectsWithGoals]);

useEffect(() => {
const isGoalDeletedAlready = preview && !goals?.some((g) => g.id === preview.id);

if (isGoalDeletedAlready) setPreview(null);
}, [goals, preview, setPreview]);

const onGoalPreviewShow = useCallback(
(goal: Parameters<typeof setPreview>[1]): MouseEventHandler<HTMLAnchorElement> =>
(e) => {
if (e.metaKey || e.ctrlKey || !goal?._shortId) return;

e.preventDefault();
setPreview(goal._shortId, goal);
},
[setPreview],
);

const onFilterStar = useCallback(async () => {
await utils.filter.getById.invalidate();
}, [utils]);
Expand Down Expand Up @@ -147,7 +130,7 @@ export const DashboardPage = ({ user, ssrTime, defaultPresetFallback }: External
href={routes.project(project.id)}
goals={nullable(goals, (g) => (
<TreeViewElement>
<GoalTableList goals={g} onGoalPreviewShow={onGoalPreviewShow} />
<GoalTableList goals={g} />
</TreeViewElement>
))}
>
Expand Down
23 changes: 3 additions & 20 deletions src/components/FlatGoalList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentProps, MouseEventHandler, useCallback, useEffect, useMemo, useState } from 'react';
import { ComponentProps, useCallback, useEffect, useMemo, useState } from 'react';
import { ListView, nullable } from '@taskany/bricks';

import { QueryState } from '../hooks/useUrlFilterParams';
Expand All @@ -20,7 +20,7 @@ const pageSize = 20;

export const FlatGoalList: React.FC<GoalListProps> = ({ queryState, onTagClick }) => {
const utils = trpc.useContext();
const { preview, setPreview, on } = useGoalPreview();
const { setPreview, on } = useGoalPreview();

const [, setPage] = useState(0);
const { data, fetchNextPage, hasNextPage } = trpc.goal.getBatch.useInfiniteQuery(
Expand Down Expand Up @@ -59,23 +59,6 @@ export const FlatGoalList: React.FC<GoalListProps> = ({ queryState, onTagClick }
setPage((prev) => prev++);
}, [fetchNextPage]);

useEffect(() => {
const isGoalDeletedAlready = preview && !goalsOnScreen?.some((g) => g.id === preview.id);

if (isGoalDeletedAlready) setPreview(null);
}, [goalsOnScreen, preview, setPreview]);

const onGoalPreviewShow = useCallback(
(goal: Parameters<typeof setPreview>[1]): MouseEventHandler<HTMLAnchorElement> =>
(e) => {
if (e.metaKey || e.ctrlKey || !goal?._shortId) return;

e.preventDefault();
setPreview(goal._shortId, goal);
},
[setPreview],
);

const handleItemEnter = useCallback(
(goal: NonNullable<GoalByIdReturnType>) => {
setPreview(goal._shortId, goal);
Expand All @@ -86,7 +69,7 @@ export const FlatGoalList: React.FC<GoalListProps> = ({ queryState, onTagClick }
return (
<ListView onKeyboardClick={handleItemEnter}>
{nullable(goalsOnScreen, (goals) => (
<GoalTableList goals={goals} onTagClick={onTagClick} onGoalPreviewShow={onGoalPreviewShow} />
<GoalTableList goals={goals} onTagClick={onTagClick} />
))}

{nullable(hasNextPage, () => (
Expand Down
34 changes: 30 additions & 4 deletions src/components/GoalTableList/GoalTableList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Badge, State, Table, Tag, Text, User, UserGroup } from '@taskany/bricks/harmony';
import { MouseEventHandler, useMemo } from 'react';
import { MouseEventHandler, useCallback, useEffect, useMemo } from 'react';
import { Link, ListViewItem, nullable } from '@taskany/bricks';
import { IconMessageTextOutline } from '@taskany/icons';

Expand All @@ -21,18 +21,44 @@ import s from './GoalTableList.module.css';

interface GoalTableListProps<T> {
goals: T[];
onGoalPreviewShow: (goal: T) => MouseEventHandler<HTMLAnchorElement>;
onGoalPreviewShow?: (goal: T) => MouseEventHandler<HTMLAnchorElement>;
onGoalClick?: MouseEventHandler<HTMLAnchorElement>;
onTagClick?: (tag: { id: string }) => MouseEventHandler<HTMLDivElement>;
}

export const GoalTableList = <T extends Partial<NonNullable<GoalByIdReturnType>>>({
goals,
onGoalPreviewShow,
onGoalClick,
onTagClick,
...attrs
}: GoalTableListProps<T>) => {
const locale = useLocale();
const { shortId, preview } = useGoalPreview();
const { shortId, preview, setPreview, on } = useGoalPreview();

useEffect(() => {
const unsubDelete = on('on:goal:delete', (updatedId) => {
const idInList = goals.find(({ _shortId }) => _shortId === updatedId);
if (idInList) {
setPreview(null);
}
});

return () => {
unsubDelete();
};
}, [goals, preview, setPreview, on]);

const onGoalPreviewShow = useCallback(
(goal: Parameters<typeof setPreview>[1]): MouseEventHandler<HTMLAnchorElement> =>
(e) => {
if (e.metaKey || e.ctrlKey || !goal?._shortId) return;

e.preventDefault();
setPreview(goal._shortId, goal);
onGoalClick?.(e);
},
[setPreview, onGoalClick],
);

const data = useMemo(
() =>
Expand Down
14 changes: 1 addition & 13 deletions src/components/GroupedGoalList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentProps, MouseEventHandler, useCallback, useEffect, useMemo } from 'react';
import { ComponentProps, useCallback, useEffect, useMemo } from 'react';
import { ListView, nullable } from '@taskany/bricks';

import { QueryState } from '../hooks/useUrlFilterParams';
Expand Down Expand Up @@ -51,17 +51,6 @@ export const GroupedGoalList: React.FC<GroupedGoalListProps> = ({ queryState, on

useFMPMetric(!!data);

const onGoalPreviewShow = useCallback(
(goal: Parameters<typeof setPreview>[1]): MouseEventHandler<HTMLAnchorElement> =>
(e) => {
if (e.metaKey || e.ctrlKey || !goal?._shortId) return;

e.preventDefault();
setPreview(goal._shortId, goal);
},
[setPreview],
);

const projectsOnScreen = useMemo(() => {
const pages = data?.pages || [];

Expand All @@ -81,7 +70,6 @@ export const GroupedGoalList: React.FC<GroupedGoalListProps> = ({ queryState, on
<ProjectListItemConnected
key={project.id}
project={project}
onClickProvider={onGoalPreviewShow}
queryState={queryState}
onTagClick={onTagClick}
/>
Expand Down
18 changes: 2 additions & 16 deletions src/components/ProjectListItemConnected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const onProjectClickHandler = (e: React.MouseEvent) => {
export const ProjectListItemConnected: FC<ProjectListItemConnectedProps> = ({
queryState,
project,
onClickProvider,
onTagClick,
...props
}) => {
Expand Down Expand Up @@ -80,14 +79,7 @@ export const ProjectListItemConnected: FC<ProjectListItemConnectedProps> = ({
project={project}
goals={nullable(projectDeepInfo?.goals, (goals) => (
<TreeViewElement>
<GoalTableList
goals={goals}
onTagClick={onTagClick}
onGoalPreviewShow={(goal) => (e) => {
onClickProvider?.(goal)(e);
onProjectClickHandler(e);
}}
/>
<GoalTableList goals={goals} onTagClick={onTagClick} onGoalClick={onProjectClickHandler} />
</TreeViewElement>
))}
{...props}
Expand All @@ -99,13 +91,7 @@ export const ProjectListItemConnected: FC<ProjectListItemConnectedProps> = ({
)}
</TreeViewElement>
{childrenProjects.map((p) => (
<ProjectListItemConnected
key={p.id}
project={p}
queryState={queryState}
onClickProvider={onClickProvider}
titleSize="m"
/>
<ProjectListItemConnected key={p.id} project={p} queryState={queryState} titleSize="m" />
))}
</ProjectListItemCollapsable>
);
Expand Down
14 changes: 1 addition & 13 deletions src/components/ProjectPage/ProjectPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MouseEventHandler, useCallback, useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import { ListView, nullable } from '@taskany/bricks';

import { Page } from '../Page/Page';
Expand Down Expand Up @@ -75,17 +75,6 @@ export const ProjectPage = ({ user, ssrTime, params: { id }, defaultPresetFallba
};
}, [on, utils.project.getDeepInfo, utils.project.getById]);

const onGoalPreviewShow = useCallback(
(goal: Parameters<typeof setPreview>[1]): MouseEventHandler<HTMLAnchorElement> =>
(e) => {
if (e.metaKey || e.ctrlKey || !goal?._shortId) return;

e.preventDefault();
setPreview(goal._shortId, goal);
},
[setPreview],
);

useEffect(() => {
if (!project || project.personal) return;

Expand Down Expand Up @@ -174,7 +163,6 @@ export const ProjectPage = ({ user, ssrTime, params: { id }, defaultPresetFallba
visible
project={p}
onTagClick={setTagsFilterOutside}
onClickProvider={onGoalPreviewShow}
queryState={queryState}
/>
))}
Expand Down

0 comments on commit 77746e5

Please sign in to comment.