Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 19, 2023
2 parents 92449b2 + ebcb2e7 commit 1cfa944
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 68 deletions.
3 changes: 1 addition & 2 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ BASE_URL=

# Specify OpenAI organization ID.(optional)
# Default: Empty
# If you do not want users to input their own API key, set this value to 1.
OPENAI_ORG_ID=

# (optional)
Expand All @@ -31,4 +30,4 @@ DISABLE_GPT4=
# (optional)
# Default: Empty
# If you do not want users to query balance, set this value to 1.
HIDE_BALANCE_QUERY=
HIDE_BALANCE_QUERY=
11 changes: 8 additions & 3 deletions app/api/cors/[...path]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ async function handle(
duplex: "half",
};

console.log("[Any Proxy]", targetUrl);
const fetchResult = await fetch(targetUrl, fetchOptions);

const fetchResult = fetch(targetUrl, fetchOptions);
console.log("[Any Proxy]", targetUrl, {
status: fetchResult.status,
statusText: fetchResult.statusText,
});

return fetchResult;
}

export const POST = handle;
export const GET = handle;
export const OPTIONS = handle;

export const runtime = "edge";
export const runtime = "nodejs";
2 changes: 1 addition & 1 deletion app/components/home.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
color: var(--black);
background-color: var(--white);
min-width: 600px;
min-height: 480px;
min-height: 370px;
max-width: 1200px;

display: flex;
Expand Down
2 changes: 1 addition & 1 deletion app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const loadAsyncGoogleFont = () => {
getClientConfig()?.buildMode === "export" ? remoteFontUrl : proxyFontUrl;
linkEl.rel = "stylesheet";
linkEl.href =
googleFontUrl + "/css2?family=Noto+Sans:wght@300;400;700;900&display=swap";
googleFontUrl + "/css2?family=" + encodeURIComponent("Noto Sans:wght@300;400;700;900") + "&display=swap";
document.head.appendChild(linkEl);
};

Expand Down
41 changes: 38 additions & 3 deletions app/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import Locale, {
} from "../locales";
import { copyToClipboard } from "../utils";
import Link from "next/link";
import { Path, RELEASE_URL, UPDATE_URL } from "../constant";
import { Path, RELEASE_URL, STORAGE_KEY, UPDATE_URL } from "../constant";
import { Prompt, SearchService, usePromptStore } from "../store/prompt";
import { ErrorBoundary } from "./error";
import { InputRange } from "./input-range";
Expand Down Expand Up @@ -275,7 +275,7 @@ function CheckButton() {

return (
<IconButton
text="检查可用性"
text={Locale.Settings.Sync.Config.Modal.Check}
bordered
onClick={check}
icon={
Expand Down Expand Up @@ -413,7 +413,42 @@ function SyncConfigModal(props: { onClose?: () => void }) {

{syncStore.provider === ProviderType.UpStash && (
<List>
<ListItem title={Locale.WIP}></ListItem>
<ListItem title={Locale.Settings.Sync.Config.UpStash.Endpoint}>
<input
type="text"
value={syncStore.upstash.endpoint}
onChange={(e) => {
syncStore.update(
(config) =>
(config.upstash.endpoint = e.currentTarget.value),
);
}}
></input>
</ListItem>

<ListItem title={Locale.Settings.Sync.Config.UpStash.UserName}>
<input
type="text"
value={syncStore.upstash.username}
placeholder={STORAGE_KEY}
onChange={(e) => {
syncStore.update(
(config) =>
(config.upstash.username = e.currentTarget.value),
);
}}
></input>
</ListItem>
<ListItem title={Locale.Settings.Sync.Config.UpStash.Password}>
<PasswordInput
value={syncStore.upstash.apiKey}
onChange={(e) => {
syncStore.update(
(config) => (config.upstash.apiKey = e.currentTarget.value),
);
}}
></PasswordInput>
</ListItem>
</List>
)}
</Modal>
Expand Down
73 changes: 46 additions & 27 deletions app/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Locale from "../locales";
import { useAppConfig, useChatStore } from "../store";

import {
DEFAULT_SIDEBAR_WIDTH,
MAX_SIDEBAR_WIDTH,
MIN_SIDEBAR_WIDTH,
NARROW_SIDEBAR_WIDTH,
Expand Down Expand Up @@ -57,53 +58,71 @@ function useDragSideBar() {

const config = useAppConfig();
const startX = useRef(0);
const startDragWidth = useRef(config.sidebarWidth ?? 300);
const startDragWidth = useRef(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
const lastUpdateTime = useRef(Date.now());

const handleMouseMove = useRef((e: MouseEvent) => {
if (Date.now() < lastUpdateTime.current + 50) {
return;
}
lastUpdateTime.current = Date.now();
const d = e.clientX - startX.current;
const nextWidth = limit(startDragWidth.current + d);
const toggleSideBar = () => {
config.update((config) => {
if (nextWidth < MIN_SIDEBAR_WIDTH) {
config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
if (config.sidebarWidth < MIN_SIDEBAR_WIDTH) {
config.sidebarWidth = DEFAULT_SIDEBAR_WIDTH;
} else {
config.sidebarWidth = nextWidth;
config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
}
});
});

const handleMouseUp = useRef(() => {
// In useRef the data is non-responsive, so `config.sidebarWidth` can't get the dynamic sidebarWidth
// startDragWidth.current = config.sidebarWidth ?? 300;
window.removeEventListener("mousemove", handleMouseMove.current);
window.removeEventListener("mouseup", handleMouseUp.current);
});
};

const onDragMouseDown = (e: MouseEvent) => {
startX.current = e.clientX;
const onDragStart = (e: MouseEvent) => {
// Remembers the initial width each time the mouse is pressed
startX.current = e.clientX;
startDragWidth.current = config.sidebarWidth;
window.addEventListener("mousemove", handleMouseMove.current);
window.addEventListener("mouseup", handleMouseUp.current);
const dragStartTime = Date.now();

const handleDragMove = (e: MouseEvent) => {
if (Date.now() < lastUpdateTime.current + 20) {
return;
}
lastUpdateTime.current = Date.now();
const d = e.clientX - startX.current;
const nextWidth = limit(startDragWidth.current + d);
config.update((config) => {
if (nextWidth < MIN_SIDEBAR_WIDTH) {
config.sidebarWidth = NARROW_SIDEBAR_WIDTH;
} else {
config.sidebarWidth = nextWidth;
}
});
};

const handleDragEnd = () => {
// In useRef the data is non-responsive, so `config.sidebarWidth` can't get the dynamic sidebarWidth
window.removeEventListener("pointermove", handleDragMove);
window.removeEventListener("pointerup", handleDragEnd);

// if user click the drag icon, should toggle the sidebar
const shouldFireClick = Date.now() - dragStartTime < 300;
if (shouldFireClick) {
toggleSideBar();
}
};

window.addEventListener("pointermove", handleDragMove);
window.addEventListener("pointerup", handleDragEnd);
};

const isMobileScreen = useMobileScreen();
const shouldNarrow =
!isMobileScreen && config.sidebarWidth < MIN_SIDEBAR_WIDTH;

useEffect(() => {
const barWidth = shouldNarrow
? NARROW_SIDEBAR_WIDTH
: limit(config.sidebarWidth ?? 300);
: limit(config.sidebarWidth ?? DEFAULT_SIDEBAR_WIDTH);
const sideBarWidth = isMobileScreen ? "100vw" : `${barWidth}px`;
document.documentElement.style.setProperty("--sidebar-width", sideBarWidth);
}, [config.sidebarWidth, isMobileScreen, shouldNarrow]);

return {
onDragMouseDown,
onDragStart,
shouldNarrow,
};
}
Expand All @@ -112,7 +131,7 @@ export function SideBar(props: { className?: string }) {
const chatStore = useChatStore();

// drag side bar
const { onDragMouseDown, shouldNarrow } = useDragSideBar();
const { onDragStart, shouldNarrow } = useDragSideBar();
const navigate = useNavigate();
const config = useAppConfig();

Expand Down Expand Up @@ -206,7 +225,7 @@ export function SideBar(props: { className?: string }) {

<div
className={styles["sidebar-drag"]}
onMouseDown={(e) => onDragMouseDown(e as any)}
onPointerDown={(e) => onDragStart(e as any)}
>
<DragIcon />
</div>
Expand Down
1 change: 1 addition & 0 deletions app/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export enum StoreKey {
Sync = "sync",
}

export const DEFAULT_SIDEBAR_WIDTH = 300;
export const MAX_SIDEBAR_WIDTH = 500;
export const MIN_SIDEBAR_WIDTH = 230;
export const NARROW_SIDEBAR_WIDTH = 100;
Expand Down
7 changes: 7 additions & 0 deletions app/locales/cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ const cn = {
Config: {
Modal: {
Title: "配置云同步",
Check: "检查可用性",
},
SyncType: {
Title: "同步类型",
Expand All @@ -206,6 +207,12 @@ const cn = {
UserName: "用户名",
Password: "密码",
},

UpStash: {
Endpoint: "UpStash Redis REST Url",
UserName: "备份名称",
Password: "UpStash Redis REST Token",
},
},

LocalState: "本地数据",
Expand Down
7 changes: 7 additions & 0 deletions app/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const en: LocaleType = {
Config: {
Modal: {
Title: "Config Sync",
Check: "Check Connection",
},
SyncType: {
Title: "Sync Type",
Expand All @@ -209,6 +210,12 @@ const en: LocaleType = {
UserName: "User Name",
Password: "Password",
},

UpStash: {
Endpoint: "UpStash Redis REST Url",
UserName: "Backup Name",
Password: "UpStash Redis REST Token",
},
},

LocalState: "Local Data",
Expand Down
9 changes: 7 additions & 2 deletions app/store/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { LLMModel } from "../client/api";
import { getClientConfig } from "../config/client";
import { DEFAULT_INPUT_TEMPLATE, DEFAULT_MODELS, StoreKey } from "../constant";
import {
DEFAULT_INPUT_TEMPLATE,
DEFAULT_MODELS,
DEFAULT_SIDEBAR_WIDTH,
StoreKey,
} from "../constant";
import { createPersistStore } from "../utils/store";

export type ModelType = (typeof DEFAULT_MODELS)[number]["name"];
Expand Down Expand Up @@ -29,7 +34,7 @@ export const DEFAULT_CONFIG = {
tightBorder: !!getClientConfig()?.isApp,
sendPreviewBubble: true,
enableAutoGenerateTitle: true,
sidebarWidth: 300,
sidebarWidth: DEFAULT_SIDEBAR_WIDTH,

disablePromptHint: false,

Expand Down
52 changes: 32 additions & 20 deletions app/store/sync.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Updater } from "../typing";
import { ApiPath, StoreKey } from "../constant";
import { ApiPath, STORAGE_KEY, StoreKey } from "../constant";
import { createPersistStore } from "../utils/store";
import {
AppState,
Expand All @@ -22,27 +22,29 @@ export interface WebDavConfig {

export type SyncStore = GetStoreState<typeof useSyncStore>;

export const useSyncStore = createPersistStore(
{
provider: ProviderType.WebDAV,
useProxy: true,
proxyUrl: corsPath(ApiPath.Cors),

webdav: {
endpoint: "",
username: "",
password: "",
},
const DEFAULT_SYNC_STATE = {
provider: ProviderType.WebDAV,
useProxy: true,
proxyUrl: corsPath(ApiPath.Cors),

upstash: {
endpoint: "",
username: "",
apiKey: "",
},
webdav: {
endpoint: "",
username: "",
password: "",
},

lastSyncTime: 0,
lastProvider: "",
upstash: {
endpoint: "",
username: STORAGE_KEY,
apiKey: "",
},

lastSyncTime: 0,
lastProvider: "",
};

export const useSyncStore = createPersistStore(
DEFAULT_SYNC_STATE,
(set, get) => ({
coundSync() {
const config = get()[get().provider];
Expand Down Expand Up @@ -108,6 +110,16 @@ export const useSyncStore = createPersistStore(
}),
{
name: StoreKey.Sync,
version: 1,
version: 1.1,

migrate(persistedState, version) {
const newState = persistedState as typeof DEFAULT_SYNC_STATE;

if (version < 1.1) {
newState.upstash.username = STORAGE_KEY;
}

return newState as any;
},
},
);
Loading

0 comments on commit 1cfa944

Please sign in to comment.