useVirtualizedTreeReducer.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. export interface VirtualizedState<T> {
  2. overscroll: number;
  3. roots: T[];
  4. scrollHeight: number;
  5. scrollTop: number;
  6. selectedNodeIndex: number | null;
  7. }
  8. interface SetScrollTop {
  9. payload: number;
  10. type: 'set scroll top';
  11. }
  12. interface SetSelectedNodeIndex {
  13. payload: number | null;
  14. type: 'set selected node index';
  15. }
  16. interface SetContainerHeight {
  17. payload: number;
  18. type: 'set scroll height';
  19. }
  20. interface ScrollToIndex {
  21. payload: {scrollTop: number; selectedNodeIndex: number};
  22. type: 'scroll to index';
  23. }
  24. type VirtualizedStateAction =
  25. | SetScrollTop
  26. | SetContainerHeight
  27. | SetSelectedNodeIndex
  28. | ScrollToIndex;
  29. export function VirtualizedTreeReducer<T>(
  30. state: VirtualizedState<T>,
  31. action: VirtualizedStateAction
  32. ): VirtualizedState<T> {
  33. switch (action.type) {
  34. case 'scroll to index': {
  35. return {...state, ...action.payload};
  36. }
  37. case 'set selected node index': {
  38. return {...state, selectedNodeIndex: action.payload};
  39. }
  40. case 'set scroll top': {
  41. return {...state, scrollTop: action.payload};
  42. }
  43. case 'set scroll height': {
  44. return {...state, scrollHeight: action.payload};
  45. }
  46. default: {
  47. return state;
  48. }
  49. }
  50. }