Skip to content

Commit 1c02048

Browse files
frant1cfrant1c
authored andcommitted
feat: add getLastQuestion function
1 parent 27c39b0 commit 1c02048

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/pages/home.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Home() {
1313
loop: false,
1414
type: "text"
1515
})
16-
const { getRandomQuestion, setQuestion } = useQsStore()
16+
const { getRandomQuestion, setQuestion, getLastQuestion } = useQsStore()
1717
const handleFileChange = async (event: any) => {
1818
const file = event.target.files[0]
1919
if (!file) return
@@ -46,6 +46,10 @@ function Home() {
4646
await setNowQuestion(getRandomQuestion())
4747
setShowAnswer(false)
4848
}
49+
const handleLastQuestion = async () => {
50+
await setNowQuestion(getLastQuestion())
51+
setShowAnswer(false)
52+
}
4953
return (
5054
<>
5155
{upload ? (
@@ -61,7 +65,10 @@ function Home() {
6165
<div className="text-white">{showAnswer ? "关闭" : "显示"}答案</div>
6266
</div>
6367
<div className="absolute top-[300px] flex flex-nowrap gap-[120px]">
64-
<div className="flex h-[40px] w-[80px] cursor-pointer items-center justify-center rounded-xl border-2 border-black/50 bg-gray-500">
68+
<div
69+
className="flex h-[40px] w-[80px] cursor-pointer items-center justify-center rounded-xl border-2 border-black/50 bg-gray-500"
70+
onClick={() => handleLastQuestion()}
71+
>
6572
<div className="text-white">上一题</div>
6673
</div>
6774
<div

src/store/question.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { create } from "zustand"
33
interface 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

1012
const 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

Comments
 (0)