Skip to content

Commit

Permalink
Improve stats view
Browse files Browse the repository at this point in the history
  • Loading branch information
ubbn committed Dec 25, 2023
1 parent 7327ab5 commit eab7e33
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
3 changes: 1 addition & 2 deletions frontend/src/common/HeatMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ const Container = styled.div`
type Props = {
startDate: Date;
data: any[];
title: any;
tipText: any;
[_: string]: any;
};

const HeatMap = ({ startDate, data, title, tipText, ...props }: Props) => {
const HeatMap = ({ startDate, data, tipText, ...props }: Props) => {
return (
<Container>
<CalendarHeatmap
Expand Down
47 changes: 36 additions & 11 deletions frontend/src/components/stats/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const Stats = () => {
const [neuronsAdded, setNeuronsAdded] = useState<any[]>([]);
const [neuronsSolid, setNeuronsSolid] = useState<any[]>([]);
const [visibleAdded, setVisibleAdded] = useState<Neuron[]>([]);
const [visibleStudied, setVisibleStudied] = useState<string[]>([]);
const [addedDate, setAddedDate] = useState<string>("");
const [studiedDate, setStudiedDate] = useState<string>("");
const [activeChip, setActiveChip] = useState<number | undefined>(0);
const [startDate, setStartDate] = useState<Date>(new Date("2023-01-01"));
const [endDate, setEndDate] = useState<Date>(new Date("2023-12-31"));
Expand All @@ -65,14 +67,28 @@ const Stats = () => {
const result = chips[chipIndex].func();
setStartDate(result.startDate);
setEndDate(result.endDate);
setAddedDate("");
setStudiedDate("");
};

const onClickAdded = (param: any) => {
if (param?.date) {
const clicked = param.date.replaceAll("/", ""); // yyyy/MM/dd => yyyyMMdd
setVisibleAdded(items.filter((v) => `${v.created}`?.startsWith(clicked)));
const clicked = param.date.replaceAll("-", ""); // yyyy/MM/dd => yyyyMMdd
setVisibleAdded(
items.filter((v: any) => `${v.created}`?.startsWith(clicked))
);
}
setAddedDate(param.date);
setStudiedDate("");
};

const onClickStudied = (param: any) => {
const found = neuronsSolid.find((v: any) => v.date === param?.date);
if (found) {
setVisibleStudied(found.words);
}
setStudiedDate(param.date);
setAddedDate("");
};

return (
Expand Down Expand Up @@ -105,8 +121,6 @@ const Stats = () => {
<HeatMap
data={neuronsAdded}
classForValue={greenScalClasses}
title={"Days you added new neurons"}
mapper={(v: any) => ({ date: v.date, count: v.minutes })}
tipText={"word(s)"}
startDate={startDate}
endDate={endDate}
Expand All @@ -116,22 +130,33 @@ const Stats = () => {
<HeatMap
data={neuronsSolid}
classForValue={greenScalClasses}
title={"Days you added new neurons"}
mapper={(v: any) => ({ date: v.date, count: v.minutes })}
tipText={"word(s)"}
startDate={startDate}
endDate={endDate}
onClick={onClickStudied}
/>
{addedDate && (
<div>
<strong>On {addedDate}, you added:</strong>
<div>
{visibleAdded.map((v) => (
<div key={v.id}>
<ul>
{visibleAdded.map((v, i) => (
<li key={i}>
{v.title} - {v.detail}
</div>
</li>
))}
{visibleAdded.length === 0 && <li>Nothing ... 😢</li>}
</ul>
</div>
)}
{studiedDate && (
<div>
<strong>On {studiedDate}, you studied:</strong>
<ul>
{visibleStudied.map((v, i) => (
<li key={i}>{v}</li>
))}
</div>
{visibleStudied.length === 0 && <li>Nothing ... 😭</li>}
</ul>
</div>
)}
</div>
Expand Down
20 changes: 10 additions & 10 deletions frontend/src/components/stats/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getNewNeurons = (
do {
const tempStr = format(temp, "yyyyMMdd");
const found = data.filter((v) => `${v.created}`?.startsWith(tempStr));
prepared.push({ date: format(temp, "yyyy/MM/dd"), count: found.length });
prepared.push({ date: format(temp, "yyyy-MM-dd"), count: found.length });
temp = addDays(temp, 1);
} while (isBefore(temp, endDateOffset));
return prepared;
Expand All @@ -53,7 +53,7 @@ export const getSolidNeurons = (
Object.keys(curr.memo).forEach((day) => {
const studiedDate = addDays(created, +day);
if (isBefore(studiedDate, endDate) && isAfter(studiedDate, startDate)) {
const studied = format(studiedDate, "yyyy/MM/dd");
const studied = format(studiedDate, "yyyy-MM-dd");
if (acc[studied]) {
acc[studied].push(curr.title);
} else {
Expand All @@ -64,18 +64,18 @@ export const getSolidNeurons = (
return acc;
}, {});

let date = startDate;
let tmp = startDate;
const endDateOffset = addDays(endDate, 1);
const result = [];
do {
const tempStr = format(date, "yyyy/MM/dd");
let count = 0;
if (cumulatedSolid[tempStr]) {
count = cumulatedSolid[tempStr].length;
const date = format(tmp, "yyyy-MM-dd");
let words = [];
if (cumulatedSolid[date]) {
words = cumulatedSolid[date];
}
result.push({ date, count });
date = addDays(date, 1);
} while (isBefore(date, endDateOffset));
result.push({ date, words, count: words.length });
tmp = addDays(tmp, 1);
} while (isBefore(tmp, endDateOffset));
return result;
};

Expand Down

0 comments on commit eab7e33

Please sign in to comment.