Skip to content

Commit 41458ba

Browse files
author
poulphunter
committed
remove uri parameters to use hashes
1 parent 8d4d5af commit 41458ba

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ trying to be more accessible for non-it people, more intuitive and easier to mai
1717
- Mobile, medium devices and desktop UI/UX
1818
- HTML code preview
1919
- SVG image preview
20+
- Change API URL from URL parameter (#h=https://127.0.0.1:1234)
21+
- Pre-input, pre-send message from URL (#m=message, #q=sendMessage)
2022
- Markdown / Math render (from legacy project)
2123
- Languages code highlight (from legacy project)
2224
- [Experimental] Python interpreter (from legacy project)

src/Config.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,27 @@ export const isDev = import.meta.env.MODE === 'development';
88
// ex, if the app is located at : http://localhost:5173
99
// this url : http://localhost:5173/?h=https%3A%2F%2Ftest.example.com%3A8080%2Fapi
1010
// will configure BASE_URL as https://test.example.com:8080/api
11-
export const BASE_URL =
12-
new URL(window.location.href).searchParams.get('h') ??
11+
function parseHashParams(hash: string): Record<string, string> {
12+
const parameters:Record<string, string> = {};
13+
hash.replace(/^#?/, '').split('&').forEach((param) => {
14+
const [key, value] = param.split('=');
15+
parameters[key] = decodeURIComponent(value || '');
16+
});
17+
return parameters;
18+
}
19+
20+
export const BASE_URL:string =
21+
parseHashParams(window.location.hash)['h'] ??
22+
parseHashParams(window.location.hash)['host'] ??
1323
new URL('.', document.baseURI).href.toString().replace(/\/$/, '');
24+
export const INIT_MESSAGE:string =
25+
parseHashParams(window.location.hash)['m'] ??
26+
parseHashParams(window.location.hash)['message'] ??
27+
'';
28+
export const INIT_QUERY:string =
29+
parseHashParams(window.location.hash)['q'] ??
30+
parseHashParams(window.location.hash)['query'] ??
31+
'';
1432

1533
export type CONFIG_DEFAULT_KEY = string | boolean | number | string[];
1634
export const CONFIG_DEFAULT = {

src/components/ChatScreen.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';
22
import { CallbackGeneratedChunk, useAppContext } from '../utils/app.context';
33
import ChatMessage from './ChatMessage';
44
import { CanvasType, Message, PendingMessage } from '../utils/types';
5-
import { classNames, cleanCurrentUrl, throttle } from '../utils/misc';
5+
import { classNames, throttle } from '../utils/misc';
66
import CanvasPyInterpreter from './CanvasPyInterpreter';
77
import StorageUtils from '../utils/storage';
88
import { useVSCodeContext } from '../utils/llama-vscode';
@@ -20,20 +20,21 @@ export interface MessageDisplay {
2020
}
2121

2222
/**
23-
* If the current URL contains "?m=...", prefill the message input with the value.
24-
* If the current URL contains "?q=...", prefill and SEND the message.
23+
* If the current URL contains "#m=...", prefill the message input with the value.
24+
* If the current URL contains "#q=...", prefill and SEND the message.
2525
*/
26+
let init_message_done:boolean=false;
27+
let init_query_done:boolean=false;
2628
const prefilledMsg = {
27-
content() {
28-
const url = new URL(window.location.href);
29-
return url.searchParams.get('m') ?? url.searchParams.get('q') ?? '';
29+
clear() {
30+
init_message_done = true;
31+
init_query_done = true;
3032
},
31-
shouldSend() {
32-
const url = new URL(window.location.href);
33-
return url.searchParams.has('q');
33+
content: function() {
34+
return init_message_done ? '' : INIT_MESSAGE;
3435
},
35-
clear() {
36-
cleanCurrentUrl(['m', 'q']);
36+
shouldSend: function() {
37+
return init_query_done ? '' : INIT_QUERY;
3738
},
3839
};
3940

src/utils/app.context.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from './types';
1616
import StorageUtils from './storage';
1717
import {
18-
cleanCurrentUrl,
1918
filterThoughtFromMsgs,
2019
getSSEStreamAsync,
2120
isBoolean,
@@ -113,9 +112,6 @@ export const AppContextProvider = ({
113112
}: {
114113
children: React.ReactElement;
115114
}) => {
116-
if (new URL(window.location.href).searchParams.has('h')) {
117-
cleanCurrentUrl(['h']);
118-
}
119115
const { t } = useTranslation();
120116
const { pathname } = useLocation();
121117
const navigate = useNavigate();
@@ -636,9 +632,8 @@ export const AppContextProvider = ({
636632
>(null);
637633
const [promptSelectFirstConfig, setPromptSelectFirstConfig] =
638634
useState<number>(-1);
639-
635+
// https://swapi.dev/api/planets/1/?format=json
640636
useEffect(() => {
641-
if (!promptSelectConfig) {
642637
fetch('/prompts.config.json')
643638
.then((response) => {
644639
if (!response.ok) throw new Error(response.status.toString());

src/utils/misc.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,3 @@ export const throttle = <T extends unknown[]>(
114114
}, delay);
115115
};
116116
};
117-
export const cleanCurrentUrl = (removeQueryParams: string[]) => {
118-
const url = new URL(window.location.href);
119-
removeQueryParams.forEach((param) => {
120-
url.searchParams.delete(param);
121-
});
122-
window.history.replaceState({}, '', url.toString());
123-
};

0 commit comments

Comments
 (0)