Skip to content

Commit

Permalink
feat(PageTitlePreset): added component for page title with applied pr…
Browse files Browse the repository at this point in the history
…eset
  • Loading branch information
troff8 authored and Troff8 committed Jun 28, 2023
1 parent 87b6ca0 commit ebaab23
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 66 deletions.
34 changes: 12 additions & 22 deletions src/components/DashboardPage/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import { CommonHeader } from '../CommonHeader';
import { FiltersPanel } from '../FiltersPanel/FiltersPanel';
import { GoalsGroup } from '../GoalsGroup';
import { GoalsListContainer } from '../GoalListItem';
import { PageTitle } from '../PageTitle';
import { Nullish } from '../../types/void';
import { trpc } from '../../utils/trpcClient';
import { FilterById, GoalByIdReturnType, ProjectByIdReturnType } from '../../../trpc/inferredTypes';
import { ProjectItemStandalone } from '../ProjectListItem';
import { PageTitlePreset } from '../PageTitlePreset/PageTitlePreset';

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

Expand Down Expand Up @@ -154,30 +154,20 @@ export const DashboardPage = ({ user, ssrTime }: ExternalPageProps) => {
[router],
);

const defaultTitle = <PageTitle title={tr('Dashboard')} />;
const presetInfo =
user.activityId !== currentPreset?.activityId
? `${tr('created by')} ${currentPreset?.activity?.user?.name}`
: undefined;
const presetTitle = <PageTitle title={tr('Dashboard')} subtitle={currentPreset?.title} info={presetInfo} />;

const onShadowPresetTitleClick = useCallback(() => {
if (shadowPreset) setPreset(shadowPreset.id);
}, [setPreset, shadowPreset]);
const shadowPresetInfo =
user.activityId !== shadowPreset?.activityId
? `${tr('created by')} ${shadowPreset?.activity?.user?.name}`
: undefined;
const shadowPresetTitle = (
<PageTitle
const title = (
<PageTitlePreset
activityId={user.activityId}
currentPresetActivityId={currentPreset?.activityId}
currentPresetActivityUserName={currentPreset?.activity.user?.name}
currentPresetTitle={currentPreset?.title}
shadowPresetActivityId={shadowPreset?.activityId}
shadowPresetActivityUserName={shadowPreset?.activity.user?.name}
shadowPresetId={shadowPreset?.id}
shadowPresetTitle={shadowPreset?.title}
title={tr('Dashboard')}
subtitle={shadowPreset?.title}
info={shadowPresetInfo}
onClick={onShadowPresetTitleClick}
setPreset={setPreset}
/>
);
// eslint-disable-next-line no-nested-ternary
const title = currentPreset ? presetTitle : shadowPreset ? shadowPresetTitle : defaultTitle;

const description =
currentPreset && currentPreset.description
Expand Down
34 changes: 12 additions & 22 deletions src/components/GoalsPage/GoalsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import { Page, PageContent } from '../Page';
import { CommonHeader } from '../CommonHeader';
import { FiltersPanel } from '../FiltersPanel/FiltersPanel';
import { GoalListItem, GoalsListContainer } from '../GoalListItem';
import { PageTitle } from '../PageTitle';
import { LoadMoreButton } from '../LoadMoreButton/LoadMoreButton';
import { Nullish } from '../../types/void';
import { PageTitlePreset } from '../PageTitlePreset/PageTitlePreset';

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

Expand Down Expand Up @@ -149,30 +149,20 @@ export const GoalsPage = ({ user, ssrTime }: ExternalPageProps) => {
[router],
);

const defaultTitle = <PageTitle title={tr('Goals')} />;
const presetInfo =
user.activityId !== currentPreset?.activityId
? `${tr('created by')} ${currentPreset?.activity?.user?.name}`
: undefined;
const presetTitle = <PageTitle title={tr('Goals')} subtitle={currentPreset?.title} info={presetInfo} />;

const onShadowPresetTitleClick = useCallback(() => {
if (shadowPreset) setPreset(shadowPreset.id);
}, [setPreset, shadowPreset]);
const shadowPresetInfo =
user.activityId !== shadowPreset?.activityId
? `${tr('created by')} ${shadowPreset?.activity?.user?.name}`
: undefined;
const shadowPresetTitle = (
<PageTitle
const title = (
<PageTitlePreset
activityId={user.activityId}
currentPresetActivityId={currentPreset?.activityId}
currentPresetActivityUserName={currentPreset?.activity.user?.name}
currentPresetTitle={currentPreset?.title}
shadowPresetActivityId={shadowPreset?.activityId}
shadowPresetActivityUserName={shadowPreset?.activity.user?.name}
shadowPresetId={shadowPreset?.id}
shadowPresetTitle={shadowPreset?.title}
title={tr('Goals')}
subtitle={shadowPreset?.title}
info={shadowPresetInfo}
onClick={onShadowPresetTitleClick}
setPreset={setPreset}
/>
);
// eslint-disable-next-line no-nested-ternary
const title = currentPreset ? presetTitle : shadowPreset ? shadowPresetTitle : defaultTitle;

const description =
currentPreset && currentPreset.description
Expand Down
3 changes: 3 additions & 0 deletions src/components/PageTitlePreset/PageTitlePreset.i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"created by": "created by"
}
17 changes: 17 additions & 0 deletions src/components/PageTitlePreset/PageTitlePreset.i18n/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
// Do not edit, use generator to update
import { i18n, fmt, I18nLangSet } from 'easy-typed-intl';
import getLang from '../../../utils/getLang';

import ru from './ru.json';
import en from './en.json';

export type I18nKey = keyof typeof ru & keyof typeof en;
type I18nLang = 'ru' | 'en';

const keyset: I18nLangSet<I18nKey> = {};

keyset['ru'] = ru;
keyset['en'] = en;

export const tr = i18n<I18nLang, I18nKey>(keyset, fmt, getLang);
3 changes: 3 additions & 0 deletions src/components/PageTitlePreset/PageTitlePreset.i18n/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"created by": "автор"
}
53 changes: 53 additions & 0 deletions src/components/PageTitlePreset/PageTitlePreset.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { useCallback } from 'react';

import { PageTitle } from '../PageTitle';

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

interface PageTitlePresetProps {
activityId: string;
currentPresetActivityId?: string;
currentPresetActivityUserName?: string | null;
currentPresetTitle?: string;
shadowPresetActivityId?: string;
shadowPresetActivityUserName?: string | null;
shadowPresetId?: string;
shadowPresetTitle?: string;
title?: string;
setPreset: (id: string) => void;
}

export const PageTitlePreset: React.FC<PageTitlePresetProps> = ({
activityId,
currentPresetActivityId,
currentPresetActivityUserName,
currentPresetTitle,
shadowPresetActivityId,
shadowPresetActivityUserName,
shadowPresetId,
shadowPresetTitle,
title,
setPreset,
}) => {
const titleDefault = <PageTitle title={title} />;
const presetInfo =
activityId !== currentPresetActivityId ? `${tr('created by')} ${currentPresetActivityUserName}` : undefined;
const titlePreset = <PageTitle title={title} subtitle={currentPresetTitle} info={presetInfo} />;

const shadowPresetInfo =
activityId !== shadowPresetActivityId ? `${tr('created by')} ${shadowPresetActivityUserName}` : undefined;

const onShadowPresetTitleClick = useCallback(() => {
if (shadowPresetId) setPreset(shadowPresetId);
}, [setPreset, shadowPresetId]);
const titleShadow = (
<PageTitle
title={title}
subtitle={shadowPresetTitle}
info={shadowPresetInfo}
onClick={onShadowPresetTitleClick}
/>
);
// eslint-disable-next-line no-nested-ternary
return currentPresetTitle ? titlePreset : shadowPresetTitle ? titleShadow : titleDefault;
};
34 changes: 12 additions & 22 deletions src/components/ProjectPage/ProjectPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { useFilterResource } from '../../hooks/useFilterResource';
import { useWillUnmount } from '../../hooks/useWillUnmount';
import { ProjectPageLayout } from '../ProjectPageLayout/ProjectPageLayout';
import { Page, PageContent } from '../Page';
import { PageTitle } from '../PageTitle';
import { createFilterKeys } from '../../utils/hotkeys';
import { Nullish } from '../../types/void';
import { trpc } from '../../utils/trpcClient';
import { FilterById, GoalByIdReturnType } from '../../../trpc/inferredTypes';
import { ProjectListItemConnected } from '../ProjectListItemConnected';
import { PageTitlePreset } from '../PageTitlePreset/PageTitlePreset';

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

Expand Down Expand Up @@ -155,30 +155,20 @@ export const ProjectPage = ({ user, ssrTime, params: { id } }: ExternalPageProps
})
.join('');

const defaultTitle = <PageTitle title={project.data?.title} />;
const presetInfo =
user.activityId !== currentPreset?.activityId
? `${tr('created by')} ${currentPreset?.activity?.user?.name}`
: undefined;
const presetTitle = <PageTitle title={project.data?.title} subtitle={currentPreset?.title} info={presetInfo} />;

const onShadowPresetTitleClick = useCallback(() => {
if (shadowPreset) setPreset(shadowPreset.id);
}, [setPreset, shadowPreset]);
const shadowPresetInfo =
user.activityId !== shadowPreset?.activityId
? `${tr('created by')} ${shadowPreset?.activity?.user?.name}`
: undefined;
const shadowPresetTitle = (
<PageTitle
const title = (
<PageTitlePreset
activityId={user.activityId}
currentPresetActivityId={currentPreset?.activityId}
currentPresetActivityUserName={currentPreset?.activity.user?.name}
currentPresetTitle={currentPreset?.title}
shadowPresetActivityId={shadowPreset?.activityId}
shadowPresetActivityUserName={shadowPreset?.activity.user?.name}
shadowPresetId={shadowPreset?.id}
shadowPresetTitle={shadowPreset?.title}
title={project.data?.title}
subtitle={shadowPreset?.title}
info={shadowPresetInfo}
onClick={onShadowPresetTitleClick}
setPreset={setPreset}
/>
);
// eslint-disable-next-line no-nested-ternary
const title = currentPreset ? presetTitle : shadowPreset ? shadowPresetTitle : defaultTitle;
const description =
currentPreset && currentPreset.description ? currentPreset.description : project.data?.description;

Expand Down

0 comments on commit ebaab23

Please sign in to comment.