@budibase/shared-core 2.6.22 → 2.6.24-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import { User } from "@budibase/types"
2
+
1
3
  /**
2
4
  * Gets a key within an object. The key supports dot syntax for retrieving deep
3
5
  * fields - e.g. "a.b.c".
@@ -21,3 +23,63 @@ export const deepGet = (obj: { [x: string]: any }, key: string) => {
21
23
  }
22
24
  return obj
23
25
  }
26
+
27
+ /**
28
+ * Gets the initials to show in a user avatar.
29
+ * @param user the user
30
+ */
31
+ export const getUserInitials = (user: User) => {
32
+ if (!user) {
33
+ return "?"
34
+ }
35
+ let initials = ""
36
+ initials += user.firstName ? user.firstName[0] : ""
37
+ initials += user.lastName ? user.lastName[0] : ""
38
+ if (initials !== "") {
39
+ return initials
40
+ }
41
+ return user.email?.[0] || "U"
42
+ }
43
+
44
+ /**
45
+ * Gets a deterministic colour for a particular user
46
+ * @param user the user
47
+ */
48
+ export const getUserColor = (user: User) => {
49
+ let id = user?._id
50
+ if (!id) {
51
+ return "var(--spectrum-global-color-blue-400)"
52
+ }
53
+
54
+ // In order to generate the same color for global users as app users, we need
55
+ // to remove the app-specific table prefix
56
+ id = id.replace("ro_ta_users_", "")
57
+
58
+ // Generate a hue based on the ID
59
+ let hue = 1
60
+ for (let i = 0; i < id.length; i++) {
61
+ hue += id.charCodeAt(i)
62
+ hue = hue % 36
63
+ }
64
+ return `hsl(${hue * 10}, 50%, 40%)`
65
+ }
66
+
67
+ /**
68
+ * Gets a friendly label to describe who a user is.
69
+ * @param user the user
70
+ */
71
+ export const getUserLabel = (user: User) => {
72
+ if (!user) {
73
+ return ""
74
+ }
75
+ const { firstName, lastName, email } = user
76
+ if (firstName && lastName) {
77
+ return `${firstName} ${lastName}`
78
+ } else if (firstName) {
79
+ return firstName
80
+ } else if (lastName) {
81
+ return lastName
82
+ } else {
83
+ return email
84
+ }
85
+ }
package/tsconfig.json CHANGED
@@ -6,6 +6,5 @@
6
6
  "paths": {
7
7
  "@budibase/types": ["../types/src"]
8
8
  }
9
- },
10
- "references": [{ "path": "../types" }]
9
+ }
11
10
  }