-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web):i18n sync with sealos desktop (#18)
- Loading branch information
Showing
4 changed files
with
146 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// components/SealosProvider.tsx | ||
import { useEffect } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { EVENT_NAME } from "@zjy365/sealos-desktop-sdk"; | ||
import { SessionV1 } from "@zjy365/sealos-desktop-sdk/*"; | ||
import { createSealosApp, sealosApp } from "@zjy365/sealos-desktop-sdk/app"; | ||
|
||
import useSessionStore from "@/pages/auth/store"; | ||
|
||
const InitSealosApp = () => { | ||
const { i18n } = useTranslation(); | ||
const { setSession } = useSessionStore(); | ||
|
||
const handleI18nChange = (data: { currentLanguage: string }) => { | ||
const currentLng = i18n.resolvedLanguage; | ||
const newLng = data.currentLanguage; | ||
if (currentLng !== newLng) { | ||
i18n.changeLanguage(newLng); | ||
} | ||
}; | ||
|
||
const initLang = async () => { | ||
const lang = await sealosApp.getLanguage(); | ||
const lng = lang.lng; | ||
if (i18n.resolvedLanguage !== lng) { | ||
i18n.changeLanguage(lng); | ||
} | ||
}; | ||
|
||
const initApp = async () => { | ||
const sealosUser = import.meta.env.VITE_SEALOS_MOCK_USER; | ||
const sealosKc = import.meta.env.VITE_SEALOS_MOCK_KC; | ||
|
||
if (sealosUser && sealosKc) { | ||
const testSession: SessionV1 = { | ||
user: { | ||
id: "", | ||
name: sealosUser, | ||
avatar: "", | ||
k8sUsername: "", | ||
nsid: "", | ||
}, | ||
kubeconfig: sealosKc, | ||
}; | ||
setSession(testSession); | ||
return; | ||
} | ||
|
||
const result = await sealosApp.getSession(); | ||
|
||
setSession(result); | ||
}; | ||
|
||
useEffect(() => { | ||
const cleanupApp = createSealosApp(); | ||
let cleanupEventListener: (() => void) | undefined; | ||
|
||
// handle iframe focus | ||
const handleIframeFocus = () => { | ||
const iframes = document.querySelectorAll("iframe"); | ||
iframes.forEach((iframe) => { | ||
iframe.style.pointerEvents = "auto"; | ||
}); | ||
}; | ||
|
||
// handle iframe visibility change | ||
const handleVisibilityChange = () => { | ||
if (!document.hidden) { | ||
handleIframeFocus(); | ||
} | ||
}; | ||
|
||
const setup = async () => { | ||
try { | ||
window.addEventListener("blur", handleIframeFocus); | ||
document.addEventListener("visibilitychange", handleVisibilityChange); | ||
|
||
cleanupEventListener = sealosApp?.addAppEventListen( | ||
EVENT_NAME.CHANGE_I18N, | ||
handleI18nChange, | ||
); | ||
|
||
handleIframeFocus(); | ||
|
||
await initLang(); | ||
await initApp(); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
// eslint-disable-next-line no-console | ||
console.error("Sealos app init error:", error.message); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.error("Sealos app init error:", error); | ||
} | ||
} | ||
}; | ||
|
||
setup().finally(() => { | ||
// eslint-disable-next-line no-console | ||
console.info("##### sealos app and sealos info init success #####"); | ||
}); | ||
|
||
return () => { | ||
window.removeEventListener("blur", handleIframeFocus); | ||
document.removeEventListener("visibilitychange", handleVisibilityChange); | ||
|
||
if (cleanupEventListener && typeof cleanupEventListener === "function") { | ||
cleanupEventListener(); | ||
} | ||
if (cleanupApp && typeof cleanupApp === "function") { | ||
cleanupApp(); | ||
} | ||
}; | ||
}, []); | ||
|
||
return null; | ||
}; | ||
|
||
export default InitSealosApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters