@@ -3,14 +3,16 @@ import { create } from "zustand"
33interface QsStore {
44 question : [ string , string ] [ ]
55 lastIndex : number
6+ nowIndex : number
67 setQuestion : ( qsString : string ) => void
78 getRandomQuestion : ( ) => [ string , string ]
9+ getLastQuestion : ( ) => [ string , string ]
810}
911
1012const useQsStore = create < QsStore > ( ( set , get ) => ( {
1113 question : [ ] ,
1214 lastIndex : - 1 ,
13-
15+ nowIndex : - 1 ,
1416 setQuestion : ( qsString : string ) => {
1517 if ( ! qsString ?. trim ( ) ) {
1618 console . warn ( "setQuestion: 输入必须是非空字符串" )
@@ -49,12 +51,25 @@ const useQsStore = create<QsStore>((set, get) => ({
4951 getRandomQuestion : ( ) => {
5052 const questions = get ( ) . question
5153 const count = questions . length
54+ if ( get ( ) . lastIndex !== get ( ) . nowIndex ) {
55+ get ( ) . lastIndex = get ( ) . nowIndex
56+ }
57+
5258 let randomIndex = Math . floor ( Math . random ( ) * count )
5359 while ( randomIndex === get ( ) . lastIndex ) {
5460 randomIndex = Math . floor ( Math . random ( ) * count )
5561 }
62+ get ( ) . nowIndex = randomIndex
5663 console . log ( questions [ randomIndex ] )
5764 return questions [ randomIndex ]
65+ } ,
66+ getLastQuestion : ( ) => {
67+ const questions = get ( ) . question
68+ const lastIndex = get ( ) . lastIndex
69+ get ( ) . nowIndex = lastIndex
70+ console . log ( "last" , lastIndex )
71+ console . log ( "lastquestion:" , questions [ lastIndex ] )
72+ return questions [ lastIndex ]
5873 }
5974} ) )
6075
0 commit comments