Skip to content

Commit

Permalink
1、adjust installEngine and updateYakitAndYaklang file
Browse files Browse the repository at this point in the history
2、add localEngine
  • Loading branch information
b1rdfree committed Apr 12, 2024
1 parent b454386 commit 643d7a9
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 164 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import React, {forwardRef, memo, useImperativeHandle, useMemo, useState} from "react"
import {LocalEngineProps} from "./LocalEngineType"
import {LocalGVS} from "@/enums/localGlobal"
import {getLocalValue} from "@/utils/kv"
import {useMemoizedFn} from "ahooks"
import {getRandomLocalEnginePort} from "../WelcomeConsoleUtil"
import {isCommunityEdition} from "@/utils/envfile"
import {failed, info} from "@/utils/notification"
import {YakitHint} from "@/components/yakitUI/YakitHint/YakitHint"
import {UpdateYakitAndYaklang} from "../update/UpdateYakitAndYaklang"

// import classNames from "classnames"
// import styles from "./LocalEngine.module.scss"

const {ipcRenderer} = window.require("electron")

export const LocalEngine: React.FC<LocalEngineProps> = memo(
forwardRef((props, ref) => {
const {system, setLog, onLinkEngine} = props

const [localPort, setLocalPort] = useState<number>(0)

const [currentYakit, setCurrentYakit] = useState<string>("")
const [latestYakit, setLatestYakit] = useState<string>("")
const [currentYaklang, setCurrentYaklang] = useState<string>("")
const [latestYaklang, setLatestYaklang] = useState<string>("")

/**
* 只在软件打开时|引擎从无到有时执行该逻辑
* 检查本地数据库权限
*/
const handleCheckDataBase = useMemoizedFn(() => {
const firstHint = "开始检查数据库权限是否正常"
setLog([firstHint])
let isError: boolean = false
ipcRenderer
.invoke("check-local-database")
.then((e) => {
isError = e === "not allow to write" && system !== "Windows_NT"
if (isError) {
setLog([firstHint, "数据库权限错误,开始进行调整操作(非WIN系统检查)"])
setDatabaseErrorVisible(true)
} else {
setLog([firstHint, "数据库权限无问题"])
handleLinkEnginePort()
}
})
.catch((e) => {
setLog([firstHint, `检查出错: ${e}`])
handleLinkEnginePort()
})
})

/** 获取上次本地连接引擎的端口缓存 */
const handleLinkEnginePort = useMemoizedFn(() => {
getLocalValue(LocalGVS.YaklangEnginePort)
.then((portRaw) => {
const port = parseInt(portRaw)
if (!port) {
getRandomLocalEnginePort((p) => {
onFetchLocalAndLatsVersion()
setLocalPort(p)
})
} else {
onFetchLocalAndLatsVersion()
setLocalPort(port)
}
})
.catch(() => {
getRandomLocalEnginePort((p) => {
onFetchLocalAndLatsVersion()
setLocalPort(p)
})
})
})

const onFetchLocalAndLatsVersion = useMemoizedFn(() => {
setTimeout(() => {
handleFetchYakitAndYaklangLatestVersion()
handleFetchYakitAndYaklangLocalVersion()
}, 500)
})

const handleFetchYakitAndYaklangLocalVersion = useMemoizedFn(async () => {
try {
let localYakit = (await ipcRenderer.invoke("fetch-yakit-version")) || ""
setCurrentYakit(localYakit)
} catch (error) {}

try {
setLog(["获取引擎版本号..."])
let localYaklang = (await ipcRenderer.invoke("get-current-yak")) || ""
setLog(["获取引擎版本号...", `引擎版本号——${localYaklang}`, "准备开始本地连接中"])
setCurrentYaklang(localYaklang)
setTimeout(() => {
// 这里的2秒是判断是否有更新弹窗出现
handleLinkLocalEnging()
}, 2000)
} catch (error) {}
})

const handleFetchYakitAndYaklangLatestVersion = useMemoizedFn(() => {
if (!isCommunityEdition()) return
getLocalValue(LocalGVS.NoAutobootLatestVersionCheck).then((val: boolean) => {
ipcRenderer
.invoke("fetch-latest-yakit-version")
.then((data: string) => {
if (!val) {
setLatestYakit(data || "")
}
})
.catch((err) => {})
ipcRenderer
.invoke("fetch-latest-yaklang-version")
.then((data: string) => {
if (!val) setLatestYaklang(data || "")
})
.catch((err) => {})
})
})

// 初始化后的本地连接-前置项检查
const initLink = useMemoizedFn(() => {
handleCheckDataBase()
})

useImperativeHandle(
ref,
() => ({
init: initLink,
link: () => {}
}),
[]
)

// 开始进行本地引擎连接
const handleLinkLocalEnging = useMemoizedFn(() => {
if (isShowUpdate) return
// 开始连接本地引擎
onLinkEngine(localPort)
})

/** ---------- 软件自启的更新检测弹框 Start ---------- */
const isShowUpdate = useMemo(() => {
if (!isCommunityEdition()) return false

if (!!currentYakit && !!latestYakit && `v${currentYakit}` !== latestYakit) {
return true
}
if (!!currentYaklang && !!latestYaklang && currentYaklang !== latestYaklang) {
return true
}

return false
}, [currentYakit, latestYakit, currentYaklang, latestYaklang])

const onCancelUpdateHint = useMemoizedFn(() => {
handleLinkLocalEnging()
})
/** ---------- 软件自启的更新检测弹框 End ---------- */

/** ---------- 数据库权限逻辑 Start ---------- */
const [databaseErrorVisible, setDatabaseErrorVisible] = useState<boolean>(false)
const [databaseErrorLoading, setDatabaseErrorLoading] = useState<boolean>(false)
const onFixDatabaseError = useMemoizedFn(() => {
setDatabaseErrorLoading(true)
ipcRenderer
.invoke("fix-local-database")
.then((e) => {
info("修复成功")
})
.catch((e) => {
failed(`修复数据库权限错误:${e}`)
})
.finally(() => {
setTimeout(() => {
setDatabaseErrorVisible(false)
setDatabaseErrorLoading(false)
handleLinkEnginePort()
}, 300)
})
})
/** ---------- 数据库权限逻辑 End ---------- */

return (
<>
{isCommunityEdition() && (
<UpdateYakitAndYaklang
currentYakit={currentYakit}
latestYakit={latestYakit}
setLatestYakit={setLatestYakit}
currentYaklang={currentYaklang}
latestYaklang={latestYaklang}
setLatestYaklang={setLatestYaklang}
isShow={isShowUpdate}
onCancel={onCancelUpdateHint}
/>
)}
{databaseErrorVisible && (
<YakitHint
getContainer={document.getElementById("yakit-uilayout-body") || undefined}
mask={false}
isDrag={true}
visible={databaseErrorVisible}
title='yaklang 数据库错误'
content='尝试修复数据库写权限(可能要求 ROOT 权限)'
okButtonText='立即修复'
okButtonProps={{loading: databaseErrorLoading}}
cancelButtonProps={{style: {display: "none"}}}
onOk={onFixDatabaseError}
/>
)}
</>
)
})
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from "react"
import {YakitSystem} from "@/yakitGVDefine"

export interface LocalEngineProps {
ref?: React.ForwardedRef<LocalEngineLinkFuncProps>
system: YakitSystem
setLog: (log: string[]) => any
onLinkEngine: (port: number) => any
}

export interface LocalEngineLinkFuncProps {
/** 初始化并检查所有前置项后的本地连接 */
init: () => any
/** 检查引擎版本后的本地连接 */
link: () => any
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.relaunch-hint {
height: 80px;
padding: 24px;
display: flex;
align-items: center;
}

.install-engine-mask {
position: absolute;
top: 0;
Expand All @@ -21,7 +28,7 @@

.install-update-modal {
width: 448px;
z-index: 99;
z-index: 100;
}
.engine-hint-modal-wrapper {
position: absolute;
Expand Down Expand Up @@ -148,11 +155,11 @@
font-size: 11px;
color: var(--yakit-primary-5);
.copy-icon {
width: 12px;
height: 12px;
display: flex;
margin-left: 8px;
cursor: pointer;
svg {
width: 12px;
height: 12px;
}
}
}
}
Expand Down Expand Up @@ -276,7 +283,7 @@
left: calc(50% - 348px);
}
.info-modal-wrapper {
z-index: 99;
z-index: 100;
position: absolute;
top: 30%;
}
Expand Down
Loading

0 comments on commit 643d7a9

Please sign in to comment.