Skip to content

Commit

Permalink
type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dtemkin1 committed Dec 16, 2024
1 parent 43732d5 commit 8c14e3a
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/components/ClassTable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AgGridReact } from "ag-grid-react";
import AgGrid, {
import {
ModuleRegistry,
AllCommunityModule,
themeQuartz,
type IRowNode,
type ColDef,
} from "ag-grid-community";
import { Box, Group, Flex, Image, Input } from "@chakra-ui/react";
import React, { useEffect, useMemo, useRef, useState } from "react";
Expand Down Expand Up @@ -41,7 +43,7 @@ type ClassTableRow = {
inCharge: string;
};

type ClassFilter = (cls: Class) => boolean;
type ClassFilter = (cls?: Class) => boolean;
/** Type of filter on class list; null if no filter. */
type SetClassFilter = React.Dispatch<React.SetStateAction<ClassFilter | null>>;

Expand Down Expand Up @@ -97,7 +99,7 @@ function ClassInput(props: {
row.inCharge.includes(simplifyInput),
);
const index = new Set(searchResults.current.map((cls) => cls.numbers[0]));
setInputFilter(() => (cls: Class) => index.has(cls.number));
setInputFilter(() => (cls?: Class) => index.has(cls?.number ?? ""));
} else {
setInputFilter(null);
}
Expand Down Expand Up @@ -212,7 +214,8 @@ function ClassFlags(props: {

// careful! we have to wrap it with a () => because otherwise React will
// think it's an updater function instead of the actual function.
setFlagsFilter(() => (cls: Class) => {
setFlagsFilter(() => (cls?: Class) => {
if (!cls) return false;
let result = true;
newFlags.forEach((value, flag) => {
if (value && flag === "fits" && !state.fitsSchedule(cls)) {
Expand Down Expand Up @@ -293,7 +296,7 @@ export function ClassTable(props: {
const gridRef = useRef<AgGridReact>(null);

// Setup table columns
const columnDefs = useMemo(() => {
const columnDefs: ColDef<ClassTableRow>[] = useMemo(() => {
const initialSort = "asc" as const;
const sortingOrder: Array<"asc" | "desc"> = ["asc", "desc"];
const sortProps = { sortable: true, unSortIcon: true, sortingOrder };
Expand All @@ -303,9 +306,10 @@ export function ClassTable(props: {
comparator: (
valueA: string,
valueB: string,
nodeA: AgGrid.RowNode,
nodeB: AgGrid.RowNode,
nodeA: IRowNode<ClassTableRow>,
nodeB: IRowNode<ClassTableRow>,
) => {
if (!nodeA.data || !nodeB.data) return 0;
const numberA = valueA === "N/A" ? Infinity : Number(valueA);
const numberB = valueB === "N/A" ? Infinity : Number(valueB);
return numberA !== numberB
Expand Down Expand Up @@ -351,9 +355,9 @@ export function ClassTable(props: {
const [flagsFilter, setFlagsFilter] = useState<ClassFilter | null>(null);

const doesExternalFilterPass = useMemo(() => {
return (node: AgGrid.RowNode) => {
if (inputFilter && !inputFilter(node.data.class)) return false;
if (flagsFilter && !flagsFilter(node.data.class)) return false;
return (node: IRowNode<ClassTableRow>) => {
if (inputFilter && !inputFilter(node.data?.class)) return false;
if (flagsFilter && !flagsFilter(node.data?.class)) return false;
return true;
};
}, [inputFilter, flagsFilter]);
Expand All @@ -376,7 +380,7 @@ export function ClassTable(props: {
updateFilter={() => gridRef.current?.api?.onFilterChanged()}
/>
<Box style={{ height: "320px", width: "100%" }}>
<AgGridReact
<AgGridReact<ClassTableRow>
theme={hydrantTheme}
ref={gridRef}
columnDefs={columnDefs}
Expand All @@ -385,8 +389,8 @@ export function ClassTable(props: {
enableCellTextSelection={true}
isExternalFilterPresent={() => true}
doesExternalFilterPass={doesExternalFilterPass}
onRowClicked={(e) => state.setViewedActivity(e.data.class)}
onRowDoubleClicked={(e) => state.toggleActivity(e.data.class)}
onRowClicked={(e) => state.setViewedActivity(e.data?.class)}
onRowDoubleClicked={(e) => state.toggleActivity(e.data?.class)}
onGridReady={() => gridRef.current?.api?.autoSizeAllColumns()}
// these have to be set here, not in css:
headerHeight={40}
Expand Down

0 comments on commit 8c14e3a

Please sign in to comment.