Skip to content

Commit

Permalink
feat(GoalHistory): grouping the same history records
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Dec 14, 2023
1 parent 24b7a3c commit 76b39f8
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 106 deletions.
126 changes: 55 additions & 71 deletions src/components/GoalActivity.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import React, { forwardRef } from 'react';
import React, { forwardRef, useMemo } from 'react';
import { nullable } from '@taskany/bricks';

import { GoalByIdReturnType } from '../../trpc/inferredTypes';
import { GoalComment } from '../types/comment';
import { safeUserData } from '../utils/getUserName';

import { ActivityFeed } from './ActivityFeed';
import {
HistoryRecord,
HistoryRecordDependency,
HistoryRecordTags,
HistoryRecordTextChange,
HistoryRecordEstimate,
HistoryRecordPriority,
HistoryRecordState,
HistoryRecordParticipant,
HistoryRecordProject,
HistoryRecordLongTextChange,
HistoryRecordCriteria,
HistoryRecordPartnerProject,
} from './HistoryRecord/HistoryRecord';
import { HistoryRecordGroup } from './HistoryRecord/HistoryRecord';

interface GoalActivityProps {
feed: NonNullable<GoalByIdReturnType>['_activityFeed'];
Expand All @@ -30,67 +18,63 @@ interface GoalActivityProps {

export const GoalActivity = forwardRef<HTMLDivElement, GoalActivityProps>(
({ feed, header, footer, renderCommentItem }, ref) => {
const unionFeed = useMemo(() => {
const res = [];
let tempRecords = [];

for (let i = 0; i < feed.length; i += 1) {
const current = feed[i];
const next = feed[i + 1];

if (current.type === 'history') {
if (current.type === next?.type && current.value.subject === next.value.subject) {
tempRecords.push(current.value);
} else {
res.push({
type: current.type,
value: tempRecords.concat([current.value]),
});

tempRecords = [];
}
} else {
// only comments
res.push(current);
}
}

return res;
}, [feed]);

return (
<ActivityFeed ref={ref}>
{header}

{feed.map((item) =>
nullable(item, ({ type, value }) => (
<React.Fragment key={value.id}>
{type === 'comment' && renderCommentItem(value)}

{type === 'history' && (
<HistoryRecord
author={value.activity.user}
id={value.id}
subject={value.subject}
action={value.action}
createdAt={value.createdAt}
>
{value.subject === 'dependencies' && (
<HistoryRecordDependency
issues={value.previousValue || value.nextValue}
strike={!!value.previousValue}
/>
)}
{value.subject === 'tags' && (
<HistoryRecordTags from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'description' && (
<HistoryRecordLongTextChange from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'title' && (
<HistoryRecordTextChange from={value.previousValue} to={value.nextValue} />
{unionFeed.map((item) =>
nullable(item, ({ type, value }) => {
if (type === 'history') {
return (
<HistoryRecordGroup
key={`${type}.${value.length}${value.at(-1)?.id ?? ''}`}
subject={value[value.length - 1].subject}
groupped={value.length > 1}
values={value.map(
({ id, action, subject, nextValue, previousValue, activity, createdAt }) => ({
author: safeUserData(activity),
from: previousValue,
to: nextValue,
action,
id,
subject,
createdAt,
}),
)}
{value.subject === 'estimate' && (
<HistoryRecordEstimate from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'priority' && (
<HistoryRecordPriority from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'state' && (
<HistoryRecordState from={value.previousValue} to={value.nextValue} />
)}
{(value.subject === 'participants' || value.subject === 'owner') && (
<HistoryRecordParticipant from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'project' && (
<HistoryRecordProject from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'partnerProject' && (
<HistoryRecordPartnerProject from={value.previousValue} to={value.nextValue} />
)}
{value.subject === 'criteria' && (
<HistoryRecordCriteria
from={value.previousValue}
to={value.nextValue}
action={value.action}
/>
)}
</HistoryRecord>
)}
</React.Fragment>
)),
/>
);
}

return <React.Fragment key={value.id}>{renderCommentItem(value)}</React.Fragment>;
}),
)}

{footer}
Expand Down
5 changes: 4 additions & 1 deletion src/components/HistoryRecord/HistoryRecord.i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"as criteria": "",
"goal complete": "completed goal",
"goal in progress": "returns goal to work",
"partner project": ""
"partner project": "",
"author and more made changes in": "{author} and {count} more made changes in {subject}",
"author and the other author made changes in": "{author} and {oneMoreAuthor} made changes in {subject}",
"author made changes in": "{author} made changes in {subject}"
}
5 changes: 4 additions & 1 deletion src/components/HistoryRecord/HistoryRecord.i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"as criteria": "как критерий",
"goal complete": "выполнил(а) цель",
"goal in progress": "вернул(а) цель в работу",
"partner project": "партнерский проект"
"partner project": "партнерский проект",
"author and more made changes in": "{author} и еще {count} внесли изменения в {subject}",
"author and the other author made changes in": "{author} и {oneMoreAuthor} внесли изменения в {subject}",
"author made changes in": "{author} внес(ла) изменения в {subject}"
}
Loading

0 comments on commit 76b39f8

Please sign in to comment.