-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #318 from dataforgoodfr/chore/process-todo-in-code
Chore/process todo in code
- Loading branch information
Showing
38 changed files
with
599 additions
and
648 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ages/client/src/modules/charts/EnergyBalanceCharts/EnergyBalanceDetailsForPlayerGraph.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useMemo } from "react"; | ||
import { Persona } from "../../persona/persona"; | ||
import { useTranslation } from "../../translations/useTranslation"; | ||
import { | ||
DetailsEnergyConsumptionBars, | ||
DetailsEnergyProductionBars, | ||
} from "../DetailsEnergyBars"; | ||
import { | ||
EnergyConsumptionButtons, | ||
EnergyProductionButtons, | ||
} from "../../common/components/EnergyButtons"; | ||
import { StepDatum } from "./types"; | ||
import { getStackName } from "./utils"; | ||
|
||
export { EnergyBalanceDetailsForPlayerGraph }; | ||
|
||
function EnergyBalanceDetailsForPlayerGraph({ | ||
stepDatum, | ||
getPersonaAtStep, | ||
}: { | ||
stepDatum: StepDatum; | ||
getPersonaAtStep: (step: number) => Persona; | ||
}) { | ||
const { t } = useTranslation(); | ||
|
||
const DetailsContent = useMemo(() => { | ||
const persona = getPersonaAtStep(stepDatum.step); | ||
|
||
const graphTitle = t( | ||
"page.player.statistics.tabs.energy-balance.graphs.details.title", | ||
{ stackName: getStackName({ stepDatum, t }) } | ||
); | ||
|
||
if (stepDatum.type === "consumption") { | ||
return ( | ||
<> | ||
<DetailsEnergyConsumptionBars title={graphTitle} persona={persona} /> | ||
<EnergyConsumptionButtons persona={persona} /> | ||
</> | ||
); | ||
} else if (stepDatum.type === "production") { | ||
return ( | ||
<> | ||
<DetailsEnergyProductionBars title={graphTitle} persona={persona} /> | ||
<EnergyProductionButtons persona={persona} /> | ||
</> | ||
); | ||
} | ||
}, [stepDatum, getPersonaAtStep, t]); | ||
|
||
return <>{DetailsContent}</>; | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/client/src/modules/charts/EnergyBalanceCharts/EnergyBalanceForPlayerChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Box } from "@mui/material"; | ||
import _ from "lodash"; | ||
import React, { useMemo, useState } from "react"; | ||
import { | ||
StackedBars, | ||
StackedBarsStackData, | ||
StackedBarsStacks, | ||
} from "../StackedBars"; | ||
import { Persona } from "../../persona/persona"; | ||
import { STEPS, getStepTypes } from "../../play"; | ||
import { formatProduction } from "../../../lib/formatter"; | ||
import { usePlay } from "../../play/context/playContext"; | ||
import { useTranslation } from "../../translations/useTranslation"; | ||
import { StepDatum } from "./types"; | ||
import { | ||
computeConsumptionBarsForPersona, | ||
computeProductionBarsForPersona, | ||
getStackName, | ||
} from "./utils"; | ||
import { EnergyBalanceDetailsForPlayerGraph } from "./EnergyBalanceDetailsForPlayerGraph"; | ||
import { buildStack } from "../utils"; | ||
|
||
export { EnergyBalanceForPlayerChart }; | ||
|
||
function EnergyBalanceForPlayerChart({ | ||
getPersonaAtStep, | ||
}: { | ||
getPersonaAtStep: (step: number) => Persona; | ||
}) { | ||
const { t } = useTranslation(); | ||
const { game } = usePlay(); | ||
const [selectedStepDatum, setSelectedStepDatum] = useState<StepDatum | null>( | ||
null | ||
); | ||
|
||
const stepDataToDisplay = useMemo((): StepDatum[] => { | ||
const maxStep = STEPS.findIndex((s) => s.id === "final-situation"); | ||
const endStep = Math.min(game.lastFinishedStep + 1, maxStep); | ||
|
||
return _.range(0, endStep) | ||
.map(getStepTypes) | ||
.map((stepTypes, idx) => | ||
stepTypes.map((stepType) => ({ step: idx, type: stepType })) | ||
) | ||
.flat(); | ||
}, [game.lastFinishedStep]); | ||
|
||
const MainGraph = useMemo(() => { | ||
const data = stepDataToDisplay.map((stepDatum): StackedBarsStackData => { | ||
const computeBars = | ||
stepDatum.type === "consumption" | ||
? computeConsumptionBarsForPersona | ||
: computeProductionBarsForPersona; | ||
|
||
return buildStack({ | ||
bars: computeBars({ | ||
persona: getPersonaAtStep(stepDatum.step), | ||
t, | ||
}), | ||
label: getStackName({ stepDatum, t }), | ||
}); | ||
}); | ||
|
||
const stacks: StackedBarsStacks = { | ||
data, | ||
yAxisUnitLabel: t("unit.watthour-per-day-bare.kilo"), | ||
palettes: ["energy", "production"], | ||
yAxisValueFormatter: (value) => | ||
formatProduction(value, { fractionDigits: 2 }), | ||
yAxisTicksValueFormatter: (value) => | ||
formatProduction(value, { fractionDigits: 0 }), | ||
}; | ||
|
||
return ( | ||
<StackedBars | ||
title={t("graph.energy-balance-for-player-graph.title")} | ||
stacks={stacks} | ||
onClick={(chartState) => { | ||
if (chartState?.activeTooltipIndex != null) { | ||
const stepDatum = stepDataToDisplay[chartState.activeTooltipIndex]; | ||
setSelectedStepDatum(stepDatum); | ||
} | ||
}} | ||
/> | ||
); | ||
}, [stepDataToDisplay, getPersonaAtStep, t]); | ||
|
||
return ( | ||
<Box> | ||
{MainGraph} | ||
{selectedStepDatum && ( | ||
<EnergyBalanceDetailsForPlayerGraph | ||
stepDatum={selectedStepDatum} | ||
getPersonaAtStep={getPersonaAtStep} | ||
/> | ||
)} | ||
</Box> | ||
); | ||
} |
75 changes: 75 additions & 0 deletions
75
packages/client/src/modules/charts/EnergyBalanceCharts/EnergyBalanceForTeamChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Box } from "@mui/material"; | ||
import React, { useMemo } from "react"; | ||
import { | ||
StackedBars, | ||
StackedBarsStackData, | ||
StackedBarsStacks, | ||
} from "../StackedBars"; | ||
import { formatProduction, formatUserName } from "../../../lib/formatter"; | ||
import { usePersonaByUserId, usePlay } from "../../play/context/playContext"; | ||
import { useTranslation } from "../../translations/useTranslation"; | ||
import { | ||
computeConsumptionBarsForPersona, | ||
computeProductionBarsForPersona, | ||
} from "./utils"; | ||
import { ITeam } from "../../../utils/types"; | ||
import { buildStack } from "../utils"; | ||
|
||
export { EnergyBalanceForTeamChart }; | ||
|
||
function EnergyBalanceForTeamChart({ team }: { team: ITeam }) { | ||
const { t } = useTranslation(); | ||
const { players } = usePlay(); | ||
|
||
const playersInTeam = useMemo( | ||
() => players.filter((p) => p.teamId === team.id), | ||
[players, team] | ||
); | ||
const userIds = useMemo( | ||
() => playersInTeam.map((p) => p.userId), | ||
[playersInTeam] | ||
); | ||
const personaByUserId = usePersonaByUserId(userIds); | ||
|
||
const Graph = useMemo(() => { | ||
const consumptionStacks: StackedBarsStackData[] = playersInTeam.map( | ||
(player) => | ||
buildStack({ | ||
bars: computeConsumptionBarsForPersona({ | ||
persona: personaByUserId[player.userId].currentPersona, | ||
t, | ||
}), | ||
label: formatUserName(player.user), | ||
}) | ||
); | ||
|
||
const productionStack: StackedBarsStackData = buildStack({ | ||
bars: computeProductionBarsForPersona({ | ||
persona: personaByUserId[playersInTeam[0].userId].currentPersona, | ||
t, | ||
}), | ||
label: t("graph.common.production"), | ||
}); | ||
|
||
const data = [...consumptionStacks, productionStack]; | ||
|
||
const stacks: StackedBarsStacks = { | ||
data, | ||
yAxisUnitLabel: t("unit.watthour-per-day-bare.kilo"), | ||
palettes: ["energy", "production"], | ||
yAxisValueFormatter: (value) => | ||
formatProduction(value, { fractionDigits: 2 }), | ||
yAxisTicksValueFormatter: (value) => | ||
formatProduction(value, { fractionDigits: 0 }), | ||
}; | ||
|
||
return ( | ||
<StackedBars | ||
title={t("graph.energy-balance-for-team-graph.title")} | ||
stacks={stacks} | ||
/> | ||
); | ||
}, [personaByUserId, playersInTeam, t]); | ||
|
||
return <Box>{Graph}</Box>; | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/client/src/modules/charts/EnergyBalanceCharts/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { EnergyBalanceForPlayerChart } from "./EnergyBalanceForPlayerChart"; | ||
export { EnergyBalanceForTeamChart } from "./EnergyBalanceForTeamChart"; |
5 changes: 5 additions & 0 deletions
5
packages/client/src/modules/charts/EnergyBalanceCharts/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { GameStepType } from "../../play"; | ||
|
||
export type { StepDatum }; | ||
|
||
type StepDatum = { step: number; type: GameStepType }; |
76 changes: 76 additions & 0 deletions
76
packages/client/src/modules/charts/EnergyBalanceCharts/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { StackedBarsBar } from "../StackedBars"; | ||
import { Persona } from "../../persona/persona"; | ||
import { sumReducer } from "../../../lib/array"; | ||
import { filterOutDuplicates } from "../../common/utils"; | ||
import { I18nTranslateFunction } from "../../translations"; | ||
import { StepDatum } from "./types"; | ||
|
||
export { | ||
computeConsumptionBarsForPersona, | ||
computeProductionBarsForPersona, | ||
getStackName, | ||
}; | ||
|
||
function computeConsumptionBarsForPersona({ | ||
persona, | ||
t, | ||
}: { | ||
persona: Persona; | ||
t: I18nTranslateFunction; | ||
}): StackedBarsBar[] { | ||
const consumptionTypes = persona.consumption | ||
.map((consumption) => consumption.type) | ||
.filter(filterOutDuplicates) | ||
.sort() | ||
.reverse(); | ||
|
||
const bars: StackedBarsBar[] = consumptionTypes.map((type) => ({ | ||
key: type, | ||
label: t(`energy.${type}`), | ||
total: persona.consumption | ||
.filter((datum) => datum.type === type) | ||
.map((datum) => datum.value) | ||
.reduce(sumReducer, 0), | ||
})); | ||
|
||
return bars; | ||
} | ||
|
||
function computeProductionBarsForPersona({ | ||
persona, | ||
t, | ||
}: { | ||
persona: Persona; | ||
t: I18nTranslateFunction; | ||
}): StackedBarsBar[] { | ||
const productionTypes = persona.productionDisplayed | ||
.map((production) => production.type) | ||
.filter(filterOutDuplicates) | ||
.sort() | ||
.reverse(); | ||
|
||
const bars: StackedBarsBar[] = productionTypes.map((type) => ({ | ||
key: type, | ||
label: t(`graph.energy.${type}`), | ||
total: persona.productionDisplayed | ||
.filter((datum) => datum.type === type) | ||
.map((datum) => datum.value) | ||
.reduce(sumReducer, 0), | ||
})); | ||
|
||
return bars; | ||
} | ||
|
||
function getStackName({ | ||
stepDatum, | ||
t, | ||
}: { | ||
stepDatum: StepDatum; | ||
t: I18nTranslateFunction; | ||
}) { | ||
return stepDatum.step === 0 | ||
? stepDatum.type === "consumption" | ||
? t("graph.step.first.consumption.name") | ||
: t("graph.step.first.production.name") | ||
: t("graph.step.other.name", { stepNumber: stepDatum.step }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.