From bbf3cf6b0e48f99b94c9a5bca341c6e51893b94d Mon Sep 17 00:00:00 2001 From: Konrad Jamrozik Date: Fri, 28 Jun 2024 21:06:24 -0700 Subject: [PATCH] refactor out factionsDataGridData.tsx --- .../FactionsDataGrid/FactionsDataGrid.tsx | 69 ++----------------- .../FactionsDataGrid/factionsDataGridData.tsx | 57 +++++++++++++++ 2 files changed, 62 insertions(+), 64 deletions(-) create mode 100644 web/src/components/FactionsDataGrid/factionsDataGridData.tsx diff --git a/web/src/components/FactionsDataGrid/FactionsDataGrid.tsx b/web/src/components/FactionsDataGrid/FactionsDataGrid.tsx index 392bab8a..d084117a 100644 --- a/web/src/components/FactionsDataGrid/FactionsDataGrid.tsx +++ b/web/src/components/FactionsDataGrid/FactionsDataGrid.tsx @@ -1,22 +1,12 @@ import { Box } from '@mui/material' -import { - DataGrid, - type GridColDef, - type GridRenderCellParams, -} from '@mui/x-data-grid' -import _ from 'lodash' -import type { Faction } from '../../lib/codesync/GameState' -import { factionPowerResolution } from '../../lib/codesync/ruleset' +import { DataGrid } from '@mui/x-data-grid' import { useGameSessionContext } from '../../lib/gameSession/GameSession' -import { - factionColors, - factionNameGridColDef, -} from '../../lib/rendering/renderFactions' +import { factionColors } from '../../lib/rendering/renderFactions' import { defaultComponentMinWidth, sxClassesFromColors, } from '../../lib/rendering/renderUtils' -import ManageFactionDialog from './ManageFactionDialog' +import { getColumns, getRows } from './factionsDataGridData' const boxHeight = 200 export function FactionsDataGrid(): React.JSX.Element { @@ -24,13 +14,6 @@ export function FactionsDataGrid(): React.JSX.Element { const gs = gameSession.getCurrentGameStateUnsafe() const factions = gs?.Factions ?? [] - const rows: FactionRow[] = _.map(factions, (faction: Faction) => ({ - id: faction.Id, - faction: faction.Name, - power: Math.floor(faction.Power / factionPowerResolution), - intel: faction.IntelInvested, - })) - return ( ) } - -export type FactionRow = { - readonly id: number - readonly faction: string - readonly power: number - readonly intel: number -} - -function columns(factions: Faction[]): GridColDef[] { - return [ - factionNameGridColDef, - { - field: 'power', - headerName: 'Power', - width: 110, - disableColumnMenu: true, - }, - { - field: 'intel', - headerName: 'Intel', - width: 80, - disableColumnMenu: true, - }, - { - field: 'deploy', - disableColumnMenu: true, - sortable: false, - headerName: '', - width: 90, - renderCell: ( - params: GridRenderCellParams, - ): React.JSX.Element => { - const row: FactionRow = params.row - - const faction: Faction = _.find(factions, { - Id: row.id, - })! - return - }, - }, - ] -} diff --git a/web/src/components/FactionsDataGrid/factionsDataGridData.tsx b/web/src/components/FactionsDataGrid/factionsDataGridData.tsx new file mode 100644 index 00000000..c900a70e --- /dev/null +++ b/web/src/components/FactionsDataGrid/factionsDataGridData.tsx @@ -0,0 +1,57 @@ +import type { GridColDef, GridRenderCellParams } from '@mui/x-data-grid' +import _ from 'lodash' +import type { Faction } from '../../lib/codesync/GameState' +import { factionPowerResolution } from '../../lib/codesync/ruleset' +import { factionNameGridColDef } from '../../lib/rendering/renderFactions' +import ManageFactionDialog from './ManageFactionDialog' + +export function getRows(factions: Faction[]): FactionRow[] { + return _.map(factions, (faction: Faction) => ({ + id: faction.Id, + faction: faction.Name, + power: Math.floor(faction.Power / factionPowerResolution), + intel: faction.IntelInvested, + })) +} + +export type FactionRow = { + readonly id: number + readonly faction: string + readonly power: number + readonly intel: number +} + +export function getColumns(factions: Faction[]): GridColDef[] { + return [ + factionNameGridColDef, + { + field: 'power', + headerName: 'Power', + width: 110, + disableColumnMenu: true, + }, + { + field: 'intel', + headerName: 'Intel', + width: 80, + disableColumnMenu: true, + }, + { + field: 'deploy', + disableColumnMenu: true, + sortable: false, + headerName: '', + width: 90, + renderCell: ( + params: GridRenderCellParams, + ): React.JSX.Element => { + const row: FactionRow = params.row + + const faction: Faction = _.find(factions, { + Id: row.id, + })! + return + }, + }, + ] +}