Skip to content

Commit c146457

Browse files
committed
Add recents section
1 parent 88cab27 commit c146457

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

src/browser/pages/home.html

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ <h2 class="main">Running</h2>
3030
</div>
3131
</div>
3232

33+
<div class="card-box">
34+
<div class="header">
35+
<h2 class="main">Recent</h2>
36+
<div class="sub">Choose a recent directory or workspace to launch below.</div>
37+
</div>
38+
<div class="content">
39+
{{APP_LIST:RECENT_PROJECTS}}
40+
</div>
41+
</div>
42+
3343
<div class="card-box">
3444
<div class="header">
3545
<h2 class="main">Editors</h2>

src/common/api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface SessionResponse {
3434

3535
export interface RecentResponse {
3636
readonly paths: string[]
37+
readonly workspaces: string[]
3738
}
3839

3940
export interface RunningResponse {

src/node/app/api.ts

+25-19
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface ServerSession {
2828
}
2929

3030
interface VsRecents {
31-
[key: string]: (string | object)[]
31+
[key: string]: (string | { configURIPath: string })[]
3232
}
3333

3434
type VsSettings = [string, string][]
@@ -322,36 +322,42 @@ export class ApiHttpProvider extends HttpProvider {
322322
throw new Error("settings appear malformed")
323323
}
324324

325-
const paths: { [key: string]: Promise<string> } = {}
325+
const pathPromises: { [key: string]: Promise<string> } = {}
326+
const workspacePromises: { [key: string]: Promise<string> } = {}
326327
Object.values(JSON.parse(setting[1]) as VsRecents).forEach((recents) => {
327-
recents
328-
.filter((recent) => typeof recent === "string")
329-
.forEach((recent) => {
330-
try {
331-
const pathname = url.parse(recent as string).pathname
332-
if (pathname && !paths[pathname]) {
333-
paths[pathname] = new Promise<string>((resolve) => {
334-
fs.stat(pathname)
335-
.then(() => resolve(pathname))
336-
.catch(() => resolve())
337-
})
338-
}
339-
} catch (error) {
340-
logger.debug("invalid path", field("path", recent))
328+
recents.forEach((recent) => {
329+
try {
330+
const target = typeof recent === "string" ? pathPromises : workspacePromises
331+
const pathname = url.parse(typeof recent === "string" ? recent : recent.configURIPath).pathname
332+
if (pathname && !target[pathname]) {
333+
target[pathname] = new Promise<string>((resolve) => {
334+
fs.stat(pathname)
335+
.then(() => resolve(pathname))
336+
.catch(() => resolve())
337+
})
341338
}
342-
})
339+
} catch (error) {
340+
logger.debug("invalid path", field("path", recent))
341+
}
342+
})
343343
})
344344

345+
const [paths, workspaces] = await Promise.all([
346+
Promise.all(Object.values(pathPromises)),
347+
Promise.all(Object.values(workspacePromises)),
348+
])
349+
345350
return {
346-
paths: await Promise.all(Object.values(paths)),
351+
paths: paths.filter((p) => !!p),
352+
workspaces: workspaces.filter((p) => !!p),
347353
}
348354
} catch (error) {
349355
if (error.code !== "ENOENT") {
350356
throw error
351357
}
352358
}
353359

354-
return { paths: [] }
360+
return { paths: [], workspaces: [] }
355361
}
356362

357363
/**

src/node/app/app.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as http from "http"
22
import * as querystring from "querystring"
3-
import { Application } from "../../common/api"
3+
import { Application, RecentResponse } from "../../common/api"
44
import { HttpCode, HttpError } from "../../common/http"
55
import { HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
66
import { ApiHttpProvider } from "./api"
@@ -86,6 +86,7 @@ export class MainHttpProvider extends HttpProvider {
8686
response.content = response.content
8787
.replace(/{{UPDATE:NAME}}/, await this.getUpdate())
8888
.replace(/{{APP_LIST:RUNNING}}/, this.getAppRows(running.applications))
89+
.replace(/{{APP_LIST:RECENT_PROJECTS}}/, this.getRecentProjectRows(await this.api.recent()))
8990
.replace(
9091
/{{APP_LIST:EDITORS}}/,
9192
this.getAppRows(apps.filter((app) => app.categories && app.categories.includes("Editor"))),
@@ -107,6 +108,21 @@ export class MainHttpProvider extends HttpProvider {
107108
return undefined
108109
}
109110

111+
private getRecentProjectRows(recents: RecentResponse): string {
112+
return recents.paths.length > 0 || recents.workspaces.length > 0
113+
? recents.paths.map((recent) => this.getRecentProjectRow(recent)).join("\n") +
114+
recents.workspaces.map((recent) => this.getRecentProjectRow(recent, true)).join("\n")
115+
: `<div class="none">No recent directories or workspaces.</div>`
116+
}
117+
118+
private getRecentProjectRow(recent: string, workspace?: boolean): string {
119+
return `<div class="block-row">
120+
<a class="item -row -link" href="./vscode/?${workspace ? "workspace" : "folder"}=${recent}">
121+
<div class="name">${recent}${workspace ? " (workspace)" : ""}</div>
122+
</a>
123+
</div>`
124+
}
125+
110126
private getAppRows(apps: ReadonlyArray<Application>): string {
111127
return apps.length > 0
112128
? apps.map((app) => this.getAppRow(app)).join("\n")

0 commit comments

Comments
 (0)