entry.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // @refresh reload
  2. import { render } from "solid-js/web"
  3. import { AppBaseProviders, AppInterface } from "@/app"
  4. import { type Platform, PlatformProvider } from "@/context/platform"
  5. import { dict as en } from "@/i18n/en"
  6. import { dict as zh } from "@/i18n/zh"
  7. import { handleNotificationClick } from "@/utils/notification-click"
  8. import pkg from "../package.json"
  9. import { ServerConnection } from "./context/server"
  10. const DEFAULT_SERVER_URL_KEY = "opencode.settings.dat:defaultServerUrl"
  11. const getLocale = () => {
  12. if (typeof navigator !== "object") return "en" as const
  13. const languages = navigator.languages?.length ? navigator.languages : [navigator.language]
  14. for (const language of languages) {
  15. if (!language) continue
  16. if (language.toLowerCase().startsWith("zh")) return "zh" as const
  17. }
  18. return "en" as const
  19. }
  20. const getRootNotFoundError = () => {
  21. const key = "error.dev.rootNotFound" as const
  22. const locale = getLocale()
  23. return locale === "zh" ? (zh[key] ?? en[key]) : en[key]
  24. }
  25. const getStorage = (key: string) => {
  26. if (typeof localStorage === "undefined") return null
  27. try {
  28. return localStorage.getItem(key)
  29. } catch {
  30. return null
  31. }
  32. }
  33. const setStorage = (key: string, value: string | null) => {
  34. if (typeof localStorage === "undefined") return
  35. try {
  36. if (value !== null) {
  37. localStorage.setItem(key, value)
  38. return
  39. }
  40. localStorage.removeItem(key)
  41. } catch {
  42. return
  43. }
  44. }
  45. const readDefaultServerUrl = () => getStorage(DEFAULT_SERVER_URL_KEY)
  46. const writeDefaultServerUrl = (url: string | null) => setStorage(DEFAULT_SERVER_URL_KEY, url)
  47. const notify: Platform["notify"] = async (title, description, href) => {
  48. if (!("Notification" in window)) return
  49. const permission =
  50. Notification.permission === "default"
  51. ? await Notification.requestPermission().catch(() => "denied")
  52. : Notification.permission
  53. if (permission !== "granted") return
  54. const inView = document.visibilityState === "visible" && document.hasFocus()
  55. if (inView) return
  56. const notification = new Notification(title, {
  57. body: description ?? "",
  58. icon: "https://opencode.ai/favicon-96x96-v3.png",
  59. })
  60. notification.onclick = () => {
  61. handleNotificationClick(href)
  62. notification.close()
  63. }
  64. }
  65. const openLink: Platform["openLink"] = (url) => {
  66. window.open(url, "_blank")
  67. }
  68. const back: Platform["back"] = () => {
  69. window.history.back()
  70. }
  71. const forward: Platform["forward"] = () => {
  72. window.history.forward()
  73. }
  74. const restart: Platform["restart"] = async () => {
  75. window.location.reload()
  76. }
  77. const root = document.getElementById("root")
  78. if (!(root instanceof HTMLElement) && import.meta.env.DEV) {
  79. throw new Error(getRootNotFoundError())
  80. }
  81. const getCurrentUrl = () => {
  82. if (location.hostname.includes("opencode.ai")) return "http://localhost:4096"
  83. if (import.meta.env.DEV)
  84. return `http://${import.meta.env.VITE_OPENCODE_SERVER_HOST ?? "localhost"}:${import.meta.env.VITE_OPENCODE_SERVER_PORT ?? "4096"}`
  85. return location.origin
  86. }
  87. const getDefaultUrl = () => {
  88. const lsDefault = readDefaultServerUrl()
  89. if (lsDefault) return lsDefault
  90. return getCurrentUrl()
  91. }
  92. const platform: Platform = {
  93. platform: "web",
  94. version: pkg.version,
  95. openLink,
  96. back,
  97. forward,
  98. restart,
  99. notify,
  100. getDefaultServer: async () => {
  101. const stored = readDefaultServerUrl()
  102. return stored ? ServerConnection.Key.make(stored) : null
  103. },
  104. setDefaultServer: writeDefaultServerUrl,
  105. }
  106. if (root instanceof HTMLElement) {
  107. const server: ServerConnection.Http = { type: "http", http: { url: getCurrentUrl() } }
  108. render(
  109. () => (
  110. <PlatformProvider value={platform}>
  111. <AppBaseProviders>
  112. <AppInterface
  113. defaultServer={ServerConnection.Key.make(getDefaultUrl())}
  114. servers={[server]}
  115. disableHealthCheck
  116. />
  117. </AppBaseProviders>
  118. </PlatformProvider>
  119. ),
  120. root,
  121. )
  122. }