Skip to content

Commit

Permalink
refa composition table stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sebald committed Oct 30, 2023
1 parent abc441f commit 9c7714f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 114 deletions.
23 changes: 7 additions & 16 deletions app/(stats)/compositions/[[...range]]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import { fromDateRange } from '@/lib/utils/url.utils';
import { Caption, Inline, Title } from '@/ui';
import { Calendar, Rocket, Trophy } from '@/ui/icons';

import {
CompositionFilter,
CompositionFilterProvider,
} from '@/ui/stats/composition-stats';
import { StatsFilter } from '@/ui/stats/stats-filter';
import { StatsHint } from '@/ui/stats/stats-hint';
import { setup } from '@/lib/stats';
Expand Down Expand Up @@ -115,19 +111,14 @@ const CompositionsPage = async ({ params }: PageProps) => {
/>
<FactionFilter />
</Inline>
<CompositionFilterProvider>
<StatsFilter smallSamples={false} dateRange={toDate(from, to)}>
<CompositionFilter />
</StatsFilter>
<div className="grid grid-cols-1 gap-4 md:grid-cols-12">
<div className="col-span-full">
<Compositions data={stats.composition} />
</div>
<div className="col-span-full pt-8 lg:col-start-2 lg:col-end-12">
<StatsHint />
</div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-12">
<div className="col-span-full">
<Compositions data={stats.composition} />
</div>
</CompositionFilterProvider>
<div className="col-span-full pt-8 lg:col-start-2 lg:col-end-12">
<StatsHint />
</div>
</div>
</>
);
};
Expand Down
28 changes: 0 additions & 28 deletions ui/stats/composition-stats/composition-filter.tsx

This file was deleted.

42 changes: 33 additions & 9 deletions ui/stats/composition-stats/composition-stats.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use client';

import { Card } from '@/ui';
import { useState } from 'react';
import { Card, FactionSelection, Select } from '@/ui';

import { CompositionTable } from './composition-table';
import { CompositionFilterProvider, useCompositionFilter } from './context';
import type { SortOptions } from './composition-table';
import type { CompositionStatsType } from './types';
import { CompositionFilter } from './composition-filter';
import { XWSFaction } from '@/lib/types';

// Props
// ---------------
Expand All @@ -15,18 +16,41 @@ export interface CompositionStatsProps {

// Component
// ---------------
export const CompositionStats = ({ value }: CompositionStatsProps) => (
<CompositionFilterProvider>
export const CompositionStats = ({ value }: CompositionStatsProps) => {
const [faction, setFaction] = useState<XWSFaction | 'all'>('all');
const [sortBy, setSortBy] = useState<SortOptions>('percentile');

return (
<Card>
<Card.Header>
<Card.Title>Compositions</Card.Title>
<Card.Actions>
<CompositionFilter />
<FactionSelection value={faction} onChange={setFaction} allowAll />
<Select
size="small"
value={sortBy}
onChange={e => setSortBy(e.target.value as any)}
>
<Select.Option value="percentile">By Percentile</Select.Option>
<Select.Option value="deviation">By Std. Deviation</Select.Option>
<Select.Option value="winrate">By Winrate</Select.Option>
<Select.Option value="frequency">By Frequency</Select.Option>
<Select.Option value="count">By Count</Select.Option>
<Select.Option value="score">By Score</Select.Option>
</Select>
</Card.Actions>
</Card.Header>
<Card.Body>
<CompositionTable value={value} />
<CompositionTable
value={value}
filter={
faction !== 'all'
? ([, stat]) => stat.faction === faction
: undefined
}
sortBy={sortBy}
/>
</Card.Body>
</Card>
</CompositionFilterProvider>
);
);
};
26 changes: 16 additions & 10 deletions ui/stats/composition-stats/composition-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ import { toPercentage } from '@/lib/utils';
import { Collapsible, FactionIcon, Link, ShipIcon, Table } from '@/ui';
import { Folder } from '@/ui/icons';

import { useCompositionFilter } from './context';
import type { CompositionStatsType } from './types';

// Types
// ---------------
export type SortOptions =
| 'percentile'
| 'deviation'
| 'winrate'
| 'frequency'
| 'count'
| 'score';

// Props
// ---------------
export interface CompositionTableProps {
value: { [id: string]: CompositionStatsType };
collapsible?: boolean;
filter?: (entry: [string, CompositionStatsType]) => boolean;
sortBy?: SortOptions;
}

// Components
Expand All @@ -23,25 +33,21 @@ export const CompositionTable = ({
value,
collapsible = true,
filter,
sortBy = 'percentile',
}: CompositionTableProps) => {
// TODO: move these out of the table, just a "filter" and "sort" function?
const { faction = 'all', sort = 'percentile' } = useCompositionFilter();
let data = Object.entries(value);

if (faction !== 'all' || filter) {
data = data.filter(entry => {
let result = faction === 'all' || entry[1].faction === faction;
return result && filter ? filter(entry) : result;
});
if (filter) {
data = data.filter(filter);
}

data.sort(([, a], [, b]) => {
const result = (b[sort] || 0) - (a[sort] || 0);
const result = (b[sortBy] || 0) - (a[sortBy] || 0);

// Secondary sort by percentile (or deviation if sorted by percentile already)
return result !== 0
? result
: sort === 'percentile'
: sortBy === 'percentile'
? b.deviation - a.deviation
: b.percentile - a.percentile;
});
Expand Down
49 changes: 0 additions & 49 deletions ui/stats/composition-stats/context.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions ui/stats/composition-stats/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './composition-filter';
export * from './composition-stats';
export * from './composition-table';
export * from './context';
export * from './types';

0 comments on commit 9c7714f

Please sign in to comment.