cogsbox-state 0.5.405 → 0.5.406
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CogsState.jsx +471 -411
- package/dist/CogsState.jsx.map +1 -1
- package/package.json +1 -1
- package/src/CogsState.tsx +103 -25
package/package.json
CHANGED
package/src/CogsState.tsx
CHANGED
|
@@ -1805,6 +1805,7 @@ function createProxyHandler<T>(
|
|
|
1805
1805
|
}
|
|
1806
1806
|
// Simplified useVirtualView approach
|
|
1807
1807
|
// Optimal approach - replace the useVirtualView implementation
|
|
1808
|
+
// Complete useVirtualView implementation with comprehensive logging
|
|
1808
1809
|
if (prop === "useVirtualView") {
|
|
1809
1810
|
return (
|
|
1810
1811
|
options: VirtualViewOptions
|
|
@@ -1816,6 +1817,12 @@ function createProxyHandler<T>(
|
|
|
1816
1817
|
dependencies = [],
|
|
1817
1818
|
} = options;
|
|
1818
1819
|
|
|
1820
|
+
console.log("useVirtualView initialized with:", {
|
|
1821
|
+
itemHeight,
|
|
1822
|
+
overscan,
|
|
1823
|
+
stickToBottom,
|
|
1824
|
+
});
|
|
1825
|
+
|
|
1819
1826
|
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
1820
1827
|
const [range, setRange] = useState({
|
|
1821
1828
|
startIndex: 0,
|
|
@@ -1830,9 +1837,11 @@ function createProxyHandler<T>(
|
|
|
1830
1837
|
|
|
1831
1838
|
// Subscribe to shadow state updates
|
|
1832
1839
|
useEffect(() => {
|
|
1840
|
+
console.log("Setting up shadow state subscription");
|
|
1833
1841
|
const unsubscribe = getGlobalStore
|
|
1834
1842
|
.getState()
|
|
1835
1843
|
.subscribeToShadowState(stateKey, () => {
|
|
1844
|
+
console.log("Shadow state updated");
|
|
1836
1845
|
setShadowUpdateTrigger((prev) => prev + 1);
|
|
1837
1846
|
});
|
|
1838
1847
|
return unsubscribe;
|
|
@@ -1843,9 +1852,11 @@ function createProxyHandler<T>(
|
|
|
1843
1852
|
path
|
|
1844
1853
|
) as any[];
|
|
1845
1854
|
const totalCount = sourceArray.length;
|
|
1855
|
+
console.log("Source array length:", totalCount);
|
|
1846
1856
|
|
|
1847
1857
|
// Calculate heights and positions
|
|
1848
1858
|
const { totalHeight, positions } = useMemo(() => {
|
|
1859
|
+
console.log("Recalculating heights and positions");
|
|
1849
1860
|
const shadowArray =
|
|
1850
1861
|
getGlobalStore.getState().getShadowMetadata(stateKey, path) ||
|
|
1851
1862
|
[];
|
|
@@ -1857,6 +1868,7 @@ function createProxyHandler<T>(
|
|
|
1857
1868
|
shadowArray[i]?.virtualizer?.itemHeight;
|
|
1858
1869
|
height += measuredHeight || itemHeight;
|
|
1859
1870
|
}
|
|
1871
|
+
console.log("Total height calculated:", height);
|
|
1860
1872
|
return { totalHeight: height, positions: pos };
|
|
1861
1873
|
}, [
|
|
1862
1874
|
totalCount,
|
|
@@ -1870,6 +1882,10 @@ function createProxyHandler<T>(
|
|
|
1870
1882
|
const virtualState = useMemo(() => {
|
|
1871
1883
|
const start = Math.max(0, range.startIndex);
|
|
1872
1884
|
const end = Math.min(totalCount, range.endIndex);
|
|
1885
|
+
console.log("Creating virtual state for range:", {
|
|
1886
|
+
start,
|
|
1887
|
+
end,
|
|
1888
|
+
});
|
|
1873
1889
|
const validIndices = Array.from(
|
|
1874
1890
|
{ length: end - start },
|
|
1875
1891
|
(_, i) => start + i
|
|
@@ -1882,21 +1898,42 @@ function createProxyHandler<T>(
|
|
|
1882
1898
|
}, [range.startIndex, range.endIndex, sourceArray, totalCount]);
|
|
1883
1899
|
|
|
1884
1900
|
// Handle auto-scroll to bottom
|
|
1885
|
-
const [hasMounted, setHasMounted] = useState(false);
|
|
1886
|
-
|
|
1887
|
-
// Add this effect to track mounting:
|
|
1888
1901
|
useEffect(() => {
|
|
1889
|
-
|
|
1890
|
-
|
|
1902
|
+
console.log("Auto-scroll effect triggered:", {
|
|
1903
|
+
stickToBottom,
|
|
1904
|
+
hasContainer: !!containerRef.current,
|
|
1905
|
+
totalCount,
|
|
1906
|
+
shouldStickToBottom: shouldStickToBottomRef.current,
|
|
1907
|
+
range,
|
|
1908
|
+
});
|
|
1891
1909
|
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1910
|
+
if (!stickToBottom) {
|
|
1911
|
+
console.log("Stick to bottom is false, skipping");
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
if (!containerRef.current) {
|
|
1916
|
+
console.log("No container ref, skipping");
|
|
1895
1917
|
return;
|
|
1896
|
-
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
if (totalCount === 0) {
|
|
1921
|
+
console.log("No items, skipping");
|
|
1922
|
+
return;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
if (!shouldStickToBottomRef.current) {
|
|
1926
|
+
console.log(
|
|
1927
|
+
"Should not stick to bottom (user scrolled), skipping"
|
|
1928
|
+
);
|
|
1929
|
+
return;
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
console.log("Proceeding with auto-scroll logic");
|
|
1897
1933
|
|
|
1898
1934
|
// Clear any existing interval
|
|
1899
1935
|
if (scrollToBottomIntervalRef.current) {
|
|
1936
|
+
console.log("Clearing existing scroll interval");
|
|
1900
1937
|
clearInterval(scrollToBottomIntervalRef.current);
|
|
1901
1938
|
}
|
|
1902
1939
|
|
|
@@ -1905,21 +1942,34 @@ function createProxyHandler<T>(
|
|
|
1905
1942
|
const isInitialLoad = range.endIndex < jumpThreshold;
|
|
1906
1943
|
const isBigJump = totalCount > range.endIndex + jumpThreshold;
|
|
1907
1944
|
|
|
1945
|
+
console.log("Jump check:", {
|
|
1946
|
+
isInitialLoad,
|
|
1947
|
+
isBigJump,
|
|
1948
|
+
totalCount,
|
|
1949
|
+
currentEndIndex: range.endIndex,
|
|
1950
|
+
});
|
|
1951
|
+
|
|
1908
1952
|
if (isInitialLoad || isBigJump) {
|
|
1909
|
-
|
|
1910
|
-
setRange({
|
|
1953
|
+
const newRange = {
|
|
1911
1954
|
startIndex: Math.max(0, totalCount - 20),
|
|
1912
1955
|
endIndex: totalCount,
|
|
1913
|
-
}
|
|
1956
|
+
};
|
|
1957
|
+
console.log("Jumping to end range:", newRange);
|
|
1958
|
+
setRange(newRange);
|
|
1914
1959
|
}
|
|
1915
1960
|
|
|
1916
1961
|
// Keep scrolling to bottom until we're actually there
|
|
1917
1962
|
let attempts = 0;
|
|
1918
1963
|
const maxAttempts = 50; // 5 seconds max
|
|
1919
1964
|
|
|
1965
|
+
console.log("Starting scroll-to-bottom interval");
|
|
1966
|
+
|
|
1920
1967
|
scrollToBottomIntervalRef.current = setInterval(() => {
|
|
1921
1968
|
const container = containerRef.current;
|
|
1922
|
-
if (!container)
|
|
1969
|
+
if (!container) {
|
|
1970
|
+
console.log("Container lost during interval");
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1923
1973
|
|
|
1924
1974
|
attempts++;
|
|
1925
1975
|
|
|
@@ -1928,41 +1978,54 @@ function createProxyHandler<T>(
|
|
|
1928
1978
|
const actualBottom = scrollHeight;
|
|
1929
1979
|
const isAtBottom = actualBottom - currentBottom < 5;
|
|
1930
1980
|
|
|
1931
|
-
console.log(
|
|
1932
|
-
|
|
1933
|
-
|
|
1981
|
+
console.log(`Scroll attempt ${attempts}:`, {
|
|
1982
|
+
currentBottom,
|
|
1983
|
+
actualBottom,
|
|
1984
|
+
isAtBottom,
|
|
1985
|
+
scrollTop,
|
|
1986
|
+
scrollHeight,
|
|
1987
|
+
clientHeight,
|
|
1988
|
+
});
|
|
1934
1989
|
|
|
1935
1990
|
if (isAtBottom || attempts >= maxAttempts) {
|
|
1936
|
-
clearInterval(scrollToBottomIntervalRef.current!);
|
|
1937
|
-
scrollToBottomIntervalRef.current = null;
|
|
1938
1991
|
console.log(
|
|
1939
|
-
isAtBottom
|
|
1992
|
+
isAtBottom
|
|
1993
|
+
? "Successfully reached bottom!"
|
|
1994
|
+
: "Timeout - giving up"
|
|
1940
1995
|
);
|
|
1996
|
+
clearInterval(scrollToBottomIntervalRef.current!);
|
|
1997
|
+
scrollToBottomIntervalRef.current = null;
|
|
1941
1998
|
} else {
|
|
1942
|
-
|
|
1999
|
+
console.log("Scrolling to", container.scrollHeight);
|
|
1943
2000
|
container.scrollTop = container.scrollHeight;
|
|
1944
2001
|
}
|
|
1945
2002
|
}, 100);
|
|
1946
2003
|
|
|
1947
2004
|
// Cleanup
|
|
1948
2005
|
return () => {
|
|
2006
|
+
console.log("Cleaning up scroll interval");
|
|
1949
2007
|
if (scrollToBottomIntervalRef.current) {
|
|
1950
2008
|
clearInterval(scrollToBottomIntervalRef.current);
|
|
1951
2009
|
scrollToBottomIntervalRef.current = null;
|
|
1952
2010
|
}
|
|
1953
2011
|
};
|
|
1954
|
-
}, [totalCount, stickToBottom,
|
|
2012
|
+
}, [totalCount, stickToBottom, range.startIndex, range.endIndex]);
|
|
1955
2013
|
|
|
1956
2014
|
// Handle user scroll
|
|
1957
2015
|
useEffect(() => {
|
|
1958
2016
|
const container = containerRef.current;
|
|
1959
|
-
if (!container)
|
|
2017
|
+
if (!container) {
|
|
2018
|
+
console.log("No container for scroll handler");
|
|
2019
|
+
return;
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
console.log("Setting up scroll handler");
|
|
1960
2023
|
|
|
1961
2024
|
let scrollTimeout: NodeJS.Timeout;
|
|
1962
2025
|
|
|
1963
2026
|
const handleScroll = () => {
|
|
1964
2027
|
if (scrollToBottomIntervalRef.current) {
|
|
1965
|
-
|
|
2028
|
+
console.log("User scrolled - stopping auto-scroll");
|
|
1966
2029
|
clearInterval(scrollToBottomIntervalRef.current);
|
|
1967
2030
|
scrollToBottomIntervalRef.current = null;
|
|
1968
2031
|
}
|
|
@@ -1973,12 +2036,14 @@ function createProxyHandler<T>(
|
|
|
1973
2036
|
|
|
1974
2037
|
// Update whether we should stick to bottom
|
|
1975
2038
|
shouldStickToBottomRef.current = isAtBottom;
|
|
2039
|
+
console.log("User scroll - at bottom:", isAtBottom);
|
|
1976
2040
|
|
|
1977
2041
|
// Mark as user scrolling
|
|
1978
2042
|
clearTimeout(scrollTimeout);
|
|
1979
2043
|
isUserScrollingRef.current = true;
|
|
1980
2044
|
scrollTimeout = setTimeout(() => {
|
|
1981
2045
|
isUserScrollingRef.current = false;
|
|
2046
|
+
console.log("User stopped scrolling");
|
|
1982
2047
|
}, 150);
|
|
1983
2048
|
|
|
1984
2049
|
// Update visible range
|
|
@@ -1999,18 +2064,23 @@ function createProxyHandler<T>(
|
|
|
1999
2064
|
endIndex = i;
|
|
2000
2065
|
}
|
|
2001
2066
|
|
|
2002
|
-
|
|
2067
|
+
const newRange = {
|
|
2003
2068
|
startIndex: Math.max(0, startIndex),
|
|
2004
2069
|
endIndex: Math.min(totalCount, endIndex + 1 + overscan),
|
|
2005
|
-
}
|
|
2070
|
+
};
|
|
2071
|
+
|
|
2072
|
+
console.log("Updating visible range:", newRange);
|
|
2073
|
+
setRange(newRange);
|
|
2006
2074
|
};
|
|
2007
2075
|
|
|
2008
2076
|
container.addEventListener("scroll", handleScroll, {
|
|
2009
2077
|
passive: true,
|
|
2010
2078
|
});
|
|
2079
|
+
console.log("Initial scroll calculation");
|
|
2011
2080
|
handleScroll(); // Initial calculation
|
|
2012
2081
|
|
|
2013
2082
|
return () => {
|
|
2083
|
+
console.log("Removing scroll handler");
|
|
2014
2084
|
container.removeEventListener("scroll", handleScroll);
|
|
2015
2085
|
clearTimeout(scrollTimeout);
|
|
2016
2086
|
};
|
|
@@ -2018,6 +2088,7 @@ function createProxyHandler<T>(
|
|
|
2018
2088
|
|
|
2019
2089
|
const scrollToBottom = useCallback(
|
|
2020
2090
|
(behavior: ScrollBehavior = "auto") => {
|
|
2091
|
+
console.log("Manual scrollToBottom called");
|
|
2021
2092
|
shouldStickToBottomRef.current = true;
|
|
2022
2093
|
if (containerRef.current) {
|
|
2023
2094
|
containerRef.current.scrollTop =
|
|
@@ -2029,6 +2100,7 @@ function createProxyHandler<T>(
|
|
|
2029
2100
|
|
|
2030
2101
|
const scrollToIndex = useCallback(
|
|
2031
2102
|
(index: number, behavior: ScrollBehavior = "smooth") => {
|
|
2103
|
+
console.log("scrollToIndex called:", index);
|
|
2032
2104
|
if (containerRef.current && positions[index] !== undefined) {
|
|
2033
2105
|
containerRef.current.scrollTo({
|
|
2034
2106
|
top: positions[index],
|
|
@@ -2057,6 +2129,12 @@ function createProxyHandler<T>(
|
|
|
2057
2129
|
},
|
|
2058
2130
|
};
|
|
2059
2131
|
|
|
2132
|
+
console.log("Returning virtualizer with props:", {
|
|
2133
|
+
range,
|
|
2134
|
+
totalHeight,
|
|
2135
|
+
startPosition: positions[range.startIndex] || 0,
|
|
2136
|
+
});
|
|
2137
|
+
|
|
2060
2138
|
return {
|
|
2061
2139
|
virtualState,
|
|
2062
2140
|
virtualizerProps,
|