Skip to content

Commit 34e03c6

Browse files
committed
refactor: move complex routing logic to wouter
1 parent 704a665 commit 34e03c6

File tree

14 files changed

+125
-37
lines changed

14 files changed

+125
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
The app works with the OpenAI API, so you need to get an API key to use it.
4343

4444
1. Get the API key [here](https://platform.openai.com/account/api-keys).
45-
2. Click the `⚙️` icon or type `:settings:` in the QuickGPT search bar
45+
2. Click the `⚙️` icon in the QuickGPT search bar
4646
3. Paste it in the `OpenAI API Key` field
4747
4. Start using it!
4848

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"react-syntax-highlighter": "15.5.0",
2323
"tauri-settings": "0.3.4",
2424
"the-new-css-reset": "1.11.2",
25+
"wouter": "2.12.1",
2526
"zustand": "4.5.0"
2627
},
2728
"devDependencies": {

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.tsx

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
import { lazy, Suspense } from "react";
2-
import { useInputStore } from "./state/inputStore";
3-
import { useGptResponseStore } from "./state/gptResponseStore";
41
import { InputBox } from "./components/InputBox";
52
import styles from "./styles.module.css";
6-
7-
const GptResponseLazy = lazy(() => import("./components/GptResponse"));
8-
const SettingsLazy = lazy(() => import("./components/Settings"));
9-
const ErrorLazy = lazy(() => import("./components/ErrorMessage"));
3+
import { Route, Router } from "wouter";
4+
import { useHashLocation } from "./hooks/useHashLocation";
5+
import { ErrorRoute, ResponseRoute, Routes, SettingsRoute } from "./Routes/routes";
106

117
export const QuickGpt = () => {
12-
const input = useInputStore(s => s.input)
13-
const [gptResponse, gptResponseError] = useGptResponseStore(s => [s.gptResponse, s.error])
14-
15-
const shouldDisplaySettings = input.match(/^:settings:$/i);
16-
const shouldDisplayGptResponse = !shouldDisplaySettings && gptResponse && !gptResponseError;
17-
188
return (
199
<div data-tauri-drag-region className={styles.container}>
2010
<InputBox />
2111

22-
{shouldDisplayGptResponse && <Suspense><GptResponseLazy /></Suspense>}
23-
{shouldDisplaySettings && <Suspense><SettingsLazy /></Suspense>}
24-
{gptResponseError && <Suspense><ErrorLazy error={gptResponseError} /></Suspense>}
12+
<Router hook={useHashLocation}>
13+
<Route path={Routes.Response}><ResponseRoute /></Route>
14+
<Route path={Routes.Settings}><SettingsRoute /></Route>
15+
<Route path={Routes.Error}><ErrorRoute /></Route>
16+
<Route></Route>
17+
</Router>
18+
2519
</div>
2620
);
2721
}

src/Routes/Error.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useEffect } from "react"
2+
import ErrorMessageComponent from "../components/ErrorMessage"
3+
import { useGptResponseStore } from "../state/gptResponseStore"
4+
import { Routes } from "./routes"
5+
import { useHashLocation } from "../hooks/useHashLocation"
6+
7+
export const ErrorRoute = () => {
8+
const error = useGptResponseStore(s => s.error)
9+
const [_, navigate] = useHashLocation()
10+
11+
useEffect(() => {
12+
if (!error) {
13+
navigate(Routes.Home)
14+
}
15+
}, [error])
16+
17+
return <ErrorMessageComponent />
18+
}

src/Routes/Response.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect } from "react"
2+
import ResponseComponent from "../components/GptResponse"
3+
import { useGptResponseStore } from "../state/gptResponseStore"
4+
import { useHashLocation } from "../hooks/useHashLocation"
5+
import { Routes } from "./routes"
6+
7+
export const ResponseRoute = () => {
8+
const [_, navigate] = useHashLocation()
9+
const [loadingResponse, gptResponse] = useGptResponseStore(s => [s.loadingResponse, s.gptResponse])
10+
const error = useGptResponseStore(s => s.error)
11+
12+
useEffect(() => {
13+
if (error) {
14+
return navigate(Routes.Error)
15+
}
16+
17+
if (!loadingResponse && !gptResponse) {
18+
navigate(Routes.Home)
19+
}
20+
}, [loadingResponse, gptResponse, error])
21+
22+
return <ResponseComponent />
23+
}

src/Routes/Settings.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import SettingsComponent from "../components/Settings"
2+
3+
export const SettingsRoute = () => {
4+
return <SettingsComponent />
5+
}

src/Routes/routes.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export { ErrorRoute } from "./Error";
2+
export { SettingsRoute } from "./Settings";
3+
export { ResponseRoute } from "./Response";
4+
5+
export const enum Routes {
6+
Home = "/",
7+
Settings = "/settings",
8+
Response = "/response",
9+
Error = "/error"
10+
}

src/components/ErrorMessage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { useGptResponseStore } from "../state/gptResponseStore";
12
import { ResultLayout } from "./ResultLayout"
23

3-
const ErrorMessage = ({ error }: { error: string }) => {
4+
const ErrorMessage = () => {
5+
const error = useGptResponseStore(s => s.error)
46
return <ResultLayout>
57
Error: {error}
68
<br />

src/components/InputBox.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@ import styles from "./inputbox.module.css"
66
import { useGptResponseStore } from "../state/gptResponseStore"
77
import { LoadingIcon } from "./LoadingIcon"
88
import { SettingsIcon } from "./SettingsIcon"
9+
import { useHashLocation } from "../hooks/useHashLocation"
10+
import { Routes } from "../Routes/routes"
911

1012
export const InputBox = () => {
13+
const [_, navigate] = useHashLocation()
1114
const inputRef = useRef<HTMLInputElement>(null)
1215
const [input, setInput] = useInputStore(s => [s.input, s.setInput])
1316
const loading = useGptResponseStore(s => s.loadingResponse)
1417

1518
useEffect(() => {
16-
listen("focus-input", () => {
17-
inputRef.current?.focus()
18-
})
19+
listen("focus-input", () => { inputRef.current?.focus() })
1920
listen("set-input", ({ payload }) => {
2021
inputRef.current?.focus()
2122
setInput(payload as string)
2223
})
2324
}, [])
2425

25-
2626
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
2727
setInput(event.target.value);
2828
}
2929

3030
const handleKeyDown = async (event: React.KeyboardEvent<HTMLInputElement>) => {
3131
if (event.key === "Enter") {
3232
askGpt(input)
33+
navigate(Routes.Response)
3334
}
3435
};
3536

@@ -43,6 +44,6 @@ export const InputBox = () => {
4344
className={styles.inputBox}
4445
value={input}
4546
/>
46-
{loading ? <LoadingIcon /> : <button className={styles.settingsIcon} onClick={() => setInput(":settings:")}><SettingsIcon /></button>}
47+
{loading ? <LoadingIcon /> : <SettingsIcon />}
4748
</div>
4849
}

0 commit comments

Comments
 (0)