Skip to content

Commit

Permalink
Merge branch 'jcarstairs-scottlogic-group-elements-use-friendly-dates'
Browse files Browse the repository at this point in the history
  • Loading branch information
ransome1 committed Aug 4, 2024
2 parents 6252fad + a2b0af0 commit f975507
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/renderer/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const GridComponent: React.FC<GridComponentProps> = memo(({
return (
<React.Fragment key={group.title}>
<Group
settings={settings}
title={group.title}
todotxtAttribute={settings.sorting[0].value}
filters={filters}
Expand Down
41 changes: 36 additions & 5 deletions src/renderer/Grid/Group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,56 @@ import React, { memo } from 'react';
import ListItem from '@mui/material/ListItem';
import Button from '@mui/material/Button';
import Divider from '@mui/material/Divider';
import dayjs from 'dayjs';
import updateLocale from 'dayjs/plugin/updateLocale';
import { friendlyDate } from 'renderer/Shared';
import { i18n } from 'renderer/Settings/LanguageSelector';
import { WithTranslation, withTranslation } from 'react-i18next';
dayjs.extend(updateLocale);

interface GroupProps {
interface FormatGroupElementProps {
groupElement: string;
settings: Settings;
t: typeof i18n.t;
todotxtAttribute: string;
}

function formatGroupElement({ groupElement, settings, t, todotxtAttribute }: FormatGroupElementProps) {
// If group element is a date, then format according to user preferences
if (
['due', 't'].includes(todotxtAttribute)
&& dayjs(groupElement).isValid()
&& settings.useHumanFriendlyDates
) {
return friendlyDate(groupElement, todotxtAttribute, settings, t).pop();
}

// No transformation required: display as-is
return groupElement;
}

interface GroupProps extends WithTranslation {
settings: Settings;
title: string | string[];
todotxtAttribute: string;
filters: Filters | null;
onClick: Function;
t: typeof i18n.t;
}

const Group: React.FC<GroupProps> = memo(({ title, todotxtAttribute, filters, onClick }) => {

const Group: React.FC<GroupProps> = memo(({ settings, t, title, todotxtAttribute, filters, onClick }) => {
if (!title || title.length === 0) {
return <ListItem className="row group"><Divider /></ListItem>;
}

const groupElements = (typeof title === 'string') ? [title] : title
const formattedGroupElements = groupElements.map(
(groupElement) => formatGroupElement({ groupElement, settings, t, todotxtAttribute })
);

return (
<ListItem className="row group">
{groupElements.map((groupElement, index) => {
{formattedGroupElements.map((groupElement, index) => {
const selected: boolean = filters && (filters[todotxtAttribute as keyof Filters] || []).some(
(filter: Filter) => filter && filter.name === groupElement.trim()
);
Expand All @@ -41,4 +72,4 @@ const Group: React.FC<GroupProps> = memo(({ title, todotxtAttribute, filters, on
);
});

export default Group;
export default withTranslation()(Group);
3 changes: 2 additions & 1 deletion src/renderer/Shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import duration from 'dayjs/plugin/duration';
import calendar from 'dayjs/plugin/calendar';
import weekday from 'dayjs/plugin/weekday';
import updateLocale from 'dayjs/plugin/updateLocale';
import { i18n } from './Settings/LanguageSelector';
dayjs.extend(relativeTime);
dayjs.extend(duration);
dayjs.extend(calendar);
Expand Down Expand Up @@ -112,4 +113,4 @@ export const friendlyDate = (value: string, attributeKey: string, settings: Sett
}

return results;
};
};

0 comments on commit f975507

Please sign in to comment.