Skip to content

Commit

Permalink
First pass at moving projects to singals
Browse files Browse the repository at this point in the history
TODO: need to teardown the old picker at the same time as we build the new picker
  • Loading branch information
tjlav5 committed Sep 8, 2023
1 parent 156abea commit 12c2d2e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
4 changes: 3 additions & 1 deletion firebase-vscode/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { pluginLogger } from "../logger-wrapper";
import { getSettings } from "../utils/settings";
import { setEnabled } from "../../../src/experiments";
import { registerUser } from "./user";
import { registerProject } from "./project";

export function registerCore(broker: ExtensionBrokerImpl): Disposable {
const settings = getSettings();
Expand Down Expand Up @@ -35,6 +36,7 @@ export function registerCore(broker: ExtensionBrokerImpl): Disposable {
registerConfig(broker),
registerEmulators(broker),
registerEnv(broker),
registerUser(broker)
registerUser(broker),
registerProject(broker)
);
}
61 changes: 61 additions & 0 deletions firebase-vscode/src/core/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import vscode, { Disposable, QuickPickItem } from "vscode";
import { ExtensionBrokerImpl } from "../extension-broker";
import { computed, effect, signal } from "@preact/signals-react";
import { firebaseRC } from "./config";
import { FirebaseProjectMetadata } from "../types/project";
import { currentUserId } from "./user";
import { listProjects } from "../cli";
import { debuglog } from "util";
import { pluginLogger } from "../logger-wrapper";

/** Available projects */
export const projects = signal<Record<string, FirebaseProjectMetadata[]>>({});

/** Currently selected project ID */
export const currentProjectId = signal("");

/** Gets the currently selected project, fallback to first default project in RC file */
export const currentProject = computed<FirebaseProjectMetadata | undefined>(
() => {
const userProjects = projects.value[currentUserId.value] ?? [];
const wantProjectId =
currentProjectId.value || firebaseRC.value.projects["default"];
return userProjects.find((p) => p.projectId === wantProjectId);
}
);

export function registerProject(broker: ExtensionBrokerImpl): Disposable {
effect(() => {
broker.send("notifyProjectChanged", {
projectId: currentProject.value?.projectId ?? "",
});
});

broker.on("getInitialData", () => {
broker.send("notifyProjectChanged", {
projectId: currentProject.value?.projectId ?? "",
});
});

broker.on("selectProject", async () => {
// TODO: implement at the same time we teardown the old picker
// const projects = await listProjects();
// const id = await promptUserForProject(projects);
// pluginLogger.info("foo:", { id });
});

return {
dispose() {},
};
}

/** Get the user to select a project */
async function promptUserForProject(projects: FirebaseProjectMetadata[]) {
const items: QuickPickItem[] = projects.map((p) => ({
label: p.projectId,
description: p.displayName,
}));

const projectId = await vscode.window.showQuickPick(items);
return projectId;
}
6 changes: 3 additions & 3 deletions firebase-vscode/src/core/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { getAccounts, login, logoutUser } from "../cli";
type User = ServiceAccountUser | AuthUser;

/** Available user accounts */
const users = signal<Record<string /** email */, User>>({});
export const users = signal<Record<string /** email */, User>>({});

/** Currently selected user email */
const currentUserId = signal("");
export const currentUserId = signal("");

/** Gets the currently selected user, fallback to first available user */
const currentUser = computed<User | undefined>(() => {
export const currentUser = computed<User | undefined>(() => {
return users.value[currentUserId.value] ?? Object.values(users.value)[0];
});

Expand Down

0 comments on commit 12c2d2e

Please sign in to comment.