Skip to content

Commit

Permalink
added duration filter for when last split run time is bugged (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
therungg authored Nov 19, 2023
1 parent 9f226e0 commit 7907b1e
Showing 1 changed file with 109 additions and 49 deletions.
158 changes: 109 additions & 49 deletions src/components/run/history/history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export const History = ({
) => {
if (newSplitFilter === "none") return;

if (newSplitFilter === "duration")
return updateSplitFiltersForDuration();

if (newSplitFilter === "full") newSplitFilter = splits.length - 1;

let min = 0;
Expand Down Expand Up @@ -182,6 +185,24 @@ export const History = ({
setDisplaySplitFilterTwo(getFormattedString(min.toString()));
};

const updateSplitFiltersForDuration = () => {
let min = 0;
let max = 100000000000000000;
const relevantHistory = history.filter((h) => !!h.duration && !!h.time);

relevantHistory.forEach((h) => {
const useTime = parseInt(h.duration);
if (useTime > min) min = useTime;
if (useTime < max) max = useTime;
});

setSplitFilterOne(max);
setSplitFilterTwo(min);

setDisplaySplitFilterOne(getFormattedString(max.toString()));
setDisplaySplitFilterTwo(getFormattedString(min.toString()));
};

if (history.length > 0 && splitFilter !== "none" && splitFilterOne !== 0) {
let currentSplitFilter = splitFilter;
if (currentSplitFilter === "full")
Expand All @@ -190,24 +211,39 @@ export const History = ({
history = history.filter((run) => {
let include = false;

if (!run.splits[currentSplitFilter] && splitFilter != "full") {
if (
!run.splits[currentSplitFilter] &&
splitFilter !== "full" &&
splitFilter !== "duration"
) {
return false;
}

if (run.splits.length === 0) return false;

if (splitFilter === "full" && !run.time) {
if (
(splitFilter === "full" || splitFilter === "duration") &&
!run.time
) {
return false;
}

const situationalCurrentSplitFilter =
splitFilter === "full"
? run.splits.length - 1
: currentSplitFilter;

const time = totalTime
? run.splits[situationalCurrentSplitFilter as number].totalTime
: run.splits[situationalCurrentSplitFilter as number].splitTime;
let time = "";

if (splitFilter === "duration") {
time = run.duration;
} else {
const situationalCurrentSplitFilter =
splitFilter === "full"
? run.splits.length - 1
: currentSplitFilter;

time = totalTime
? run.splits[situationalCurrentSplitFilter as number]
.totalTime
: run.splits[situationalCurrentSplitFilter as number]
.splitTime;
}

if (!parseInt(time) && parseInt(time) == 0) return false;

Expand Down Expand Up @@ -250,22 +286,37 @@ export const History = ({
}

if (sortColumn == "split") {
const aTime = totalTime
? a.splits[
splitFilter === "full" ? a.splits.length - 1 : splitFilter
].totalTime
: a.splits[
splitFilter === "full" ? a.splits.length - 1 : splitFilter
].splitTime;
const bTime = totalTime
? b.splits[
splitFilter === "full" ? b.splits.length - 1 : splitFilter
].totalTime
: b.splits[
splitFilter === "full" ? b.splits.length - 1 : splitFilter
].splitTime;

res = parseInt(aTime) > parseInt(bTime) ? -1 : 1;
if (splitFilter === "duration") {
const aTime = a.duration;
const bTime = b.duration;

res = parseInt(aTime) > parseInt(bTime) ? -1 : 1;
} else {
const aTime = totalTime
? a.splits[
splitFilter === "full"
? a.splits.length - 1
: splitFilter
].totalTime
: a.splits[
splitFilter === "full"
? a.splits.length - 1
: splitFilter
].splitTime;
const bTime = totalTime
? b.splits[
splitFilter === "full"
? b.splits.length - 1
: splitFilter
].totalTime
: b.splits[
splitFilter === "full"
? b.splits.length - 1
: splitFilter
].splitTime;

res = parseInt(aTime) > parseInt(bTime) ? -1 : 1;
}
}

if (!sortAsc) res *= -1;
Expand Down Expand Up @@ -341,6 +392,8 @@ export const History = ({
splitName =
splitFilter === "full"
? "Full run time"
: splitFilter === "duration"
? "Run duration"
: splits[splitFilter as number].name;

filterTextSplitFilter = `${splitName} ${splitFilterType} `;
Expand Down Expand Up @@ -524,7 +577,9 @@ export const History = ({

if (
e.currentTarget.value ===
"full"
"full" ||
e.currentTarget.value ===
"duration"
) {
currentTotalTime = true;
setTotalTime(
Expand All @@ -548,6 +603,12 @@ export const History = ({
>
Full run
</option>
<option
key={"duration"}
value={"duration"}
>
Run Duration
</option>
<option disabled>
------------------------------------
</option>
Expand Down Expand Up @@ -957,6 +1018,26 @@ export const History = ({
.reverse()
.slice((active - 1) * 10, active * 10)
.map((run) => {
let filterString = "";
if (splitFilter === "duration") {
filterString = getFormattedString(run.duration);
} else if (splitName) {
filterString = getFormattedString(
totalTime
? run.splits[
splitFilter === "full"
? run.splits.length - 1
: splitFilter
].totalTime
: run.splits[
splitFilter === "full"
? run.splits.length - 1
: splitFilter
].splitTime,
true
);
}

return (
<Accordion.Item
key={
Expand Down Expand Up @@ -1022,28 +1103,7 @@ export const History = ({
styles.optionalColumn
}
>
{getFormattedString(
totalTime
? run.splits[
splitFilter ===
"full"
? run
.splits
.length -
1
: splitFilter
].totalTime
: run.splits[
splitFilter ===
"full"
? run
.splits
.length -
1
: splitFilter
].splitTime,
true
)}
{filterString}
</Col>
)}
</Row>
Expand Down

0 comments on commit 7907b1e

Please sign in to comment.