Skip to content

Commit

Permalink
fix(GoalActivity): get last state comment
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Jul 23, 2024
1 parent 73848e1 commit 6d19c0a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 48 deletions.
7 changes: 3 additions & 4 deletions src/components/GoalActivityFeed/GoalActivityFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { AddInlineTrigger } from '../AddInlineTrigger/AddInlineTrigger';
import { GoalCriteriaSuggest } from '../GoalCriteriaSuggest';
import { GoalFormPopupTrigger } from '../GoalFormPopupTrigger/GoalFormPopupTrigger';
import { GoalActivityWithTabs } from '../GoalActivityWithTabs/GoalActivityWithTabs';
import { safeUserData } from '../../utils/getUserName';

import { tr } from './GoalActivityFeed.i18n';
import s from './GoalActivityFeed.module.css';
Expand Down Expand Up @@ -45,13 +46,11 @@ export const GoalActivityFeed = forwardRef<HTMLDivElement, GoalActivityFeedProps
onGoalCommentCreate,
onGoalCommentReactionToggle,
onGoalCommentDelete,
lastStateComment,
highlightCommentId,
} = useGoalResource(
{
id: goal?.id,
stateId: goal?.stateId,
comments: goal?._comments,
},
{
invalidate: {
Expand Down Expand Up @@ -183,11 +182,11 @@ export const GoalActivityFeed = forwardRef<HTMLDivElement, GoalActivityFeedProps
))}
</GoalCriteriaView>
))}
{nullable(lastStateComment, (value) => (
{nullable(goal._lastComment, (value) => (
<CommentView
pin
id={value.id}
author={value.author}
author={safeUserData(value.activity)}
description={value.description}
state={value.state ?? undefined}
createdAt={value.createdAt}
Expand Down
20 changes: 1 addition & 19 deletions src/hooks/useGoalResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from '../schema/criteria';
import { ModalEvent, dispatchModalEvent } from '../utils/dispatchModal';
import { TagObject } from '../types/tag';
import { safeUserData } from '../utils/getUserName';
import { GoalByIdReturnType } from '../../trpc/inferredTypes';

import { useHighlightedComment } from './useHighlightedComment';
Expand All @@ -33,7 +32,7 @@ interface Configuration {
interface GoalFields {
id?: string;
stateId?: string | null;
comments?: NonNullable<GoalByIdReturnType>['_comments'];
lastStateUpdateComment?: NonNullable<GoalByIdReturnType>['_lastComment'];
}

export const useGoalResource = (fields: GoalFields, config?: Configuration) => {
Expand Down Expand Up @@ -257,22 +256,6 @@ export const useGoalResource = (fields: GoalFields, config?: Configuration) => {
[deleteGoalComment, invalidate],
);

const lastStateComment = useMemo(() => {
if ((fields.comments?.length ?? 0) <= 1) {
return null;
}

const foundResult = fields.comments?.findLast((comment) => comment.stateId);
if (!foundResult) return;

return foundResult.stateId === fields.stateId
? {
...foundResult,
author: safeUserData(foundResult.activity),
}
: null;
}, [fields.comments, fields.stateId]);

const onGoalCriteriaAdd = useCallback(
async (val: AddCriteriaSchema, invalidateKey?: RefetchKeys | RefetchKeys[]) => {
await notifyPromise(addGoalCriteria.mutateAsync(val), 'criteriaCreate');
Expand Down Expand Up @@ -498,7 +481,6 @@ export const useGoalResource = (fields: GoalFields, config?: Configuration) => {

return {
highlightCommentId,
lastStateComment,

invalidate,

Expand Down
50 changes: 49 additions & 1 deletion src/utils/db/calculatedGoalsFields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Role } from '@prisma/client';
import { Activity, Reaction, Role, User, Ghost, Comment, State } from '@prisma/client';

import { calcAchievedWeight } from '../recalculateCriteriaScore';
import { ReactionsMap } from '../../types/reactions';
import { safeGetUserName } from '../getUserName';

interface UserActivity {
activity: Activity & { user: User; ghost: Ghost | null };
}

export const addCommonCalculatedGoalFields = (goal: any) => {
const _shortId = `${goal.projectId}-${goal.scopeId}`;
Expand All @@ -10,10 +16,52 @@ export const addCommonCalculatedGoalFields = (goal: any) => {
? calcAchievedWeight(goal.goalAchieveCriteria)
: goal.completedCriteriaWeight;

const lastCommentWithUpdateState: Comment &
UserActivity & { reactions: (Reaction & UserActivity)[]; state: State } = goal.comments?.[0];

let reactions: ReactionsMap = {};
if (lastCommentWithUpdateState) {
const limit = 10;
reactions = lastCommentWithUpdateState.reactions?.reduce<ReactionsMap>((acc, cur) => {
const data = {
activityId: cur.activityId,
name: safeGetUserName(cur.activity),
};

if (acc[cur.emoji]) {
acc[cur.emoji].count += 1;
acc[cur.emoji].authors.push(data);
} else {
acc[cur.emoji] = {
count: 1,
authors: [data],
remains: 0,
};
}

return acc;
}, {});

for (const key in reactions) {
if (key in reactions) {
const { authors } = reactions[key];

if (authors.length > limit) {
reactions[key].authors = authors.slice(0, limit);
reactions[key].remains = authors.length - limit;
}
}
}
}

return {
_shortId,
_hasAchievementCriteria,
_achivedCriteriaWeight,
_lastComment: {
...lastCommentWithUpdateState,
reactions,
},
};
};

Expand Down
24 changes: 0 additions & 24 deletions trpc/queries/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,30 +514,6 @@ export const getGoalDeepQuery = (user?: { activityId: string; role: Role }) => {
},
where: depsWhere,
},
comments: {
orderBy: {
createdAt: 'asc',
},
include: {
activity: {
include: {
user: true,
ghost: true,
},
},
state: true,
reactions: {
include: {
activity: {
include: {
user: true,
ghost: true,
},
},
},
},
},
},
reactions: {
include: {
activity: {
Expand Down
26 changes: 26 additions & 0 deletions trpc/router/goal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,32 @@ export const goal = router({
activityId,
role,
}),
comments: {
orderBy: { updatedAt: 'desc' },
take: 1,
where: {
stateId: { not: null },
},
include: {
activity: {
include: {
user: true,
ghost: true,
},
},
reactions: {
include: {
activity: {
include: {
user: true,
ghost: true,
},
},
},
},
state: true,
},
},
goalAchiveCriteria: {
include: {
criteriaGoal: {
Expand Down

0 comments on commit 6d19c0a

Please sign in to comment.