11"use client" ;
22
33import React , { createContext , useCallback , useContext , useRef , useState } from "react" ;
4- import { useRouter , ReadonlyURLSearchParams } from "next/navigation" ;
4+ import { useRouter , ReadonlyURLSearchParams , useSelectedLayoutSegments } from "next/navigation" ;
55// import { useAudio } from '@context/AudioContext';
66// import { useSnackbar } from "@context/SnackbarContext";
77// import { useGamepads } from 'awesome-react-gamepads';
@@ -67,11 +67,14 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
6767 const [ currentCategory , setCurrentCategory ] = useState < XmbCategory | null > ( null ) ;
6868 const [ currentItem , setCurrentItem ] = useState < XmbItem | null > ( null ) ;
6969 const [ currentItems , setCurrentItems ] = useState < XmbItem [ ] | null > ( null ) ;
70- const [ modal , setModal ] = useState < string | null > ( null ) ;
70+ // const [modal, setModal] = useState<string | null>(null);
7171 const xmbMenuRef = useRef < XmbMenu | null > ( null ) ;
7272 const { play } = useAudio ( ) ;
7373 const { showSnackbar } = useSnackbar ( ) ;
7474
75+ const segment = useSelectedLayoutSegments ( 'modal' ) ;
76+ const modal = segment . filter ( s => ! s . startsWith ( '(' ) ) . length !== 0 ;
77+
7578 if ( ! xmbItemRef . current ) {
7679 xmbItemRef . current = new Map < string , XmbItem > ( ) ;
7780 }
@@ -98,18 +101,18 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
98101 }
99102
100103 // udpates the selected item (y)
101- const updateY = ( newY : number ) => {
104+ const updateY = useCallback ( ( newY : number ) => {
102105 // update cache
103106 cache [ positionRef . current . x ] = newY ;
104107 positionRef . current = { ...positionRef . current , y : newY } ;
105108 if ( ! currentCategory ) return ;
106109 const item = currentCategory . items [ newY ] ;
107110 setCurrentItem ( item ) ;
108111 setY ( newY ) ;
109- } ;
112+ } , [ currentCategory ] ) ;
110113
111114 // udpates both the category (x) and restores the previous selected item (y)
112- const updateX = ( newX : number , loadCache : boolean = true ) => {
115+ const updateX = useCallback ( ( newX : number , loadCache : boolean = true ) => {
113116 const prevY = loadCache ? ( cache [ newX ] ?? 0 ) : 0 ;
114117 setX ( newX ) ;
115118 positionRef . current = { x : newX , y : prevY } ;
@@ -118,7 +121,7 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
118121 setCurrentCategory ( cat ) ;
119122 setCurrentItems ( cat . items ) ;
120123 updateY ( prevY ) ;
121- } ;
124+ } , [ categories , updateY ] ) ;
122125
123126 const openItem = useCallback ( ( item : XmbItem ) => {
124127 if ( item . modal ) {
@@ -179,7 +182,7 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
179182 router . push ( '/?modal=help' ) ;
180183 } , [ ] ) ;
181184
182- const moveUp = ( ) => {
185+ const moveUp = useCallback ( ( ) => {
183186 const nextY = y - 1 ;
184187
185188 play ( XMB_AUDIO_SRC ) ;
@@ -189,9 +192,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
189192 updateY ( nextY ) ;
190193
191194 return positionRef . current ;
192- } ;
195+ } , [ y , updateY ] ) ;
193196
194- const moveTop = ( ) => {
197+ const moveTop = useCallback ( ( ) => {
195198 const nextY = 0 ;
196199
197200 play ( XMB_AUDIO_SRC ) ;
@@ -201,9 +204,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
201204 updateY ( nextY ) ;
202205
203206 return positionRef . current ;
204- } ;
207+ } , [ y , updateY ] ) ;
205208
206- const moveDown = ( ) => {
209+ const moveDown = useCallback ( ( ) => {
207210 if ( ! currentCategory ) return null ;
208211
209212 play ( XMB_AUDIO_SRC ) ;
@@ -216,9 +219,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
216219 updateY ( nextY ) ;
217220
218221 return positionRef . current ;
219- } ;
222+ } , [ currentCategory , y , updateY ] ) ;
220223
221- const moveBottom = ( ) => {
224+ const moveBottom = useCallback ( ( ) => {
222225 if ( ! currentCategory ) return null ;
223226
224227 play ( XMB_AUDIO_SRC ) ;
@@ -230,9 +233,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
230233 updateY ( max ) ;
231234
232235 return positionRef . current ;
233- } ;
236+ } , [ currentCategory , y , updateY ] ) ;
234237
235- const moveLeft = ( ) => {
238+ const moveLeft = useCallback ( ( ) => {
236239 if ( ! categories ) return null ;
237240
238241 play ( XMB_AUDIO_SRC ) ;
@@ -245,9 +248,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
245248 updateX ( nextX ) ;
246249
247250 return positionRef . current ;
248- } ;
251+ } , [ x , categories , updateX ] ) ;
249252
250- const moveFirst = ( ) => {
253+ const moveFirst = useCallback ( ( ) => {
251254 if ( ! categories ) return null ;
252255
253256 play ( XMB_AUDIO_SRC ) ;
@@ -260,9 +263,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
260263 updateX ( nextX ) ;
261264
262265 return positionRef . current ;
263- } ;
266+ } , [ x , categories , updateX ] ) ;
264267
265- const moveRight = ( ) => {
268+ const moveRight = useCallback ( ( ) => {
266269 if ( ! categories ) return null ;
267270
268271 play ( XMB_AUDIO_SRC ) ;
@@ -276,9 +279,9 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
276279 updateX ( nextX ) ;
277280
278281 return positionRef . current ;
279- } ;
282+ } , [ x , categories , updateX ] ) ;
280283
281- const moveLast = ( ) => {
284+ const moveLast = useCallback ( ( ) => {
282285 if ( ! categories ) return null ;
283286 play ( XMB_AUDIO_SRC ) ;
284287
@@ -290,13 +293,7 @@ export function XmbProvider({ children }: { children: React.ReactNode }) {
290293 updateX ( max ) ;
291294
292295 return positionRef . current ;
293- } ;
294-
295- const onPathChanged = useCallback ( ( path : string , searchParams : ReadonlyURLSearchParams , modal : string | null ) => {
296- setModal ( modal ) ;
297- } , [ ] ) ;
298-
299- useQuery ( { onPathChanged : onPathChanged } ) ;
296+ } , [ x , categories , updateX ] ) ;
300297
301298 const actions = new Map < string , KeyPressAction > ( ) ;
302299
0 commit comments