Skip to content

Commit

Permalink
fix: option handling
Browse files Browse the repository at this point in the history
Fix initialization of global state from localStorage.
  • Loading branch information
cgawron committed Mar 19, 2024
1 parent 01a4914 commit 20a3153
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/chat/Chat.jsx → src/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import './style.less'

export default function Chat() {
const { is } = useGlobal()
const chatStyle = is.fullScreen ? styles.full : styles.normal
const chatStyle = is?.fullScreen ? styles.full : styles.normal
const onSearch = (e) => {
console.log(e)
}
Expand All @@ -22,16 +22,16 @@ export default function Chat() {
<div className={styles.chat_inner}>
<ChatSideBar />
{
is.config ?
is?.config ?
<ChatOptions /> :
<React.Fragment>
{
is.sidebar && <div className={styles.sider}>
is?.sidebar && <div className={styles.sider}>
<div className={styles.search}>
<Search onSearch={onSearch} />
</div>
<ScrollView>
{is.apps ? <Apps /> : <ChatList />}
{is?.apps ? <Apps /> : <ChatList />}
</ScrollView>
</div>
}
Expand Down
20 changes: 19 additions & 1 deletion src/chat/context/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ import i18next, { t, use } from "i18next";
import { useApps } from "../apps/context";
import { AccountOptions, GeneralOptions, GlobalState, OpenAIOptions, Options, OptionAction } from ".";

export default function action(state, dispatch) {
export type GlobalAction = {
setState: (payload: Partial<GlobalState>) => void;
clearTypeing: () => void;
sendMessage: () => void;
setApp: (app: any) => void;
newChat: (app: any) => void;
modifyChat: (arg: any, index: number) => void;
editChat: (index: number, title: string) => void;
removeChat: (index: number) => void;
setMessage: (content: string) => void;
clearMessage: () => void;
removeMessage: (id: number) => void;
setOptions: (arg: OptionAction) => void;
setIs: (arg: any) => void;
currentList: () => any;
stopResonse: () => void;
};

export default function action(state, dispatch): GlobalAction {
const setState = (payload = {}) =>
dispatch({
type: "SET_STATE",
Expand Down
6 changes: 3 additions & 3 deletions src/chat/context/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const ChatContext = createContext(null);
export const MessagesContext = createContext<Dispatch<GlobalAction> | null>(null);

export const ChatProvider = ({ children }) => {
const init = JSON.parse(localStorage.getItem("SESSIONS") || "") || initState;
const init: GlobalState = JSON.parse(localStorage.getItem("SESSIONS")) || initState;
const [state, dispatch] = useReducer(reducer, init);
const actionList = action(state, dispatch);
const latestState = useRef(state);
Expand All @@ -101,7 +101,7 @@ export const ChatProvider = ({ children }) => {
}, [state]);

useEffect(() => {
const savedState = JSON.parse(localStorage.getItem("SESSIONS") || "");
const savedState = JSON.parse(localStorage.getItem("SESSIONS"));
if (savedState) {
dispatch({ type: GlobalActionType.SET_STATE, payload: savedState });
}
Expand Down Expand Up @@ -130,7 +130,7 @@ export const ChatProvider = ({ children }) => {
);
};

export const useGlobal = () => useContext(ChatContext);
export const useGlobal = () => useContext<GlobalAction & GlobalState>(ChatContext);
export const useMessages = () => useContext(MessagesContext);


2 changes: 1 addition & 1 deletion src/chat/hooks/useOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useGlobal } from "../context";

export function useOptions() {
const { options, setOptions } = useGlobal();
const { size, theme } = options.general;
const { size, theme } = options?.general;
useEffect(() => {
const body = document.querySelector("html");
body.classList = [];
Expand Down

0 comments on commit 20a3153

Please sign in to comment.