index.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // @refresh reload
  2. import { render } from "solid-js/web"
  3. import { App, PlatformProvider, Platform } from "@opencode-ai/app"
  4. import { open, save } from "@tauri-apps/plugin-dialog"
  5. import { open as shellOpen } from "@tauri-apps/plugin-shell"
  6. import { type as ostype } from "@tauri-apps/plugin-os"
  7. import { AsyncStorage } from "@solid-primitives/storage"
  8. import { fetch as tauriFetch } from "@tauri-apps/plugin-http"
  9. import { Store } from "@tauri-apps/plugin-store"
  10. import { UPDATER_ENABLED } from "./updater"
  11. import { createMenu } from "./menu"
  12. import { check, Update } from "@tauri-apps/plugin-updater"
  13. import { invoke } from "@tauri-apps/api/core"
  14. import { getCurrentWindow } from "@tauri-apps/api/window"
  15. import { isPermissionGranted, requestPermission } from "@tauri-apps/plugin-notification"
  16. import { relaunch } from "@tauri-apps/plugin-process"
  17. import pkg from "../package.json"
  18. const root = document.getElementById("root")
  19. if (import.meta.env.DEV && !(root instanceof HTMLElement)) {
  20. throw new Error(
  21. "Root element not found. Did you forget to add it to your index.html? Or maybe the id attribute got misspelled?",
  22. )
  23. }
  24. let update: Update | null = null
  25. const platform: Platform = {
  26. platform: "tauri",
  27. version: pkg.version,
  28. async openDirectoryPickerDialog(opts) {
  29. const result = await open({
  30. directory: true,
  31. multiple: opts?.multiple ?? false,
  32. title: opts?.title ?? "Choose a folder",
  33. })
  34. return result
  35. },
  36. async openFilePickerDialog(opts) {
  37. const result = await open({
  38. directory: false,
  39. multiple: opts?.multiple ?? false,
  40. title: opts?.title ?? "Choose a file",
  41. })
  42. return result
  43. },
  44. async saveFilePickerDialog(opts) {
  45. const result = await save({
  46. title: opts?.title ?? "Save file",
  47. defaultPath: opts?.defaultPath,
  48. })
  49. return result
  50. },
  51. openLink(url: string) {
  52. shellOpen(url)
  53. },
  54. storage: (name = "default.dat") => {
  55. const api: AsyncStorage = {
  56. _store: null,
  57. _getStore: async () => api._store || (api._store = Store.load(name)),
  58. getItem: async (key: string) => (await (await api._getStore()).get(key)) ?? null,
  59. setItem: async (key: string, value: string) => await (await api._getStore()).set(key, value),
  60. removeItem: async (key: string) => await (await api._getStore()).delete(key),
  61. clear: async () => await (await api._getStore()).clear(),
  62. key: async (index: number) => (await (await api._getStore()).keys())[index],
  63. getLength: async () => (await api._getStore()).length(),
  64. get length() {
  65. return api.getLength()
  66. },
  67. }
  68. return api
  69. },
  70. checkUpdate: async () => {
  71. if (!UPDATER_ENABLED) return { updateAvailable: false }
  72. update = await check()
  73. if (!update) return { updateAvailable: false }
  74. await update.download()
  75. return { updateAvailable: true, version: update.version }
  76. },
  77. update: async () => {
  78. if (!UPDATER_ENABLED || !update) return
  79. if (ostype() === "windows") await invoke("kill_sidecar")
  80. await update.install()
  81. },
  82. restart: async () => {
  83. await invoke("kill_sidecar")
  84. await relaunch()
  85. },
  86. notify: async (title, description, href) => {
  87. const granted = await isPermissionGranted().catch(() => false)
  88. const permission = granted ? "granted" : await requestPermission().catch(() => "denied")
  89. if (permission !== "granted") return
  90. const win = getCurrentWindow()
  91. const focused = await win.isFocused().catch(() => document.hasFocus())
  92. if (focused) return
  93. await Promise.resolve()
  94. .then(() => {
  95. const notification = new Notification(title, { body: description ?? "" })
  96. notification.onclick = () => {
  97. const win = getCurrentWindow()
  98. void win.show().catch(() => undefined)
  99. void win.unminimize().catch(() => undefined)
  100. void win.setFocus().catch(() => undefined)
  101. if (href) {
  102. window.history.pushState(null, "", href)
  103. window.dispatchEvent(new PopStateEvent("popstate"))
  104. }
  105. notification.close()
  106. }
  107. })
  108. .catch(() => undefined)
  109. },
  110. // @ts-expect-error
  111. fetch: tauriFetch,
  112. }
  113. createMenu()
  114. // Stops mousewheel events from reaching Tauri's pinch-to-zoom handler
  115. root?.addEventListener("mousewheel", (e) => {
  116. e.stopPropagation()
  117. })
  118. render(() => {
  119. return (
  120. <PlatformProvider value={platform}>
  121. {ostype() === "macos" && (
  122. <div class="bg-background-base border-b border-border-weak-base h-8" data-tauri-drag-region />
  123. )}
  124. <App />
  125. </PlatformProvider>
  126. )
  127. }, root!)