From c4cca8cf6c62b55296cfaca2ddd22606af6586a1 Mon Sep 17 00:00:00 2001
From: Paul Frazee <pfrazee@gmail.com>
Date: Fri, 20 Jan 2023 10:15:59 -0600
Subject: Sanity check selector swipes

---
 src/view/com/util/ViewSelector.tsx | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

(limited to 'src/view/com/util/ViewSelector.tsx')

diff --git a/src/view/com/util/ViewSelector.tsx b/src/view/com/util/ViewSelector.tsx
index a9252035f..9ea7bc740 100644
--- a/src/view/com/util/ViewSelector.tsx
+++ b/src/view/com/util/ViewSelector.tsx
@@ -4,6 +4,7 @@ import {Selector} from './Selector'
 import {HorzSwipe} from './gestures/HorzSwipe'
 import {useAnimatedValue} from '../../lib/hooks/useAnimatedValue'
 import {OnScrollCb} from '../../lib/hooks/useOnMainScroll'
+import {clamp} from '../../../lib/numbers'
 
 const HEADER_ITEM = {_reactKey: '__header__'}
 const SELECTOR_ITEM = {_reactKey: '__selector__'}
@@ -46,10 +47,11 @@ export function ViewSelector({
 
   const onSwipeEnd = (dx: number) => {
     if (dx !== 0) {
-      setSelectedIndex(selectedIndex + dx)
+      setSelectedIndex(clamp(selectedIndex + dx, 0, sections.length))
     }
   }
-  const onPressSelection = (index: number) => setSelectedIndex(index)
+  const onPressSelection = (index: number) =>
+    setSelectedIndex(clamp(index, 0, sections.length))
   useEffect(() => {
     onSelectView?.(selectedIndex)
   }, [selectedIndex])
-- 
cgit 1.4.1

