Skip to content

Commit

Permalink
Fix activity sorting (l2beat#5823)
Browse files Browse the repository at this point in the history
  • Loading branch information
torztomasz authored Nov 13, 2024
1 parent 5a96a2a commit 28b4f9e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@tanstack/react-table'
import { BasicTable } from '~/components/table/basic-table'
import { RollupsTable } from '~/components/table/rollups-table'
import { env } from '~/env'
import { useTable } from '~/hooks/use-table'
import { type ScalingActivityEntry } from '~/server/features/scaling/activity/get-scaling-activity-entries'
import { getScalingActivityColumns } from './columns'
Expand All @@ -27,7 +28,8 @@ export function ScalingActivityTable({
}: Props) {
const table = useTable({
columns: getScalingActivityColumns({
activity: !!customSortedRowModel,
activity:
!!customSortedRowModel && env.NEXT_PUBLIC_FEATURE_FLAG_STAGE_SORTING,
}),
data: entries,
getCoreRowModel: getCoreRowModel(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
} from '../../projects-change-report/get-projects-change-report'
import { getProjectsVerificationStatuses } from '../../verification-status/get-projects-verification-statuses'
import { getCommonScalingEntry } from '../get-common-scaling-entry'
import { orderByStageAndPastDayTps } from '../utils/order-by-stage-and-past-day-tps'
import {
orderByStageAndPastDayTps,
sortByTps,
} from '../utils/order-by-stage-and-past-day-tps'
import {
type ActivityProjectTableData,
getActivityTableData,
Expand Down Expand Up @@ -48,17 +51,38 @@ export async function getScalingActivityEntries() {
.sort((a, b) => b.data.pastDayTps - a.data.pastDayTps)

if (env.NEXT_PUBLIC_FEATURE_FLAG_RECATEGORISATION) {
const recategorisedEntries = groupByMainCategories(
const categorisedEntries = groupByMainCategories(
orderByStageAndPastDayTps(entries),
)
if (!env.NEXT_PUBLIC_FEATURE_FLAG_STAGE_SORTING) {
return {
type: 'recategorised' as const,
entries: {
rollups: [ethereumEntry, ...categorisedEntries.rollups].sort(
sortByTps,
),
validiumsAndOptimiums: [
ethereumEntry,
...categorisedEntries.validiumsAndOptimiums,
].sort(sortByTps),
others: categorisedEntries.others
? [ethereumEntry, ...categorisedEntries.others].sort(sortByTps)
: undefined,
},
}
}

return {
type: 'recategorised' as const,
entries: {
rollups: [ethereumEntry, ...recategorisedEntries.rollups],
rollups: [ethereumEntry, ...categorisedEntries.rollups],
validiumsAndOptimiums: [
ethereumEntry,
...recategorisedEntries.validiumsAndOptimiums,
...categorisedEntries.validiumsAndOptimiums,
],
others: categorisedEntries.others
? [ethereumEntry, ...categorisedEntries.others]
: undefined,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,55 @@
import { type StageConfig } from '@l2beat/config'
import { type ProjectId } from '@l2beat/shared-pure'
import { env } from '~/env'

export function orderByStageAndPastDayTps<
T extends {
id: ProjectId
name: string
stage: StageConfig
data: {
pastDayTps: number
}
},
>(projects: T[]): T[] {
const sortByStageAndTps = (a: T, b: T) => {
const stageA = a.stage.stage
const stageB = b.stage.stage

if (stageA === 'Stage 2' || stageA === 'Stage 1') {
if (stageB !== 'Stage 2' && stageB !== 'Stage 1') return -1
} else if (stageB === 'Stage 2' || stageB === 'Stage 1') {
return 1
} else if (stageA === 'Stage 0') {
if (stageB !== 'Stage 0') return -1
} else if (stageB === 'Stage 0') {
return 1
}

const aTps = a.data.pastDayTps
const bTps = b.data.pastDayTps
if (aTps !== bTps) {
return bTps - aTps
}

return a.name.localeCompare(b.name)
interface BaseProject {
id: ProjectId
name: string
stage: StageConfig
data: {
pastDayTps: number
}
}

export function orderByStageAndPastDayTps<T extends BaseProject>(
projects: T[],
): T[] {
if (!env.NEXT_PUBLIC_FEATURE_FLAG_STAGE_SORTING) {
return projects.sort(sortByTps)
}

return projects.sort(sortByStageAndTps)
}

const sortByStageAndTps = <T extends BaseProject>(a: T, b: T) => {
const stageA = a.stage.stage
const stageB = b.stage.stage

if (stageA === 'Stage 2' || stageA === 'Stage 1') {
if (stageB !== 'Stage 2' && stageB !== 'Stage 1') return -1
} else if (stageB === 'Stage 2' || stageB === 'Stage 1') {
return 1
} else if (stageA === 'Stage 0') {
if (stageB !== 'Stage 0') return -1
} else if (stageB === 'Stage 0') {
return 1
}

const aTps = a.data.pastDayTps
const bTps = b.data.pastDayTps
if (aTps !== bTps) {
return bTps - aTps
}

return a.name.localeCompare(b.name)
}

export const sortByTps = <T extends BaseProject>(a: T, b: T) => {
const aTps = a.data.pastDayTps
const bTps = b.data.pastDayTps
if (aTps !== bTps) {
return bTps - aTps
}

return a.name.localeCompare(b.name)
}

0 comments on commit 28b4f9e

Please sign in to comment.