summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/server.tsx
blob: 07700ba09dcf5319d941fbb8918c69812d78bf02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { createOpencodeClient } from "@opencode-ai/sdk/v2/client"
import { createSimpleContext } from "@opencode-ai/ui/context"
import { batch, createEffect, createMemo, createResource, createSignal, onCleanup } from "solid-js"
import { createStore } from "solid-js/store"
import { usePlatform } from "@/context/platform"
import { persisted } from "@/utils/persist"

type StoredProject = { worktree: string; expanded: boolean }

export function normalizeServerUrl(input: string) {
  const trimmed = input.trim()
  if (!trimmed) return
  const withProtocol = /^https?:\/\//.test(trimmed) ? trimmed : `http://${trimmed}`
  const cleaned = withProtocol.replace(/\/+$/, "")
  return cleaned.replace(/^(https?:\/\/[^/]+).*/, "$1")
}

export function serverDisplayName(url: string) {
  if (!url) return ""
  return url
    .replace(/^https?:\/\//, "")
    .replace(/\/+$/, "")
    .split("/")[0]
}

export const { use: useServer, provider: ServerProvider } = createSimpleContext({
  name: "Server",
  init: (props: { defaultUrl: string; forceUrl?: boolean }) => {
    const platform = usePlatform()
    const fallback = () => normalizeServerUrl(props.defaultUrl)
    const [forced, setForced] = createSignal(props.forceUrl ?? false)

    const [store, setStore, _, ready] = persisted(
      "server.v2",
      createStore({
        list: [] as string[],
        active: "",
        projects: {} as Record<string, StoredProject[]>,
      }),
    )

    function setActive(input: string) {
      const url = normalizeServerUrl(input)
      if (!url) return
      batch(() => {
        if (!store.list.includes(url)) {
          setStore("list", (list) => [url, ...list])
        }
        setStore("active", url)
      })
    }

    function remove(input: string) {
      const url = normalizeServerUrl(input)
      if (!url) return

      const list = store.list.filter((x) => x !== url)
      const next = store.active === url ? (list[0] ?? fallback() ?? "") : store.active

      batch(() => {
        setStore("list", list)
        setStore("active", next)
      })
    }

    createEffect(() => {
      if (!ready()) return

      const url = fallback()
      if (!url) return

      if (forced()) {
        batch(() => {
          if (!store.list.includes(url)) {
            setStore("list", (list) => [url, ...list])
          }
          if (store.active !== url) {
            setStore("active", url)
          }
        })
        setForced(false)
        return
      }

      if (store.list.length === 0) {
        batch(() => {
          setStore("list", [url])
          setStore("active", url)
        })
        return
      }

      if (store.active && store.list.includes(store.active)) return
      setStore("active", store.list[0])
    })

    const isReady = createMemo(() => ready() && !!store.active)

    const [healthy, { refetch }] = createResource(
      () => store.active || undefined,
      async (url) => {
        if (!url) return

        const sdk = createOpencodeClient({
          baseUrl: url,
          fetch: platform.fetch,
          signal: AbortSignal.timeout(2000),
        })
        return sdk.global
          .health()
          .then((x) => x.data?.healthy === true)
          .catch(() => false)
      },
    )

    createEffect(() => {
      if (!store.active) return
      const interval = setInterval(() => refetch(), 10_000)
      onCleanup(() => clearInterval(interval))
    })

    const projectsList = createMemo(() => store.projects[store.active] ?? [])

    return {
      ready: isReady,
      healthy,
      get url() {
        return store.active
      },
      get name() {
        return serverDisplayName(store.active)
      },
      get list() {
        return store.list
      },
      setActive,
      add: setActive,
      remove,
      projects: {
        list: projectsList,
        open(directory: string) {
          const url = store.active
          if (!url) return
          const current = store.projects[url] ?? []
          if (current.find((x) => x.worktree === directory)) return
          setStore("projects", url, [{ worktree: directory, expanded: true }, ...current])
        },
        close(directory: string) {
          const url = store.active
          if (!url) return
          const current = store.projects[url] ?? []
          setStore(
            "projects",
            url,
            current.filter((x) => x.worktree !== directory),
          )
        },
        expand(directory: string) {
          const url = store.active
          if (!url) return
          const current = store.projects[url] ?? []
          const index = current.findIndex((x) => x.worktree === directory)
          if (index !== -1) setStore("projects", url, index, "expanded", true)
        },
        collapse(directory: string) {
          const url = store.active
          if (!url) return
          const current = store.projects[url] ?? []
          const index = current.findIndex((x) => x.worktree === directory)
          if (index !== -1) setStore("projects", url, index, "expanded", false)
        },
        move(directory: string, toIndex: number) {
          const url = store.active
          if (!url) return
          const current = store.projects[url] ?? []
          const fromIndex = current.findIndex((x) => x.worktree === directory)
          if (fromIndex === -1 || fromIndex === toIndex) return
          const result = [...current]
          const [item] = result.splice(fromIndex, 1)
          result.splice(toIndex, 0, item)
          setStore("projects", url, result)
        },
      },
    }
  },
})