summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/server.tsx
diff options
context:
space:
mode:
authorAdam <[email protected]>2025-12-30 09:24:03 -0600
committerAdam <[email protected]>2025-12-30 09:24:03 -0600
commit55c601d13a3d9b5c72d5452d4905a6cd7570a793 (patch)
tree15ed8789e11b85958cff70ee65ad011cac1dcfa4 /packages/app/src/context/server.tsx
parent9115fac4c401e4079019b2231f1112b1a46f94bd (diff)
downloadopencode-55c601d13a3d9b5c72d5452d4905a6cd7570a793.tar.gz
opencode-55c601d13a3d9b5c72d5452d4905a6cd7570a793.zip
fix(desktop): don't hang on to dead server
Diffstat (limited to 'packages/app/src/context/server.tsx')
-rw-r--r--packages/app/src/context/server.tsx71
1 files changed, 26 insertions, 45 deletions
diff --git a/packages/app/src/context/server.tsx b/packages/app/src/context/server.tsx
index 07700ba09..991fa7aaf 100644
--- a/packages/app/src/context/server.tsx
+++ b/packages/app/src/context/server.tsx
@@ -25,28 +25,27 @@ export function serverDisplayName(url: string) {
export const { use: useServer, provider: ServerProvider } = createSimpleContext({
name: "Server",
- init: (props: { defaultUrl: string; forceUrl?: boolean }) => {
+ init: (props: { defaultUrl: string }) => {
const platform = usePlatform()
- const fallback = () => normalizeServerUrl(props.defaultUrl)
- const [forced, setForced] = createSignal(props.forceUrl ?? false)
const [store, setStore, _, ready] = persisted(
- "server.v2",
+ "server.v3",
createStore({
list: [] as string[],
- active: "",
projects: {} as Record<string, StoredProject[]>,
}),
)
+ const [active, setActiveRaw] = createSignal("")
+
function setActive(input: string) {
const url = normalizeServerUrl(input)
if (!url) return
batch(() => {
if (!store.list.includes(url)) {
- setStore("list", (list) => [url, ...list])
+ setStore("list", store.list.length, url)
}
- setStore("active", url)
+ setActiveRaw(url)
})
}
@@ -55,49 +54,31 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(
if (!url) return
const list = store.list.filter((x) => x !== url)
- const next = store.active === url ? (list[0] ?? fallback() ?? "") : store.active
+ const next = active() === url ? (list[0] ?? normalizeServerUrl(props.defaultUrl) ?? "") : active()
batch(() => {
setStore("list", list)
- setStore("active", next)
+ setActiveRaw(next)
})
}
createEffect(() => {
if (!ready()) return
-
- const url = fallback()
+ const url = normalizeServerUrl(props.defaultUrl)
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])
+ batch(() => {
+ if (!store.list.includes(url)) {
+ setStore("list", store.list.length, url)
+ }
+ setActiveRaw(url)
+ })
})
- const isReady = createMemo(() => ready() && !!store.active)
+ const isReady = createMemo(() => ready() && !!active())
const [healthy, { refetch }] = createResource(
- () => store.active || undefined,
+ () => active() || undefined,
async (url) => {
if (!url) return
@@ -114,21 +95,21 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(
)
createEffect(() => {
- if (!store.active) return
+ if (!active()) return
const interval = setInterval(() => refetch(), 10_000)
onCleanup(() => clearInterval(interval))
})
- const projectsList = createMemo(() => store.projects[store.active] ?? [])
+ const projectsList = createMemo(() => store.projects[active()] ?? [])
return {
ready: isReady,
healthy,
get url() {
- return store.active
+ return active()
},
get name() {
- return serverDisplayName(store.active)
+ return serverDisplayName(active())
},
get list() {
return store.list
@@ -139,14 +120,14 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(
projects: {
list: projectsList,
open(directory: string) {
- const url = store.active
+ const url = 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
+ const url = active()
if (!url) return
const current = store.projects[url] ?? []
setStore(
@@ -156,21 +137,21 @@ export const { use: useServer, provider: ServerProvider } = createSimpleContext(
)
},
expand(directory: string) {
- const url = store.active
+ const url = 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
+ const url = 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
+ const url = active()
if (!url) return
const current = store.projects[url] ?? []
const fromIndex = current.findIndex((x) => x.worktree === directory)