Skip to content

Commit 57b2a20

Browse files
authored
fix: load app & web utils (#337)
1 parent 59622a7 commit 57b2a20

9 files changed

Lines changed: 69 additions & 38 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"headlessui",
1616
"Icdbb",
1717
"icns",
18+
"iconfont",
1819
"INFINI",
1920
"infinilabs",
2021
"inputbox",

src/components/Assistant/ChatHeader.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
PopoverPanel,
1818
} from "@headlessui/react";
1919
import { useTranslation } from "react-i18next";
20+
import clsx from "clsx";
2021

2122
import logoImg from "@/assets/icon.svg";
2223
import HistoryIcon from "@/icons/History";
@@ -32,8 +33,6 @@ import platformAdapter from "@/utils/platformAdapter";
3233
import { list_coco_servers } from "@/commands";
3334
import VisibleKey from "../Common/VisibleKey";
3435
import { useShortcutsStore } from "@/stores/shortcutsStore";
35-
import { useBoolean } from "ahooks";
36-
import clsx from "clsx";
3736

3837
interface ChatHeaderProps {
3938
onCreateNewChat: () => void;

src/components/AudioRecording/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { useAppStore } from "@/stores/appStore";
2-
import { useKeyPress, useReactive } from "ahooks";
1+
import { useReactive } from "ahooks";
32
import clsx from "clsx";
43
import { Check, Loader, Mic, X } from "lucide-react";
54
import { FC, useEffect, useRef } from "react";
@@ -9,10 +8,12 @@ import {
98
} from "tauri-plugin-macos-permissions-api";
109
import { useWavesurfer } from "@wavesurfer/react";
1110
import RecordPlugin from "wavesurfer.js/dist/plugins/record.esm.js";
11+
1212
import { useConnectStore } from "@/stores/connectStore";
1313
import { useShortcutsStore } from "@/stores/shortcutsStore";
1414
import { transcription } from "@/commands";
15-
import VisibleKey from "../Common/VisibleKey";
15+
import VisibleKey from "@/components/Common/VisibleKey";
16+
import { useAppStore } from "@/stores/appStore";
1617

1718
interface AudioRecordingProps {
1819
onChange?: (text: string) => void;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useIconfontScript } from "@/hooks/useScript";
2+
3+
interface FontIconProps {
4+
name: string;
5+
className?: string;
6+
style?: React.CSSProperties;
7+
}
8+
9+
const FontIcon = ({ name, className, style, ...rest }: FontIconProps) => {
10+
useIconfontScript();
11+
return (
12+
<svg className={`icon ${className || ""}`} style={style} {...rest}>
13+
<use xlinkHref={`#${name}`} />
14+
</svg>
15+
);
16+
};
17+
18+
export default FontIcon;

src/components/Search/InputBox.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { useSearchStore } from "@/stores/searchStore";
1313
import { metaOrCtrlKey } from "@/utils/keyboardUtils";
1414
import SearchPopover from "./SearchPopover";
1515
// import AudioRecording from "../AudioRecording";
16-
import { hide_coco } from "@/commands";
1716
import { DataSource } from "@/types/commands";
1817
// import InputExtra from "./InputExtra";
1918
// import { useConnectStore } from "@/stores/connectStore";

src/hooks/useScript.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useEffect } from 'react';
2+
3+
const useScript = (src: string) => {
4+
useEffect(() => {
5+
if (document.querySelector(`script[src="${src}"]`)) {
6+
return; // Prevent duplicate script loading
7+
}
8+
9+
const script = document.createElement('script');
10+
script.src = src;
11+
script.async = true;
12+
document.body.appendChild(script);
13+
14+
return () => {
15+
document.body.removeChild(script);
16+
};
17+
}, [src]);
18+
};
19+
20+
export default useScript;
21+
22+
23+
export const useIconfontScript = () => {
24+
useScript('/assets/fonts/icons/iconfont.js');
25+
};

src/main.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
.input-body {
9999
@apply rounded-xl overflow-hidden;
100100
}
101+
102+
.icon {
103+
width: 1em;
104+
height: 1em;
105+
vertical-align: -0.15em;
106+
fill: currentColor;
107+
overflow: hidden;
108+
}
101109
}
102110

103111
/* Component styles */

src/utils/platformAdapter.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createWebAdapter } from './webAdapter';
2+
import { createTauriAdapter } from './tauriAdapter';
23

34
import { IShortcutsStore } from "@/stores/shortcutsStore";
45
import { IStartupStore } from "@/stores/startupStore";
@@ -91,38 +92,9 @@ export interface PlatformAdapter {
9192
openExternal: (url: string) => Promise<void>;
9293
}
9394

94-
let adapter: PlatformAdapter | null = null;
95+
const appStore = JSON.parse(localStorage.getItem("app-store") || "{}");
96+
const isTauri = appStore.state?.isTauri ?? !!(window as any).__TAURI__;
9597

96-
const loadAdapter = () => {
97-
if (adapter) return adapter;
98-
99-
try {
100-
const appStore = JSON.parse(localStorage.getItem("app-store") || "{}");
101-
const isTauri = appStore.state?.isTauri ?? !!(window as any).__TAURI__;
102-
103-
if (isTauri) {
104-
const { createTauriAdapter } = require('./tauriAdapter');
105-
adapter = createTauriAdapter();
106-
} else {
107-
adapter = createWebAdapter();
108-
}
109-
} catch (error) {
110-
console.error('Failed to load adapter:', error);
111-
adapter = createWebAdapter();
112-
}
113-
114-
return adapter;
115-
};
116-
117-
// Use proxy to ensure lazy loading and singleton pattern
118-
const platformAdapter = new Proxy({} as PlatformAdapter, {
119-
get: (_target, prop) => {
120-
const currentAdapter = loadAdapter();
121-
if (!currentAdapter) {
122-
throw new Error('Platform adapter not initialized');
123-
}
124-
return currentAdapter[prop as keyof PlatformAdapter];
125-
}
126-
});
98+
const platformAdapter: PlatformAdapter = isTauri ? createTauriAdapter() : createWebAdapter();
12799

128100
export default platformAdapter;

src/web.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ body,
158158
border-radius: 0.75rem;
159159
}
160160

161+
.icon {
162+
width: 1em;
163+
height: 1em;
164+
vertical-align: -0.15em;
165+
fill: currentColor;
166+
overflow: hidden;
167+
}
168+
161169
.\!container {
162170
width: 100% !important;
163171
}

0 commit comments

Comments
 (0)