entry.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // @refresh reload
  2. import { render } from "solid-js/web"
  3. import { AppBaseProviders, AppInterface } from "@/app"
  4. import { Platform, PlatformProvider } from "@/context/platform"
  5. import { dict as en } from "@/i18n/en"
  6. import { dict as zh } from "@/i18n/zh"
  7. import pkg from "../package.json"
  8. const root = document.getElementById("root")
  9. if (import.meta.env.DEV && !(root instanceof HTMLElement)) {
  10. const locale = (() => {
  11. if (typeof navigator !== "object") return "en" as const
  12. const languages = navigator.languages?.length ? navigator.languages : [navigator.language]
  13. for (const language of languages) {
  14. if (!language) continue
  15. if (language.toLowerCase().startsWith("zh")) return "zh" as const
  16. }
  17. return "en" as const
  18. })()
  19. const key = "error.dev.rootNotFound" as const
  20. const message = locale === "zh" ? (zh[key] ?? en[key]) : en[key]
  21. throw new Error(message)
  22. }
  23. const platform: Platform = {
  24. platform: "web",
  25. version: pkg.version,
  26. openLink(url: string) {
  27. window.open(url, "_blank")
  28. },
  29. restart: async () => {
  30. window.location.reload()
  31. },
  32. notify: async (title, description, href) => {
  33. if (!("Notification" in window)) return
  34. const permission =
  35. Notification.permission === "default"
  36. ? await Notification.requestPermission().catch(() => "denied")
  37. : Notification.permission
  38. if (permission !== "granted") return
  39. const inView = document.visibilityState === "visible" && document.hasFocus()
  40. if (inView) return
  41. await Promise.resolve()
  42. .then(() => {
  43. const notification = new Notification(title, {
  44. body: description ?? "",
  45. icon: "https://opencode.ai/favicon-96x96-v3.png",
  46. })
  47. notification.onclick = () => {
  48. window.focus()
  49. if (href) {
  50. window.history.pushState(null, "", href)
  51. window.dispatchEvent(new PopStateEvent("popstate"))
  52. }
  53. notification.close()
  54. }
  55. })
  56. .catch(() => undefined)
  57. },
  58. }
  59. render(
  60. () => (
  61. <PlatformProvider value={platform}>
  62. <AppBaseProviders>
  63. <AppInterface />
  64. </AppBaseProviders>
  65. </PlatformProvider>
  66. ),
  67. root!,
  68. )