paper-ai-release-24-07-21/app/store/slices/authSlice.ts

102 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-01-21 23:08:25 +08:00
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { Reference } from "@/utils/global";
2024-01-21 23:08:25 +08:00
export interface APIState {
apiKey: string;
referencesRedux: Reference[];
2024-01-23 12:10:03 +08:00
editorContent: string;
upsreamUrl: string;
2024-01-29 13:35:24 +08:00
systemPrompt: string;
2024-02-08 10:31:29 +08:00
showPaperManagement: boolean;
2024-01-21 23:08:25 +08:00
}
const initialState: APIState = {
2024-01-30 15:16:39 +08:00
apiKey: "sess-L6DwIB7N859iQLWfNBTaPsmkErqZrjoXVk6m7BmA",
referencesRedux: [],
2024-01-23 12:10:03 +08:00
editorContent: "",
2024-01-30 15:16:39 +08:00
upsreamUrl: "https://api.liuweiqing.top", //https://api.openai.com https://one.caifree.com https://chatserver.3211000.xyz
2024-01-29 13:35:24 +08:00
systemPrompt: "",
2024-02-08 10:31:29 +08:00
showPaperManagement: false,
2024-01-21 23:08:25 +08:00
};
export const authSlice = createSlice({
name: "auth",
initialState,
reducers: {
2024-02-08 10:31:29 +08:00
setShowPaperManagement: (state, action: PayloadAction<boolean>) => {
state.showPaperManagement = action.payload;
},
2024-01-21 23:08:25 +08:00
setApiKey: (state, action: PayloadAction<string>) => {
state.apiKey = action.payload;
},
setUpsreamUrl: (state, action: PayloadAction<string>) => {
state.upsreamUrl = action.payload;
},
2024-01-29 13:35:24 +08:00
setSystemPrompt: (state, action: PayloadAction<string>) => {
state.systemPrompt = action.payload;
},
2024-01-29 15:01:43 +08:00
setEditorContent: (state, action: PayloadAction<string>) => {
state.editorContent = action.payload;
},
addReferenceRedux: (state, action: PayloadAction<Reference>) => {
state.referencesRedux.push(action.payload);
},
addReferencesRedux: (
state,
action: PayloadAction<{ references: Reference[]; position?: number }>
) => {
const { references, position } = action.payload;
const insertPosition =
position !== undefined ? position : state.referencesRedux.length;
state.referencesRedux.splice(insertPosition, 0, ...references);
},
removeReferenceRedux: (state, action: PayloadAction<number>) => {
state.referencesRedux = state.referencesRedux.filter(
(_, i) => i !== action.payload
);
},
clearReferencesRedux: (state) => {
state.referencesRedux = [];
},
2024-02-08 10:31:29 +08:00
setReferencesRedux: (state, action: PayloadAction<Reference[]>) => {
state.referencesRedux = action.payload;
},
2024-01-29 15:01:43 +08:00
swapReferencesRedux: (
state,
action: PayloadAction<{ indexA: number; indexB: number }>
) => {
console.log("moveReference", state.referencesRedux); // 调试输出
const { indexA, indexB } = action.payload;
if (
indexA >= 0 &&
indexA < state.referencesRedux.length &&
indexB >= 0 &&
indexB < state.referencesRedux.length
) {
const newReferences = [...state.referencesRedux];
const temp = newReferences[indexA];
newReferences[indexA] = newReferences[indexB];
newReferences[indexB] = temp;
state.referencesRedux = newReferences;
}
2024-01-23 12:10:03 +08:00
},
2024-01-21 23:08:25 +08:00
},
});
// Action creators are generated for each case reducer function
export const {
2024-02-08 10:31:29 +08:00
setShowPaperManagement,
setApiKey,
setUpsreamUrl,
addReferenceRedux,
addReferencesRedux,
removeReferenceRedux,
clearReferencesRedux,
2024-01-23 12:10:03 +08:00
setEditorContent,
2024-02-08 10:31:29 +08:00
setReferencesRedux,
2024-01-29 13:35:24 +08:00
setSystemPrompt,
2024-01-29 15:01:43 +08:00
swapReferencesRedux,
} = authSlice.actions;
2024-01-21 23:08:25 +08:00
export const authReducer = authSlice.reducer;