11import { useCallback , useRef , useState } from "react" ;
2+ import { shuffle } from "es-toolkit" ;
23
34import { RANDOM_NICKNAMES } from "#/constants/nickname" ;
45
5- const shuffle = < T > ( arr : T [ ] ) : T [ ] => {
6- const result = [ ...arr ] ;
7- for ( let i = result . length - 1 ; i > 0 ; i -- ) {
8- const j = Math . floor ( Math . random ( ) * ( i + 1 ) ) ;
9- [ result [ i ] , result [ j ] ] = [ result [ j ] , result [ i ] ] ;
10- }
11- return result ;
12- } ;
13-
14- const createInitialPool = ( ) => shuffle ( [ ...RANDOM_NICKNAMES ] ) ;
6+ const generateInitialPool = ( ) => shuffle ( [ ...RANDOM_NICKNAMES ] ) ;
157
168export const useRandomNickname = ( ) => {
179 // 초기 풀은 모듈 스코프 함수로 생성하여 렌더 시점 ref 접근 없이 초기화
18- const initialPool = createInitialPool ( ) ;
10+ const initialPool = generateInitialPool ( ) ;
1911 const initialNicknameValue = initialPool [ 0 ] ;
2012
2113 const poolRef = useRef < string [ ] > ( initialPool ) ;
@@ -26,7 +18,7 @@ export const useRandomNickname = () => {
2618
2719 const getNextNickname = useCallback ( ( ) : string => {
2820 if ( indexRef . current >= poolRef . current . length ) {
29- let newPool = shuffle ( [ ... RANDOM_NICKNAMES ] ) ;
21+ let newPool = generateInitialPool ( ) ;
3022 // 재셔플 시 마지막 사용 닉네임이 첫 번째가 되는 것 방지
3123 if (
3224 lastUsedRef . current !== null &&
0 commit comments