11'use client'
22
3- import React , { KeyboardEvent , useEffect , useState } from 'react'
3+ import React , { KeyboardEvent , useEffect , useRef , useState } from 'react'
44import { useParams } from 'next/navigation'
55
66interface PathInputProps {
@@ -13,7 +13,8 @@ export default function PathInput({ pathState, nameState }: PathInputProps) {
1313 const [ , setName ] = nameState
1414 const params = useParams ( )
1515
16- const [ basePath , setBasePath ] = useState < string [ ] > ( [ ] )
16+ // Store fixed base path in ref (no re-renders needed)
17+ const fixedBasePathRef = useRef < string [ ] > ( [ ] )
1718
1819 const [ userSegments , setUserSegments ] = useState < string [ ] > ( [ ] )
1920 const [ current , setCurrent ] = useState < string > ( '' )
@@ -27,7 +28,7 @@ export default function PathInput({ pathState, nameState }: PathInputProps) {
2728 }
2829
2930 useEffect ( ( ) => {
30- const raw = ( params as any ) ?. path
31+ const raw = params ?. path
3132 let newBase : string [ ] = [ ]
3233
3334 if ( Array . isArray ( raw ) ) {
@@ -37,10 +38,10 @@ export default function PathInput({ pathState, nameState }: PathInputProps) {
3738 }
3839 newBase . unshift ( '' )
3940
40- setBasePath ( newBase )
41+ fixedBasePathRef . current = newBase
4142 updatePath ( newBase , userSegments , current )
4243 // eslint-disable-next-line react-hooks/exhaustive-deps
43- } , [ JSON . stringify ( ( params as any ) ?. path ) ] )
44+ } , [ params ?. path ] )
4445
4546 const handleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
4647 const value = e . target . value
@@ -55,14 +56,14 @@ export default function PathInput({ pathState, nameState }: PathInputProps) {
5556
5657 setUserSegments ( nextUser )
5758 setCurrent ( last )
58- updatePath ( basePath , nextUser , last )
59+ updatePath ( fixedBasePathRef . current , nextUser , last )
5960 } else {
6061 setCurrent ( '' )
61- updatePath ( basePath , userSegments , '' )
62+ updatePath ( fixedBasePathRef . current , userSegments , '' )
6263 }
6364 } else {
6465 setCurrent ( value )
65- updatePath ( basePath , userSegments , value )
66+ updatePath ( fixedBasePathRef . current , userSegments , value )
6667 }
6768 }
6869
@@ -73,26 +74,22 @@ export default function PathInput({ pathState, nameState }: PathInputProps) {
7374 const nextUser = userSegments . slice ( 0 , - 1 )
7475
7576 setUserSegments ( nextUser )
76- updatePath ( basePath , nextUser , '' )
77- } else if ( basePath . length > 1 ) {
78- const nextBase = basePath . slice ( 0 , - 1 )
79-
80- setBasePath ( nextBase )
81- updatePath ( nextBase , userSegments , '' )
77+ updatePath ( fixedBasePathRef . current , nextUser , '' )
8278 }
79+ // basePath is now protected - cannot be deleted
8380 }
8481 }
8582
8683 return (
8784 < div className = 'text-primary flex max-w-[900px] flex-wrap items-center gap-x-1 gap-y-2' >
88- { [ ...basePath , ...userSegments ] . map ( ( seg , i , arr ) => (
85+ { [ ...fixedBasePathRef . current , ...userSegments ] . map ( ( seg , i , arr ) => (
8986 // eslint-disable-next-line react/no-array-index-key
9087 < React . Fragment key = { i } >
9188 < span className = 'text-accent font-medium' > { seg } </ span >
9289 { i < arr . length - 1 && < span className = 'text-secondary' > /</ span > }
9390 </ React . Fragment >
9491 ) ) }
95- { [ ...basePath , ...userSegments ] . length > 0 && < span className = 'text-secondary' > /</ span > }
92+ { [ ...fixedBasePathRef . current , ...userSegments ] . length > 0 && < span className = 'text-secondary' > /</ span > }
9693
9794 < input
9895 type = 'text'
0 commit comments