提示词修改
This commit is contained in:
1 parent
c79f5c5a7a
commit
a7f1e89ce9
6 files changed
+389
-27
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { useCallback, useSyncExternalStore } from "react";
|
||||
|
||||
import {
|
||||
DEFAULT_QUESTIONS,
|
||||
getRecommendedQuestions,
|
||||
saveRecommendedQuestions,
|
||||
type RecommendedQuestion,
|
||||
} from "./index";
|
||||
|
||||
type Listener = () => void;
|
||||
|
||||
const listeners = new Set<Listener>();
|
||||
let cached: RecommendedQuestion[] | null = null;
|
||||
|
||||
function getSnapshot(): RecommendedQuestion[] {
|
||||
if (cached === null) {
|
||||
cached = getRecommendedQuestions();
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
function getServerSnapshot(): RecommendedQuestion[] {
|
||||
return DEFAULT_QUESTIONS;
|
||||
}
|
||||
|
||||
function subscribe(listener: Listener): () => void {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
|
||||
function emitChange() {
|
||||
for (const l of listeners) l();
|
||||
}
|
||||
|
||||
export function updateRecommendedQuestions(
|
||||
questions: RecommendedQuestion[],
|
||||
): void {
|
||||
cached = questions;
|
||||
saveRecommendedQuestions(questions);
|
||||
emitChange();
|
||||
}
|
||||
|
||||
export function useRecommendedQuestions(): [
|
||||
RecommendedQuestion[],
|
||||
(questions: RecommendedQuestion[]) => void,
|
||||
] {
|
||||
const questions = useSyncExternalStore(
|
||||
subscribe,
|
||||
getSnapshot,
|
||||
getServerSnapshot,
|
||||
);
|
||||
|
||||
const save = useCallback((qs: RecommendedQuestion[]) => {
|
||||
updateRecommendedQuestions(qs);
|
||||
}, []);
|
||||
|
||||
return [questions, save];
|
||||
}
|
||||
Reference in new issue
Block a user