Skip to content

Commit

Permalink
Merge pull request #10807 from DestinyItemManager/stat-tracker-crash
Browse files Browse the repository at this point in the history
Prevent Guardian Games stat trackers from crashing (parts of) the app
  • Loading branch information
robojumper authored Nov 20, 2024
2 parents 0ae554d + b1af6d9 commit aca0ad6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
6 changes: 2 additions & 4 deletions src/app/records/MetricBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { useD2Definitions } from 'app/manifest/selectors';
import { METRICS_ACCOUNT_NODE } from 'app/search/d2-known-values';
import { DestinyObjectiveProgress } from 'bungie-api-ts/destiny2';
import clsx from 'clsx';
import { TraitHashes } from 'data/d2/generated-enums';
import masterworkOverlay from 'images/masterwork-metric.png';
import BungieImage from '../dim-ui/BungieImage';
import styles from './MetricBanner.m.scss';
import { getMetricTimeScope } from './presentation-nodes';

interface Props {
metricHash: number;
Expand All @@ -21,9 +21,7 @@ export default function MetricBanner({ metricHash, objectiveProgress, className
}
const metricIcon = metricDef.displayProperties.icon;

const metricScope = metricDef.traitHashes
.filter((h) => h !== TraitHashes.All)
.map((h) => defs.Trait.get(h))[0];
const metricScope = getMetricTimeScope(defs, metricDef);

const parentNodeHash = metricDef.parentNodeHashes.length
? metricDef.parentNodeHashes[0]
Expand Down
22 changes: 6 additions & 16 deletions src/app/records/Metrics.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import BungieImage from 'app/dim-ui/BungieImage';
import { useD2Definitions } from 'app/manifest/selectors';
import { TraitHashes } from 'data/d2/generated-enums';
import { keyBy } from 'es-toolkit';
import Metric from './Metric';
import styles from './Metrics.m.scss';
import { DimMetric } from './presentation-nodes';
import { DimMetric, getMetricTimeScope } from './presentation-nodes';

export default function Metrics({ metrics }: { metrics: DimMetric[] }) {
const defs = useD2Definitions()!;
const groupedMetrics = Object.groupBy(
metrics,
(metric) => metric.metricDef.traitHashes.find((h) => h !== TraitHashes.All)!,
);

const traits = keyBy(
Object.keys(groupedMetrics).map((th) => defs.Trait.get(Number(th))),
(t) => t.hash,
);
const groupedMetrics = Map.groupBy(metrics, (m) => getMetricTimeScope(defs, m.metricDef));

return (
<div className={styles.metrics}>
{Object.entries(groupedMetrics).map(([traitHash, metrics]) => (
<div key={traitHash}>
{[...groupedMetrics.entries()].map(([trait, metrics]) => (
<div key={trait.hash}>
<div className={styles.title}>
<BungieImage src={traits[Number(traitHash)].displayProperties.icon} />
{traits[Number(traitHash)].displayProperties.name}
<BungieImage src={trait.displayProperties.icon} />
{trait.displayProperties.name}
</div>
{metrics.map((metric) => (
<Metric key={metric.metricDef.hash} metric={metric} />
Expand Down
10 changes: 10 additions & 0 deletions src/app/records/presentation-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
DestinyRecordDefinition,
DestinyRecordState,
DestinyScope,
DestinyTraitDefinition,
} from 'bungie-api-ts/destiny2';
import { TraitHashes } from 'data/d2/generated-enums';
import { minBy } from 'es-toolkit';
import { unlockedItemsForCharacterOrProfilePlugSet } from './plugset-helpers';

Expand Down Expand Up @@ -609,6 +611,14 @@ export function getCollectibleState(
: profileResponse.profileCollectibles?.data?.collectibles[collectibleDef.hash]?.state;
}

export function getMetricTimeScope(
defs: D2ManifestDefinitions,
metric: DestinyMetricDefinition,
): DestinyTraitDefinition {
const traitHash = metric.traitHashes.find((h) => h !== TraitHashes.All);
return defs.Trait.get(traitHash ?? TraitHashes.All);
}

function getMetricComponent(
metricDef: DestinyMetricDefinition,
profileResponse: DestinyProfileResponse,
Expand Down

0 comments on commit aca0ad6

Please sign in to comment.