UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

48 lines (47 loc) 1.56 kB
import { useRef, useState } from "react"; import { useControlled, useEvent } from "@1771technologies/lytenyte-core/internal"; export function useSourceState(props) { const [rows, setRows] = useState(new Map()); const [maxDepth, setMaxDepth] = useState(0); const [isLoading, setIsLoading] = useState(false); const [loadingError, setLoadingError] = useState(null); const [requestsForView, setRequestsForView] = useState([]); const [idUniverse, setIdUniverse] = useState(new Set()); const [topCount, setTopCount] = useState(0); const [rowCount, setRowCount] = useState(0); const [botCount, setBotCount] = useState(0); const [expansions, setExpansions] = useControlled({ controlled: props.rowGroupExpansions, default: {}, }); const onExpansionsChange = useEvent((delta) => { setExpansions({ ...expansions, ...delta }); props.onRowGroupExpansionChange?.({ ...expansions, ...delta }); }); const state = { isLoading, setIsLoading, loadingError, setLoadingError, requestsForView, setRequestsForView, topCount, setTopCount, botCount, setBotCount, rowCount, setRowCount, expansions, onExpansionsChange, maxDepth, setMaxDepth, rows, setRows, idUniverse, setIdUniverse, }; // React is stupid sometimes. const stateRef = useRef(state); Object.assign(stateRef.current, state); return stateRef.current; }