Skip to content

Commit

Permalink
add feature: /?q= auto request
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 2, 2023
1 parent d02668a commit a378a57
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/src/conf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";

export const version: string = "3.2.0";
export const version: string = "3.2.1";
export const deploy: boolean = true;
export let rest_api: string = "http://localhost:8094";
export let ws_api: string = "ws://localhost:8094";
Expand Down
30 changes: 23 additions & 7 deletions app/src/routes/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from "../components/ui/tooltip.tsx";
import { useDispatch, useSelector } from "react-redux";
import type { RootState } from "../store";
import { selectAuthenticated } from "../store/auth.ts";
import {selectAuthenticated, selectInit} from "../store/auth.ts";
import { login, supportModels } from "../conf.ts";
import {
deleteConversation,
Expand Down Expand Up @@ -282,6 +282,7 @@ function ChatWrapper() {
});
const [clearEvent, setClearEvent] = useState<() => void>(() => {});
const dispatch = useDispatch();
const init = useSelector(selectInit);
const auth = useSelector(selectAuthenticated);
const model = useSelector(selectModel);
const web = useSelector(selectWeb);
Expand All @@ -297,16 +298,23 @@ function ChatWrapper() {
clearEvent?.();
}

async function processSend(data: string, auth: boolean, model: string, web: boolean): Promise<boolean> {
const message: string = formatMessage(file, data);
if (message.length > 0 && data.trim().length > 0) {
if (await manager.send(t, auth, { message, web, model })) {
clearFile();
return true;
}
return false;
}
}

async function handleSend(auth: boolean, model: string, web: boolean) {
// because of the function wrapper, we need to update the selector state using props.
if (!target.current) return;
const el = target.current as HTMLInputElement;
const message: string = formatMessage(file, el.value);
if (message.length > 0 && el.value.trim().length > 0) {
if (await manager.send(t, auth, { message, web, model })) {
clearFile();
el.value = "";
}
if (await processSend(el.value, auth, model, web)) {
el.value = "";
}
}

Expand All @@ -315,6 +323,14 @@ function ChatWrapper() {
if (el) el.focus();
});

useEffect(() => {
if (!init) return;
const search = new URLSearchParams(window.location.search);
const query = (search.get("q") || "").trim();
if (query.length > 0) processSend(query, auth, model, web).then();
window.history.replaceState({}, "", "/");
}, [init]);

return (
<div className={`chat-container`}>
<div className={`chat-wrapper`}>
Expand Down
9 changes: 8 additions & 1 deletion app/src/store/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const authSlice = createSlice({
name: "auth",
initialState: {
token: "",
init: false,
authenticated: false,
username: "",
},
Expand All @@ -21,6 +22,9 @@ export const authSlice = createSlice({
setUsername: (state, action) => {
state.username = action.payload as string;
},
setInit: (state, action) => {
state.init = action.payload as boolean;
},
logout: (state) => {
state.token = "";
state.authenticated = false;
Expand All @@ -40,13 +44,15 @@ export function validateToken(dispatch: any, token: string, hook?: () => any) {
if (token.length === 0) {
dispatch(setAuthenticated(false));
dispatch(setUsername(""));
dispatch(setInit(true));
return;
} else
axios
.post("/state")
.then((res) => {
dispatch(setAuthenticated(res.data.status));
dispatch(setUsername(res.data.user));
dispatch(setInit(true));
hook && hook();
})
.catch((err) => {
Expand All @@ -57,7 +63,8 @@ export function validateToken(dispatch: any, token: string, hook?: () => any) {

export const selectAuthenticated = (state: any) => state.auth.authenticated;
export const selectUsername = (state: any) => state.auth.username;
export const selectInit = (state: any) => state.auth.init;

export const { setToken, setAuthenticated, setUsername, logout } =
export const { setToken, setAuthenticated, setUsername, logout, setInit } =
authSlice.actions;
export default authSlice.reducer;

0 comments on commit a378a57

Please sign in to comment.