Skip to content

Commit f2015bc

Browse files
Clean up cloud controller
1 parent 3d6d55f commit f2015bc

1 file changed

Lines changed: 56 additions & 63 deletions

File tree

src/cloud/cloudController.ts

Lines changed: 56 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as vscode from "vscode"
2+
import { log } from "../utils/logger"
23
import {
34
trackCloudAppOpened,
45
trackCloudDashboardOpened,
@@ -79,56 +80,51 @@ export class CloudController {
7980
return
8081
}
8182

82-
if (this.workspaceRoot) {
83-
const config = await this.configService.getConfig(this.workspaceRoot)
84-
85-
if (!config) {
86-
console.log(
87-
"[FastAPI Cloud] No config found at",
88-
this.workspaceRoot.toString(),
89-
)
90-
this.hasConfig = false
91-
this.currentApp = null
92-
this.currentTeam = null
93-
this.statusBarItem.text = "$(cloud) Set up FastAPI Cloud"
94-
return
95-
}
83+
const config = await this.configService.getConfig(this.workspaceRoot)
84+
85+
if (!config) {
86+
log(`No config found at ${this.workspaceRoot.toString()}`)
87+
this.hasConfig = false
88+
this.currentApp = null
89+
this.currentTeam = null
90+
this.statusBarItem.text = "$(cloud) Set up FastAPI Cloud"
91+
return
92+
}
9693

97-
this.hasConfig = true
94+
this.hasConfig = true
9895

99-
try {
100-
this.currentApp = await this.apiService.getApp(config.app_id)
101-
this.currentTeam = await this.apiService.getTeam(config.team_id)
96+
try {
97+
this.currentApp = await this.apiService.getApp(config.app_id)
98+
this.currentTeam = await this.apiService.getTeam(config.team_id)
10299

103-
if (this.currentApp) {
104-
this.statusBarItem.text = `$(cloud) ${this.currentApp.slug}`
105-
}
106-
} catch (err) {
107-
console.error("[FastAPI Cloud] Failed to fetch app/team:", err)
108-
this.currentApp = null
109-
this.currentTeam = null
110-
111-
const is404 =
112-
err instanceof Error && err.message.includes("returned 404")
113-
114-
if (is404) {
115-
this.statusBarItem.text = "$(warning) FastAPI Cloud"
116-
if (!this.appNotFoundWarningShown) {
117-
this.appNotFoundWarningShown = true
118-
vscode.window
119-
.showWarningMessage(
120-
"This project is linked to a FastAPI Cloud app that could not be found. You may need to unlink and relink it.",
121-
"Unlink",
122-
)
123-
.then((selected) => {
124-
if (selected === "Unlink") this.unlinkProject()
125-
})
126-
}
127-
} else {
128-
// Transient error (network, 500, etc.) — don't bother the user,
129-
// next refresh will retry automatically
130-
this.statusBarItem.text = "$(cloud) Set up FastAPI Cloud"
100+
if (this.currentApp) {
101+
this.statusBarItem.text = `$(cloud) ${this.currentApp.slug}`
102+
}
103+
} catch (err) {
104+
log(`Failed to fetch app/team: ${err}`)
105+
this.currentApp = null
106+
this.currentTeam = null
107+
108+
const is404 =
109+
err instanceof Error && err.message.includes("returned 404")
110+
111+
if (is404) {
112+
this.statusBarItem.text = "$(warning) FastAPI Cloud"
113+
if (!this.appNotFoundWarningShown) {
114+
this.appNotFoundWarningShown = true
115+
vscode.window
116+
.showWarningMessage(
117+
"This project is linked to a FastAPI Cloud app that could not be found. You may need to unlink and relink it.",
118+
"Unlink",
119+
)
120+
.then((selected) => {
121+
if (selected === "Unlink") this.unlinkProject()
122+
})
131123
}
124+
} else {
125+
// Transient error (network, 500, etc.) — don't bother the user,
126+
// next refresh will retry automatically
127+
this.statusBarItem.text = "$(cloud) Set up FastAPI Cloud"
132128
}
133129
}
134130
} finally {
@@ -195,7 +191,8 @@ export class CloudController {
195191
}
196192

197193
private async showAppMenu() {
198-
const app = this.currentApp!
194+
if (!this.currentApp) return
195+
const app = this.currentApp
199196
const dashboardUrl = this.currentTeam
200197
? ApiService.getDashboardUrl(this.currentTeam.slug, app.slug)
201198
: undefined
@@ -238,6 +235,16 @@ export class CloudController {
238235
}
239236
}
240237

238+
private async saveLink(app: App, team: Team) {
239+
await this.configService.writeConfig(this.workspaceRoot!, {
240+
app_id: app.id,
241+
team_id: team.id,
242+
})
243+
trackCloudProjectLinked(app.slug)
244+
vscode.window.showInformationMessage(`Linked to ${app.slug}`)
245+
await this.refresh()
246+
}
247+
241248
async createAndLinkProject() {
242249
if (!this.workspaceRoot) {
243250
vscode.window.showErrorMessage("No workspace folder open")
@@ -251,14 +258,7 @@ export class CloudController {
251258
const app = await createNewApp(this.apiService, team, folderName)
252259
if (!app) return
253260

254-
await this.configService.writeConfig(this.workspaceRoot, {
255-
app_id: app.id,
256-
team_id: team.id,
257-
})
258-
259-
trackCloudProjectLinked(app.slug)
260-
vscode.window.showInformationMessage(`Linked to ${app.slug}`)
261-
await this.refresh()
261+
await this.saveLink(app, team)
262262
}
263263

264264
async linkProject() {
@@ -273,14 +273,7 @@ export class CloudController {
273273
const app = await pickExistingApp(this.apiService, team)
274274
if (!app) return
275275

276-
await this.configService.writeConfig(this.workspaceRoot, {
277-
app_id: app.id,
278-
team_id: team.id,
279-
})
280-
281-
trackCloudProjectLinked(app.slug)
282-
vscode.window.showInformationMessage(`Linked to ${app.slug}`)
283-
await this.refresh()
276+
await this.saveLink(app, team)
284277
}
285278

286279
private async showMoreMenu() {

0 commit comments

Comments
 (0)