entry.tsx 3.7 KB

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