Skip to content

Commit

Permalink
Merge pull request #186 from caorushizi/dev-ap
Browse files Browse the repository at this point in the history
feat: ✨  ap
  • Loading branch information
caorushizi authored Jun 21, 2024
2 parents 2003794 + 59d4056 commit 4b8421a
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .cspell/custom-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ acodec
adblocker
ahooks
antd
aplus
behaviour
bili
bilibili
Expand All @@ -15,6 +16,7 @@ conventionalcommits
datetime
DDTHH
execa
idtype
immer
inversify
KHTML
Expand Down
1 change: 0 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
APP_TD_APPID=
APP_CLARITY_APPID=

GH_TOKEN=
LOAD_DEVTOOLS=
Expand Down
1 change: 0 additions & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
APP_TD_APPID=
APP_CLARITY_APPID=

GH_TOKEN=
DEBUG_PLUGINS=false
2 changes: 2 additions & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
"lint-staged": "^15.2.5",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"nanoid": "^5.0.7",
"node-fetch": "^3.3.2",
"node-machine-id": "^1.1.12",
"node-pty": "^1.0.0",
"reflect-metadata": "^0.2.2",
"strip-ansi": "^7.1.0",
Expand Down
19 changes: 19 additions & 0 deletions packages/main/src/controller/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import WebviewService from "../services/WebviewService.ts";
import FavoriteRepository from "../repository/FavoriteRepository.ts";
import VideoRepository from "../repository/VideoRepository.ts";
import ConversionRepository from "../repository/ConversionRepository.ts";
import { machineId } from "node-machine-id";
import { nanoid } from "nanoid";

@injectable()
export default class HomeController implements Controller {
Expand Down Expand Up @@ -271,4 +273,21 @@ export default class HomeController implements Controller {
}
return "";
}

@handle("get-machine-id")
async getMachineId() {
try {
const id = this.store.get("machineId");
if (id) return id;
const newId = await machineId();
this.store.set("machineId", newId);
return newId;
} catch (e) {
const id = this.store.get("machineId");
if (id) return id;
const newId = nanoid();
this.store.set("machineId", newId);
return newId;
}
}
}
2 changes: 2 additions & 0 deletions packages/main/src/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ declare interface AppStore {
showTerminal: boolean;
// 隐私模式
privacy: boolean;
// 机器id
machineId: string;
}

declare interface BrowserStore {
Expand Down
3 changes: 3 additions & 0 deletions packages/main/src/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ const electronApi = {
deleteConversion(id: number): Promise<void> {
return ipcRenderer.invoke("delete-conversion", id);
},
getMachineId(): Promise<string> {
return ipcRenderer.invoke("get-machine-id");
},
};

contextBridge.exposeInMainWorld(apiKey, electronApi);
1 change: 1 addition & 0 deletions packages/main/src/vendor/ElectronStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class StoreService extends Store<AppStore> implements Vendor {
language: AppLanguage.System,
showTerminal: false,
privacy: false,
machineId: "",
},
});
}
Expand Down
9 changes: 8 additions & 1 deletion packages/renderer/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import "./App.scss";
import useElectron from "./hooks/electron";
import Loading from "./components/Loading";
import { DownloadFilter } from "./types";
import { tdApp } from "./utils";
import { useAsyncEffect } from "ahooks";

const AppLayout = lazy(() => import("./layout/App"));
const HomePage = lazy(() => import("./pages/HomePage"));
Expand All @@ -23,7 +25,7 @@ function getAlgorithm(appTheme: "dark" | "light") {
const App: FC = () => {
const dispatch = useDispatch();
const [appTheme, setAppTheme] = React.useState<"dark" | "light">("light");
const { addIpcListener, removeIpcListener } = useElectron();
const { addIpcListener, removeIpcListener, getMachineId } = useElectron();

const themeChange = (event: MediaQueryListEvent) => {
if (event.matches) {
Expand Down Expand Up @@ -52,6 +54,11 @@ const App: FC = () => {
};
}, []);

useAsyncEffect(async () => {
const deviceId = await getMachineId();
tdApp.onEvent("页面加载", { deviceId });
}, []);

useEffect(() => {
const isDarkTheme = matchMedia("(prefers-color-scheme: dark)");
isDarkTheme.addEventListener("change", themeChange);
Expand Down
5 changes: 2 additions & 3 deletions packages/renderer/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { Provider } from "react-redux";
import store from "./store";
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
import { tdApp, initClarity } from "./utils";
import { tdApp } from "./utils";
import App from "./App";
import "./i18n";

dayjs.locale("zh-cn");
tdApp.init();
initClarity();

createRoot(document.getElementById("root") as HTMLElement).render(
<StrictMode>
<Provider store={store}>
<App />
</Provider>
</StrictMode>,
</StrictMode>
);
14 changes: 0 additions & 14 deletions packages/renderer/src/utils/clarity.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/renderer/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isUrl } from "./url";

export { http } from "./http";
export { tdApp } from "./tdapp";
export { initClarity } from "./clarity";

export const requestImage = (url: string, timeout = 1000): Promise<void> => {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer/src/utils/tdapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TDEvent {
headElement.appendChild(script);
}

private onEvent(eventId: string, mapKv: Record<string, string> = {}) {
onEvent(eventId: string, mapKv: Record<string, string> = {}) {
try {
window.TDAPP?.onEvent(eventId, "", mapKv);
} catch (e) {
Expand Down
3 changes: 1 addition & 2 deletions packages/renderer/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ declare global {
onEvent: (
eventId: string,
label: "",
mapKv: Record<string, string>,
mapKv: Record<string, string>
) => void;
};
clarity?: any;
}
}

Expand Down
25 changes: 18 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4b8421a

Please sign in to comment.