-
-
Notifications
You must be signed in to change notification settings - Fork 730
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
feat: activity widget #8628
feat: activity widget #8628
Changes from all commits
37365b0
61285fb
81a84c9
3a47428
5bbb152
7080ee3
73f950d
481d05f
a026c11
cbcbb2d
3415b53
1420436
1fa93c8
0efcf21
36a8f52
6e7e32d
731b009
e262721
0eaa359
a016154
b283173
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { useRequiredPathParam } from 'hooks/useRequiredPathParam'; | ||
import { useProjectStatus } from 'hooks/api/getters/useProjectStatus/useProjectStatus'; | ||
import ActivityCalendar, { type ThemeInput } from 'react-activity-calendar'; | ||
import type { ProjectActivitySchema } from '../../../../openapi'; | ||
import { styled, Tooltip } from '@mui/material'; | ||
|
||
const StyledContainer = styled('div')(({ theme }) => ({ | ||
gap: theme.spacing(1), | ||
})); | ||
|
||
type Output = { date: string; count: number; level: number }; | ||
|
||
export function transformData(inputData: ProjectActivitySchema): Output[] { | ||
const resultMap: Record<string, number> = {}; | ||
|
||
// Step 1: Count the occurrences of each date | ||
inputData.forEach((item) => { | ||
const formattedDate = new Date(item.date).toISOString().split('T')[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't they already formatted this way? But anyway, this is just an accumulation, right? Assuming we have multiple entries for each date, it'll count each entry as one and sum them together? Maybe I've misunderstood the API, but I thought we returned each date only once, with already summed up activities? In that case, this'd be wrong, right? |
||
resultMap[formattedDate] = (resultMap[formattedDate] || 0) + 1; | ||
}); | ||
|
||
// Step 2: Get all counts, sort them, and find the cut-off values for percentiles | ||
const counts = Object.values(resultMap).sort((a, b) => a - b); | ||
|
||
const percentile = (percent: number) => { | ||
const index = Math.floor((percent / 100) * counts.length); | ||
return counts[index] || counts[counts.length - 1]; | ||
}; | ||
|
||
const thresholds = [ | ||
percentile(25), // 25th percentile | ||
percentile(50), // 50th percentile | ||
percentile(75), // 75th percentile | ||
percentile(100), // 100th percentile | ||
]; | ||
|
||
// Step 3: Assign a level based on the percentile thresholds | ||
const calculateLevel = (count: number): number => { | ||
if (count <= thresholds[0]) return 1; // 1-25% | ||
if (count <= thresholds[1]) return 2; // 26-50% | ||
if (count <= thresholds[2]) return 3; // 51-75% | ||
return 4; // 76-100% | ||
}; | ||
|
||
// Step 4: Convert the map back to an array and assign levels | ||
return Object.entries(resultMap) | ||
.map(([date, count]) => ({ | ||
date, | ||
count, | ||
level: calculateLevel(count), | ||
})) | ||
.reverse(); // Optional: reverse the order if needed | ||
} | ||
|
||
export const ProjectActivity = () => { | ||
const projectId = useRequiredPathParam('projectId'); | ||
const { data } = useProjectStatus(projectId); | ||
|
||
const explicitTheme: ThemeInput = { | ||
light: ['#f1f0fc', '#ceccfd', '#8982ff', '#6c65e5', '#615bc2'], | ||
dark: ['#f1f0fc', '#ceccfd', '#8982ff', '#6c65e5', '#615bc2'], | ||
}; | ||
|
||
const levelledData = transformData(data.activityCountByDate); | ||
|
||
return ( | ||
<StyledContainer> | ||
{data.activityCountByDate.length > 0 ? ( | ||
<> | ||
<span>Activity in project</span> | ||
<ActivityCalendar | ||
theme={explicitTheme} | ||
data={levelledData} | ||
maxLevel={4} | ||
showWeekdayLabels={true} | ||
renderBlock={(block, activity) => ( | ||
<Tooltip | ||
title={`${activity.count} activities on ${activity.date}`} | ||
> | ||
{block} | ||
</Tooltip> | ||
)} | ||
/> | ||
</> | ||
) : ( | ||
<span>No activity</span> | ||
)} | ||
</StyledContainer> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { fetcher, useApiGetter } from '../useApiGetter/useApiGetter'; | ||
import type { ProjectStatusSchema } from '../../../../openapi'; | ||
import { formatApiPath } from 'utils/formatPath'; | ||
|
||
const path = (projectId: string) => `api/admin/projects/${projectId}/status`; | ||
|
||
const placeholderData: ProjectStatusSchema = { | ||
activityCountByDate: [], | ||
}; | ||
|
||
export const useProjectStatus = (projectId: string) => { | ||
const projectPath = formatApiPath(path(projectId)); | ||
const { data, refetch, loading, error } = useApiGetter<ProjectStatusSchema>( | ||
projectPath, | ||
() => fetcher(projectPath, 'Project Status'), | ||
); | ||
|
||
return { data: data || placeholderData, refetch, loading, error }; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be cleaned up later, but basically for each day we need a level(color), and this divides them evenly into level buckets.