-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
50 lines (42 loc) · 1.27 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { collection, doc, getDoc, setDoc } from "firebase/firestore";
import db from "./app/db";
import { v4 } from "uuid";
import { Session } from "next-auth";
export const createWorkspace = async (agenda: string, userEmail: string) => {
// Creates a document reference with id to be assigned before adding to db
const workspaceRef = doc(collection(db, "workspaces"));
localStorage.setItem("workspace", workspaceRef.id);
await setDoc(workspaceRef, {
agenda,
owner: userEmail,
});
const d = await getDoc(workspaceRef);
return { ...d.data(), id: d.id };
};
export const makeID = () => {
const newID = v4();
localStorage.setItem("temporary-user", newID);
return newID;
};
export const getSafeUserEmail = (session: Session | null) => {
return session && session.user && session.user.email
? session.user.email
: localStorage.getItem("temporary-user")
? (localStorage.getItem("temporary-user") as string)
: makeID();
};
export const randomColor = () => {
const colors = [
"indigo",
"crimson",
"purple",
"orange",
"",
"amber",
"bronze",
"brown",
"gold",
"tomato",
];
return colors[Math.round(Math.random() * 10)];
};