Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

local storage dev root id #2407

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@
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
Loading