mirror of
https://github.com/coaidev/coai.git
synced 2025-05-22 06:20:14 +09:00
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import { createSlice } from "@reduxjs/toolkit";
|
|
import { getSubscription } from "@/conversation/addition.ts";
|
|
import { AppDispatch } from "./index.ts";
|
|
|
|
export const subscriptionSlice = createSlice({
|
|
name: "subscription",
|
|
initialState: {
|
|
dialog: false,
|
|
is_subscribed: false,
|
|
expired: 0,
|
|
usage: {
|
|
gpt4: 0,
|
|
dalle: 0,
|
|
},
|
|
},
|
|
reducers: {
|
|
toggleDialog: (state) => {
|
|
state.dialog = !state.dialog;
|
|
},
|
|
setDialog: (state, action) => {
|
|
state.dialog = action.payload as boolean;
|
|
},
|
|
openDialog: (state) => {
|
|
state.dialog = true;
|
|
},
|
|
closeDialog: (state) => {
|
|
state.dialog = false;
|
|
},
|
|
updateSubscription: (state, action) => {
|
|
state.is_subscribed = action.payload.is_subscribed;
|
|
state.expired = action.payload.expired;
|
|
state.usage = action.payload.usage;
|
|
},
|
|
},
|
|
});
|
|
|
|
export const {
|
|
toggleDialog,
|
|
setDialog,
|
|
openDialog,
|
|
closeDialog,
|
|
updateSubscription,
|
|
} = subscriptionSlice.actions;
|
|
export default subscriptionSlice.reducer;
|
|
|
|
export const dialogSelector = (state: any): boolean =>
|
|
state.subscription.dialog;
|
|
export const isSubscribedSelector = (state: any): boolean =>
|
|
state.subscription.is_subscribed;
|
|
export const expiredSelector = (state: any): number =>
|
|
state.subscription.expired;
|
|
export const usageSelector = (state: any): any => state.subscription.usage;
|
|
|
|
export const refreshSubscription = async (dispatch: AppDispatch) => {
|
|
const current = new Date().getTime(); //@ts-ignore
|
|
if (
|
|
window.hasOwnProperty("subscription") && //@ts-ignore
|
|
current - window.subscription < 2500
|
|
)
|
|
return; //@ts-ignore
|
|
window.subscription = current;
|
|
|
|
const response = await getSubscription();
|
|
if (response.status) dispatch(updateSubscription(response));
|
|
};
|
|
|
|
export const refreshSubscriptionTask = (dispatch: AppDispatch) => {
|
|
setInterval(() => refreshSubscription(dispatch), 20000);
|
|
refreshSubscription(dispatch).then();
|
|
};
|