Skip to content

Commit

Permalink
local storage dev root id
Browse files Browse the repository at this point in the history
This puts a random id in local storage and uses it for the Firestore and Realtime database root ids.
This approach is currently broken though, because the Firebase functions have not been updated to use this same root id. The functions continue to use the Firebase user id instead.

See this PR for more info: #2403
  • Loading branch information
scytacki committed Sep 17, 2024
1 parent 62d0166 commit 45b5ebe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/clue/components/clue-app-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ToggleGroup } from "@concord-consortium/react-components";
import { GroupModelType, GroupUserModelType } from "../../models/stores/groups";
import { CustomSelect } from "./custom-select";
import { useStores } from "../../hooks/use-stores";
import { getDevId } from "../../lib/root-id";

// cf. https://mattferderer.com/use-sass-variables-in-typescript-and-javascript
import styles from "./toggle-buttons.scss";
Expand All @@ -29,6 +30,7 @@ export const ClueAppHeaderComponent: React.FC<IProps> = observer(function ClueAp
const getUserTitle = () => {
switch(appMode){
case "dev":
return `Firebase Root: ${getDevId()}`;

Check warning on line 33 in src/clue/components/clue-app-header.tsx

View check run for this annotation

Codecov / codecov/patch

src/clue/components/clue-app-header.tsx#L33

Added line #L33 was not covered by tests
case "qa":
case "test":
return `Firebase UID: ${db.firebase.userId}`;
Expand Down
7 changes: 7 additions & 0 deletions src/lib/firebase.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DB } from "./db";
import { Firebase } from "./firebase";
import { kClueDevIDKey } from "./root-id";

const mockStores = {
appMode: "authed",
Expand All @@ -22,6 +23,12 @@ describe("Firebase class", () => {
const firebase = new Firebase(mockDB);
expect(firebase.getRootFolder()).toBe("/authed/portals/test-portal/");
});
it("should handle the dev appMode", () => {
window.localStorage.setItem(kClueDevIDKey, "random-id");
const stores = {...mockStores, appMode: "dev"};
const firestore = new Firebase({stores} as DB);
expect(firestore.getRootFolder()).toBe("/dev/random-id/portals/test-portal/");
});
describe("should handle the demo appMode", () => {
it("handles basic demo name", () => {
const stores = {...mockStores,
Expand Down
7 changes: 7 additions & 0 deletions src/lib/firestore.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DB } from "./db";
import { Firestore } from "./firestore";
import { kClueDevIDKey } from "./root-id";

const mockStores = {
appMode: "authed",
Expand Down Expand Up @@ -65,6 +66,12 @@ describe("Firestore class", () => {
const firestore = new Firestore(mockDB);
expect(firestore.getRootFolder()).toBe("/authed/test-portal/");
});
it("should handle the dev appMode", () => {
window.localStorage.setItem(kClueDevIDKey, "random-id");
const stores = {...mockStores, appMode: "dev"};
const firestore = new Firestore({stores} as DB);
expect(firestore.getRootFolder()).toBe("/dev/random-id/");
});
describe("should handle the demo appMode", () => {
it("handles basic demo name", () => {
const stores = {...mockStores,
Expand Down
18 changes: 17 additions & 1 deletion src/lib/root-id.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { nanoid } from "nanoid";
import { IStores } from "../models/stores/stores";
import { escapeKey } from "./fire-utils";

export const kClueDevIDKey = "clue-dev-id";

export function getDevId() {
let devId = window.localStorage.getItem(kClueDevIDKey);
if (!devId) {
const newDevId = nanoid();
window.localStorage.setItem(kClueDevIDKey, newDevId);
devId = newDevId;
}
return devId;
}

type IRootDocIdStores = Pick<IStores, "appMode" | "demo" | "user">;

export function getRootId(stores: IRootDocIdStores, firebaseUserId: string) {
Expand All @@ -22,7 +35,10 @@ export function getRootId(stores: IRootDocIdStores, firebaseUserId: string) {
const escapedDemoName = demoName ? escapeKey(demoName) : demoName;
return escapedDemoName || escapedPortal || "demo";
}
// "dev", "qa", and "test"
case "dev": {
return getDevId();
}
// "test" and "qa"
default: {
return firebaseUserId;
}
Expand Down

0 comments on commit 45b5ebe

Please sign in to comment.