Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support preset target #3169

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generated/kysely/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export type Filter = {
title: string;
description: string | null;
params: string;
target: string | null;
default: Generated<boolean>;
activityId: string | null;
createdAt: Generated<Timestamp>;
Expand Down
120 changes: 120 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Filter" ADD COLUMN "target" TEXT;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ model Filter {
title String
description String?
params String
target String?
default Boolean @default(false)
activity Activity? @relation(fields: [activityId], references: [id])
activityId String?
Expand Down
3 changes: 1 addition & 2 deletions src/components/DashboardPage/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ export const DashboardPage = ({ user, ssrTime, defaultPresetFallback }: External
<FiltersPanel
title={getPageTitle({
title: tr('Dashboard'),
shadowPresetTitle: currentPreset?.title,
currentPresetTitle: currentPreset?.title,
presetTitle: currentPreset?.title,
})}
total={totalGoalsCount}
counter={goalsCount}
Expand Down
4 changes: 4 additions & 0 deletions src/components/FilterCreateForm/FilterCreateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { createFilterSchema, CreateFilter } from '../../schema/filter';
import { useFilterResource } from '../../hooks/useFilterResource';
import { ModalEvent, dispatchModalEvent } from '../../utils/dispatchModal';
import { FormAction, FormActions } from '../FormActions/FormActions';
import { useRouter } from '../../hooks/router';

import { tr } from './FilterCreateForm.i18n';

Expand All @@ -31,6 +32,8 @@ const FilterCreateForm: React.FC<FilterCreateFormProps> = ({ mode, params, onSub
const { createFilter } = useFilterResource();
const [formBusy, setFormBusy] = useState(false);

const { appRouter } = useRouter();

const {
control,
register,
Expand All @@ -45,6 +48,7 @@ const FilterCreateForm: React.FC<FilterCreateFormProps> = ({ mode, params, onSub
defaultValues: {
mode,
params,
target: appRouter.asPath.split('?')[0] ?? '',
},
});

Expand Down
3 changes: 1 addition & 2 deletions src/components/GoalsPage/GoalsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export const GoalsPage = ({ user, ssrTime, defaultPresetFallback, baseQueryState
<FiltersPanel
title={getPageTitle({
title: tr('Goals'),
shadowPresetTitle: currentPreset?.title,
currentPresetTitle: currentPreset?.title,
presetTitle: currentPreset?.title,
})}
total={data?.count || 0}
counter={data?.filtered || 0}
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageNavigation/PageNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const PageNavigation: FC<AppNavigationProps> = ({ logo }) => {
const { goalsRoutes, presetRoutes, projectsRoutes, isPresetActive } = useMemo(() => {
const presetRoutes = presets.map((preset) => ({
title: preset.title,
href: routes.goals(preset.id),
href: routes.preset(preset.id, preset.target ?? ''),
}));

return {
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const routes = {
crewUser: (login: string) => `${process.env.NEXT_PUBLIC_CREW_URL}${login}`,

jiraTask: (id: string) => `${process.env.NEXT_PUBLIC_JIRA_URL}browse/${id}`,

preset: (filterId: string, target?: string) => `${target}?filter=${filterId}`,
};

export const useRouter = () => {
Expand All @@ -55,6 +57,9 @@ export const useRouter = () => {
exploreGoals: () => router.push(routes.exploreGoals()),

help: (slug: AvailableHelpPages) => router.push(slug),

preset: (filterId: string, target?: string) => router.push(routes.preset(filterId, target)),

appRouter: router,
}),
[router],
Expand Down
30 changes: 22 additions & 8 deletions src/hooks/useFiltersPreset.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { useEffect, useMemo } from 'react';
import { useRouter } from 'next/router';
import { deleteCookie } from '@taskany/bricks';

import { trpc } from '../utils/trpcClient';
import { refreshInterval } from '../utils/config';
import { filtersNoSearchPresetCookie } from '../utils/parseUrlParams';

import { useRouter } from './router';

export const useFiltersPreset = ({ defaultPresetFallback = true }: { defaultPresetFallback?: boolean }) => {
const router = useRouter();
const queryString = router.asPath.split('?')[1];
const { appRouter, preset: goToPreset } = useRouter();
const [baseRoute, queryString] = appRouter.asPath.split('?');

const userPreset = trpc.filter.getById.useQuery(router.query.filter as string, { enabled: !!router.query.filter });
const userPreset = trpc.filter.getById.useQuery(appRouter.query.filter as string, {
enabled: !!appRouter.query.filter,
});
const defaultPreset = trpc.filter.getDefaultFilter.useQuery(undefined, {
enabled: defaultPresetFallback,
});
Expand All @@ -28,14 +31,25 @@ export const useFiltersPreset = ({ defaultPresetFallback = true }: { defaultPres
}
}, [defaultPresetFallback]);

const shadowPreset = useMemo(
() =>
userFilters.data?.find(
(f) => decodeURIComponent(f.params) === decodeURIComponent(queryString) && baseRoute === f.target,
),
[baseRoute, userFilters, queryString],
);

useEffect(() => {
if (shadowPreset) {
goToPreset(shadowPreset.id, shadowPreset.target ?? '');
}
}, [shadowPreset, goToPreset]);

return useMemo(
() => ({
preset: preset.data,
shadowPreset: userFilters.data?.find(
(f) => decodeURIComponent(f.params) === decodeURIComponent(queryString),
),
userFilters: userFilters.data,
}),
[preset.data, userFilters.data, queryString],
[preset.data, userFilters.data],
);
};
1 change: 1 addition & 0 deletions src/schema/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const createFilterSchema = z.object({
}),
mode: z.nativeEnum(FilterMode),
params: z.string().min(1),
target: z.string(),
description: z.string().optional(),
});

Expand Down
18 changes: 3 additions & 15 deletions src/utils/getPageTitle.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
export const getPageTitle = ({
title = '',
currentPresetTitle,
shadowPresetTitle,
}: {
title?: string;
shadowPresetTitle?: string;
currentPresetTitle?: string;
}): string => {
if (currentPresetTitle) {
return `${title}: ${currentPresetTitle}`;
}

if (shadowPresetTitle) {
return `${title}: ${shadowPresetTitle}`;
export const getPageTitle = ({ title = '', presetTitle }: { title?: string; presetTitle?: string }): string => {
if (presetTitle) {
return `${title}: ${presetTitle}`;
}

return title;
Expand Down
16 changes: 6 additions & 10 deletions trpc/queries/projectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,9 @@ const getGoalsFiltersWhereExpressionBuilder =
): ExpressionFactory<
DB & {
participant: Nullable<Activity>;
tag: Nullable<{
id: string;
title: string;
description: string | null;
activityId: string;
createdAt: Date;
updatedAt: Date;
}>;
cte_projects: any;
},
'Goal' | 'tag' | 'participant' | 'cte_projects',
'Goal' | 'participant' | 'cte_projects',
SqlBool
> =>
({ or, and, eb, selectFrom, cast, val }) => {
Expand Down Expand Up @@ -372,7 +364,11 @@ const getGoalsFiltersWhereExpressionBuilder =
.select('State.id')
.where('State.type', 'in', goalsQuery?.stateType || []),
),
tag: eb('tag.id', 'in', goalsQuery?.tag || []),
tag: eb('Goal.id', 'in', ({ selectFrom }) =>
selectFrom('_GoalToTag')
.select('A')
.where('B', 'in', goalsQuery?.tag || []),
),
estimate:
// eslint-disable-next-line no-nested-ternary
estimate.length > 0
Expand Down
Loading