placementt-core 1.20.196 → 11.0.533

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.
Files changed (135) hide show
  1. package/.eslintrc.js +40 -40
  2. package/.gitattributes +2 -2
  3. package/lib/config.d.ts +0 -1
  4. package/lib/constants.d.ts +5 -2
  5. package/lib/constants.js +130 -136
  6. package/lib/constants.js.map +1 -1
  7. package/lib/features/analytics/useAnalytics.d.ts +0 -1
  8. package/lib/features/analytics/useAnalytics.js +3 -4
  9. package/lib/features/analytics/useAnalytics.js.map +1 -1
  10. package/lib/features/config.d.ts +133 -133
  11. package/lib/features/config.js +35 -35
  12. package/lib/features/contacts/contacts.d.ts +75 -75
  13. package/lib/features/contacts/contacts.js +105 -105
  14. package/lib/features/contacts/contactsSlice.d.ts +5 -5
  15. package/lib/features/contacts/useContacts.js +1 -2
  16. package/lib/features/contacts/useContacts.js.map +1 -1
  17. package/lib/features/downtime/useDowntime.d.ts +11 -11
  18. package/lib/features/downtime/useDowntime.js +22 -22
  19. package/lib/features/dropdown/useDropdown.d.ts +2 -3
  20. package/lib/features/dropdown/useDropdown.js +1 -2
  21. package/lib/features/dropdown/useDropdown.js.map +1 -1
  22. package/lib/features/global/downtime/useDowntime.d.ts +0 -1
  23. package/lib/features/global/downtime/useDowntime.js +3 -2
  24. package/lib/features/global/downtime/useDowntime.js.map +1 -1
  25. package/lib/features/global/users/useUserFunctions.d.ts +0 -1
  26. package/lib/features/global/users/useUserFunctions.js +6 -7
  27. package/lib/features/global/users/useUserFunctions.js.map +1 -1
  28. package/lib/features/placements/studentPlacements/activePlacement.d.ts +1 -1
  29. package/lib/features/placements/studentPlacements/activePlacement.js +1 -1
  30. package/lib/features/placements/studentPlacements/activePlacement.js.map +1 -1
  31. package/lib/features/placements/studentPlacements/completedStudentPlacementsSlice.d.ts +4 -5
  32. package/lib/features/placements/studentPlacements/completedStudentPlacementsSlice.js +1 -4
  33. package/lib/features/placements/studentPlacements/completedStudentPlacementsSlice.js.map +1 -1
  34. package/lib/features/placements/studentPlacements/studentPlacementsSlice.d.ts +63 -63
  35. package/lib/features/placements/studentPlacements/studentPlacementsSlice.js +81 -81
  36. package/lib/features/placements/studentPlacements/upcomingStudentPlacementsSlice.d.ts +4 -4
  37. package/lib/features/placements/studentPlacements/upcomingStudentPlacementsSlice.js +1 -1
  38. package/lib/features/placements/studentPlacements/upcomingStudentPlacementsSlice.js.map +1 -1
  39. package/lib/features/placements/studentPlacements/useStudentPlacements.d.ts +0 -1
  40. package/lib/features/placements/studentPlacements/useStudentPlacements.js +1 -2
  41. package/lib/features/placements/studentPlacements/useStudentPlacements.js.map +1 -1
  42. package/lib/features/providerPlacements/providerPlacementsSlice.d.ts +19 -19
  43. package/lib/features/providerPlacements/providerPlacementsSlice.js +24 -24
  44. package/lib/features/referrals/useReferrals.d.ts +0 -1
  45. package/lib/features/referrals/useReferrals.js +1 -2
  46. package/lib/features/referrals/useReferrals.js.map +1 -1
  47. package/lib/features/studentPlacements/studentPlacementsSlice.d.ts +62 -62
  48. package/lib/features/studentPlacements/studentPlacementsSlice.js +87 -87
  49. package/lib/features/studentPlacements/useStudentPlacements.d.ts +6 -6
  50. package/lib/features/studentPlacements/useStudentPlacements.js +18 -18
  51. package/lib/features/updates/useUpdates.js +1 -2
  52. package/lib/features/updates/useUpdates.js.map +1 -1
  53. package/lib/features/userSlice.d.ts +26 -26
  54. package/lib/features/userSlice.js +23 -23
  55. package/lib/features/users/useUserFunctions.d.ts +25 -25
  56. package/lib/features/users/useUserFunctions.js +124 -124
  57. package/lib/features/users/userSlice.d.ts +46 -46
  58. package/lib/features/users/userSlice.js +48 -48
  59. package/lib/firebase/firebase.d.ts +1 -1
  60. package/lib/firebase/firebase.js +9 -4
  61. package/lib/firebase/firebase.js.map +1 -1
  62. package/lib/firebase/firebaseConfig.js +3 -0
  63. package/lib/firebase/firebaseConfig.js.map +1 -1
  64. package/lib/firebase/firebaseQuery.js +3 -0
  65. package/lib/firebase/firebaseQuery.js.map +1 -1
  66. package/lib/firebase/persistence.js +2 -2
  67. package/lib/firebase/persistence.js.map +1 -1
  68. package/lib/firebase/readDatabase.d.ts +9 -6
  69. package/lib/firebase/readDatabase.js +16 -6
  70. package/lib/firebase/readDatabase.js.map +1 -1
  71. package/lib/firebase/util.d.ts +3 -4
  72. package/lib/firebase/util.js +49 -4
  73. package/lib/firebase/util.js.map +1 -1
  74. package/lib/firebase/writeDatabase.d.ts +3 -3
  75. package/lib/firebase/writeDatabase.js +7 -1
  76. package/lib/firebase/writeDatabase.js.map +1 -1
  77. package/lib/hooks.d.ts +384 -23
  78. package/lib/hooks.js +1342 -223
  79. package/lib/hooks.js.map +1 -1
  80. package/lib/images/GatsbyBenchmarks.d.ts +0 -1
  81. package/lib/images/GatsbyBenchmarks.js +1 -1
  82. package/lib/images/GatsbyBenchmarks.js.map +1 -1
  83. package/lib/reduxHooks.d.ts +11 -20
  84. package/lib/reduxHooks.js +28 -18
  85. package/lib/reduxHooks.js.map +1 -1
  86. package/lib/tasksAndTips.d.ts +25 -5
  87. package/lib/tasksAndTips.js +346 -48
  88. package/lib/tasksAndTips.js.map +1 -1
  89. package/lib/typeDefinitions.d.ts +478 -53
  90. package/lib/util.d.ts +1 -0
  91. package/lib/util.js +78 -6
  92. package/lib/util.js.map +1 -1
  93. package/package.json +52 -49
  94. package/src/DatabaseDefinitions.ts +18 -18
  95. package/src/apiCalls.ts +128 -128
  96. package/src/config.ts +50 -50
  97. package/src/constants.ts +708 -707
  98. package/src/databaseTypes.ts +42 -42
  99. package/src/features/analytics/useAnalytics.tsx +55 -55
  100. package/src/features/contacts/contactsSlice.ts +147 -147
  101. package/src/features/contacts/useContacts.tsx +73 -73
  102. package/src/features/dropdown/useDropdown.tsx +52 -52
  103. package/src/features/global/downtime/useDowntime.tsx +19 -18
  104. package/src/features/global/users/useUserFunctions.tsx +132 -132
  105. package/src/features/jobs/jobsSlice.ts +65 -65
  106. package/src/features/placements/studentPlacements/activePlacement.ts +63 -63
  107. package/src/features/placements/studentPlacements/completedStudentPlacementsSlice.ts +94 -97
  108. package/src/features/placements/studentPlacements/upcomingStudentPlacementsSlice.ts +108 -108
  109. package/src/features/placements/studentPlacements/useStudentPlacements.tsx +33 -33
  110. package/src/features/placements/types.ts +10 -10
  111. package/src/features/referrals/useReferrals.tsx +56 -56
  112. package/src/features/updates/useUpdates.tsx +36 -36
  113. package/src/firebase/firebase.tsx +142 -138
  114. package/src/firebase/firebaseConfig.tsx +45 -42
  115. package/src/firebase/firebaseQuery.tsx +143 -140
  116. package/src/firebase/persistence.ts +84 -84
  117. package/src/firebase/readDatabase.tsx +208 -197
  118. package/src/firebase/util.tsx +352 -308
  119. package/src/firebase/writeDatabase.tsx +75 -68
  120. package/src/hooks.tsx +3395 -1943
  121. package/src/images/GatsbyBenchmarks.tsx +711 -711
  122. package/src/images/LogFuturePlacement.jsx +64 -64
  123. package/src/images/LogPreviousPlacement.jsx +228 -228
  124. package/src/images/gatsby_benchmarks.svg +466 -466
  125. package/src/images/log_future_placement.svg +114 -114
  126. package/src/images/log_previous_placement.svg +199 -199
  127. package/src/index.ts +34 -34
  128. package/src/readDatabase.tsx +3 -3
  129. package/src/reduxHooks.ts +183 -170
  130. package/src/tasksAndTips.ts +744 -410
  131. package/src/tutorialTips.ts +58 -58
  132. package/src/typeDefinitions.ts +899 -503
  133. package/src/util.ts +132 -47
  134. package/tsconfig.dev.json +5 -5
  135. package/tsconfig.json +21 -21
@@ -1,140 +1,143 @@
1
- import {addDoc, collection, CollectionReference, deleteDoc, doc,
2
- DocumentData, DocumentReference, FieldValue, getCountFromServer, getDoc,
3
- getDocs, onSnapshot, Query, query, QueryConstraint, QueryDocumentSnapshot, setDoc, updateDoc, where} from "firebase/firestore";
4
- import {SetStateAction} from "react";
5
- import {db} from "./firebaseConfig";
6
- import {pathToArr} from "./util";
7
- import {Products} from "../typeDefinitions";
8
- import { AppDispatch } from "../config";
9
-
10
- export default class FirebaseQuery {
11
- private createQuery(queryType:"collection"|"doc", path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
12
- switch (queryType) {
13
- case "collection":
14
- return query(collection(db, ...(pathToArr(path) as [any])), ...[...(constraints ? Array.isArray(constraints) ? constraints : [constraints] : [])]);
15
- case "doc":
16
- return doc(db, ...(pathToArr(path) as [any]));
17
- }
18
- }
19
-
20
- async getCount(path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint):Promise<number> {
21
- console.log("GET COUNT", path, constraints);
22
- const countObject = await getCountFromServer(this.createQuery("collection", path, constraints) as Query).catch((e) => console.log(e));
23
- if (!countObject) return 0;
24
- return (countObject).data().count;
25
- }
26
-
27
- async add(path:string[]|string, data:{[key:string]:unknown}) {
28
- if (path.length % 2 === 0) {
29
- throw new Error("Provide a collection reference");
30
- }
31
- return await addDoc(this.createQuery("collection", path) as CollectionReference, data);
32
- }
33
-
34
- async set(path:string[]|string, data:{[key:string]:unknown}, merge=false) {
35
- if (path.length % 2 === 1) {
36
- throw new Error("Provide a document reference");
37
- }
38
- return await setDoc(this.createQuery("doc", path) as DocumentReference, data, {merge: merge});
39
- }
40
-
41
- async update(path:string[]|string, data:{ [key: string]: unknown}) {
42
- if (path.length % 2 === 1) {
43
- throw new Error("Provide a document reference");
44
- }
45
- console.log("update", path, data);
46
- return await updateDoc(this.createQuery("doc", path) as DocumentReference, data as { [key: string]: FieldValue | Partial<unknown> | undefined});
47
- }
48
-
49
- async delete(path:string[]|string) {
50
- if (path.length % 2 === 1) {
51
- throw new Error("Provide a document reference");
52
- }
53
- await deleteDoc(this.createQuery("doc", path) as DocumentReference);
54
- }
55
-
56
- async copy(path: string[]|string, id: string, oId: string, product: Products) {
57
- const existing = await this.getDocData([...path, id]);
58
- console.log("existing", existing);
59
- existing.name = await this.getUniqueDocName(path, existing.name, oId, product);
60
- if (existing.default) {
61
- existing.default = false;
62
- }
63
- return await this.add(path, existing);
64
- }
65
-
66
- async getUniqueDocName(path: string[]|string, name: string, oId: string, product: Products): Promise<string> {
67
- const q = this.createQuery("collection", path, [where("name", "==", name), where("oId", "==", oId), where("product", "==", product)]);
68
-
69
- const querySnapshot = await getDocs(q as Query);
70
- if (!querySnapshot.empty) {
71
- return this.getUniqueDocName(path, name += "-", oId, product);
72
- }
73
- return name;
74
- }
75
-
76
- async getDocData(path:string[]|string) {
77
- console.log("GET DOC", path);
78
- if (path.length % 2 === 1) {
79
- throw new Error("Provide a document reference");
80
- }
81
- const result = await getDoc(this.createQuery("doc", path) as DocumentReference).catch((error) => {
82
- console.log(`Error in snapshot ${path}`, error);
83
- throw error;
84
- });
85
- const resultData = result.data() || {};
86
- resultData.id = result.id;
87
- return resultData;
88
- }
89
-
90
- async getDocsWhere(path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, rawDocs=false) {
91
- const q = this.createQuery("collection", path, constraints);
92
-
93
- const docs = await getDocs(q as Query).catch((e) => console.log(e, q));
94
- if (rawDocs) return docs;
95
- if (!docs) return {};
96
- console.log("GET DOCS WHERE", docs.size, path, constraints);
97
-
98
- return docs.docs.reduce((acc: {[k: string]: any}, document) => {
99
- const docData = {...document.data(), id: document.id};
100
- acc[document.id] = docData;
101
- return acc;
102
- }, {});
103
- }
104
-
105
- async collectionUpdate(data:{[key:string]:unknown}, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
106
- if (path.length % 2 === 0) {
107
- throw new Error("Provide a collection reference");
108
- }
109
- const q = this.createQuery("collection", path, constraints) as Query;
110
- (await getDocs(q)).docs.forEach((doc) => {
111
- this.update(doc.ref.path.split("/"), data);
112
- });
113
- }
114
-
115
- documentSnapshot(path: string[]|string, setState: SetStateAction<any>) {
116
- const q = this.createQuery("doc", path) as DocumentReference;
117
- return onSnapshot(q, (doc) => {
118
- setState({...doc.data(), id: doc.id});
119
- }, (error) => {
120
- console.log(`Error in snapshot ${path}`, error);
121
- });
122
- }
123
-
124
- collectionSnapshot(setState:SetStateAction<any>, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, dispatch?: AppDispatch) {
125
- const q = this.createQuery("collection", path, constraints) as Query<DocumentData>;
126
-
127
- return onSnapshot(q, (querySnapshot: DocumentData) => {
128
- const itemList: {[key: string]: any} = {};
129
-
130
- querySnapshot.docs.forEach((doc: QueryDocumentSnapshot) => {
131
- const item = doc.data();
132
- item.id = doc.id;
133
- itemList[doc.id] = item;
134
- });
135
- dispatch ? dispatch(setState(itemList)) : setState(itemList);
136
- }, (error) => {
137
- console.log(`Error in snapshot ${path}`, error);
138
- });
139
- }
140
- }
1
+ import {addDoc, collection, CollectionReference, deleteDoc, doc,
2
+ DocumentData, DocumentReference, FieldValue, getCountFromServer, getDoc,
3
+ getDocs, onSnapshot, Query, query, QueryConstraint, QueryDocumentSnapshot, setDoc, updateDoc, where} from "firebase/firestore";
4
+ import {SetStateAction} from "react";
5
+ import {db} from "./firebaseConfig";
6
+ import {pathToArr} from "./util";
7
+ import {Products} from "../typeDefinitions";
8
+ import { AppDispatch } from "../config";
9
+
10
+ export default class FirebaseQuery {
11
+ private createQuery(queryType:"collection"|"doc", path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
12
+ switch (queryType) {
13
+ case "collection":
14
+ return query(collection(db, ...(pathToArr(path) as [any])), ...[...(constraints ? Array.isArray(constraints) ? constraints : [constraints] : [])]);
15
+ case "doc":
16
+ return doc(db, ...(pathToArr(path) as [any]));
17
+ }
18
+ }
19
+
20
+ async getCount(path:string[]|string, constraints?:QueryConstraint[]|QueryConstraint):Promise<number> {
21
+ console.log("GET COUNT", path, constraints);
22
+ const countObject = await getCountFromServer(this.createQuery("collection", path, constraints) as Query).catch((e) => console.log(e));
23
+ if (!countObject) return 0;
24
+ return (countObject).data().count;
25
+ }
26
+
27
+ async add(path:string[]|string, data:{[key:string]:unknown}) {
28
+ if (path.length % 2 === 0) {
29
+ throw new Error("Provide a collection reference");
30
+ }
31
+ return await addDoc(this.createQuery("collection", path) as CollectionReference, data);
32
+ }
33
+
34
+ async set(path:string[]|string, data:{[key:string]:unknown}, merge=false) {
35
+ if (path.length % 2 === 1) {
36
+ throw new Error("Provide a document reference");
37
+ }
38
+ return await setDoc(this.createQuery("doc", path) as DocumentReference, data, {merge: merge});
39
+ }
40
+
41
+ async update(path:string[]|string, data:{ [key: string]: unknown}) {
42
+ if (path.length % 2 === 1) {
43
+ throw new Error("Provide a document reference");
44
+ }
45
+ console.log("update", path, data);
46
+ return await updateDoc(this.createQuery("doc", path) as DocumentReference, data as { [key: string]: FieldValue | Partial<unknown> | undefined});
47
+ }
48
+
49
+ async delete(path:string[]|string) {
50
+ if (path.length % 2 === 1) {
51
+ throw new Error("Provide a document reference");
52
+ }
53
+ await deleteDoc(this.createQuery("doc", path) as DocumentReference);
54
+ }
55
+
56
+ async copy(path: string[]|string, id: string, oId: string, product: Products) {
57
+ const existing = await this.getDocData([...path, id]);
58
+ console.log("existing", existing);
59
+ existing.name = await this.getUniqueDocName(path, existing.name, oId, product);
60
+ if (existing.default) {
61
+ existing.default = false;
62
+ }
63
+ return await this.add(path, existing);
64
+ }
65
+
66
+ async getUniqueDocName(path: string[]|string, name: string, oId: string, product: Products): Promise<string> {
67
+ const q = this.createQuery("collection", path, [where("name", "==", name), where("oId", "==", oId), where("product", "==", product)]);
68
+
69
+ const querySnapshot = await getDocs(q as Query);
70
+ if (!querySnapshot.empty) {
71
+ return this.getUniqueDocName(path, name += "-", oId, product);
72
+ }
73
+ return name;
74
+ }
75
+
76
+ async getDocData(path:string[]|string) {
77
+ console.log("GET DOC", path);
78
+ if (path.length % 2 === 1) {
79
+ throw new Error("Provide a document reference");
80
+ }
81
+ const result = await getDoc(this.createQuery("doc", path) as DocumentReference).catch((error) => {
82
+ console.log(`Error in snapshot ${path}`, error);
83
+ throw error;
84
+ });
85
+ const resultData = result.data() || {};
86
+ resultData.id = result.id;
87
+ return resultData;
88
+ }
89
+
90
+ async getDocsWhere(path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, rawDocs=false) {
91
+ const q = this.createQuery("collection", path, constraints);
92
+
93
+ const docs = await getDocs(q as Query).catch((e) => console.log(e, q));
94
+ if (rawDocs) return docs;
95
+ if (!docs) return {};
96
+ console.log("GET DOCS WHERE", docs.size, path, constraints);
97
+
98
+ return docs.docs.reduce((acc: {[k: string]: any}, document) => {
99
+ const docData = {...document.data(), id: document.id};
100
+ acc[document.id] = docData;
101
+ return acc;
102
+ }, {});
103
+ }
104
+
105
+ async collectionUpdate(data:{[key:string]:unknown}, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint) {
106
+ if (path.length % 2 === 0) {
107
+ throw new Error("Provide a collection reference");
108
+ }
109
+ const q = this.createQuery("collection", path, constraints) as Query;
110
+ (await getDocs(q)).docs.forEach((doc) => {
111
+ this.update(doc.ref.path.split("/"), data);
112
+ });
113
+ }
114
+
115
+ documentSnapshot(path: string[]|string, setState: SetStateAction<any>) {
116
+ console.log("Building snapshot query: ", path);
117
+ const q = this.createQuery("doc", path) as DocumentReference;
118
+ console.log("Query created");
119
+ return onSnapshot(q, (doc) => {
120
+ console.log("Data", doc.data());
121
+ setState({...doc.data(), id: doc.id});
122
+ }, (error) => {
123
+ console.log(`Error in snapshot ${path}`, error);
124
+ });
125
+ }
126
+
127
+ collectionSnapshot(setState:SetStateAction<any>, path: string[]|string, constraints?:QueryConstraint[]|QueryConstraint, dispatch?: AppDispatch) {
128
+ const q = this.createQuery("collection", path, constraints) as Query<DocumentData>;
129
+
130
+ return onSnapshot(q, (querySnapshot: DocumentData) => {
131
+ const itemList: {[key: string]: any} = {};
132
+
133
+ querySnapshot.docs.forEach((doc: QueryDocumentSnapshot) => {
134
+ const item = doc.data();
135
+ item.id = doc.id;
136
+ itemList[doc.id] = item;
137
+ });
138
+ dispatch ? dispatch(setState(itemList)) : setState(itemList);
139
+ }, (error) => {
140
+ console.log(`Error in snapshot ${path}`, error);
141
+ });
142
+ }
143
+ }
@@ -1,84 +1,84 @@
1
- import {Persistence, ReactNativeAsyncStorage} from "firebase/auth";
2
-
3
- export const enum PersistenceType {
4
- SESSION = "SESSION",
5
- LOCAL = "LOCAL",
6
- NONE = "NONE"
7
- }
8
-
9
- export type PersistedBlob = Record<string, unknown>;
10
-
11
- export interface Instantiator<T> {
12
- (blob: PersistedBlob): T;
13
- }
14
-
15
- export type PersistenceValue = PersistedBlob | string;
16
-
17
- export const STORAGE_AVAILABLE_KEY = "__sak";
18
-
19
- export interface StorageEventListener {
20
- (value: PersistenceValue | null): void;
21
- }
22
-
23
- export interface PersistenceInternal extends Persistence {
24
- type: PersistenceType;
25
- _isAvailable(): Promise<boolean>;
26
- _set(key: string, value: PersistenceValue): Promise<void>;
27
- _get<T extends PersistenceValue>(key: string): Promise<T | null>;
28
- _remove(key: string): Promise<void>;
29
- _addListener(key: string, listener: StorageEventListener): void;
30
- _removeListener(key: string, listener: StorageEventListener): void;
31
- // Should this persistence allow migration up the chosen hierarchy?
32
- _shouldAllowMigration?: boolean;
33
- }
34
-
35
- export function getReactNativePersistence(
36
- storage: ReactNativeAsyncStorage
37
- ): Persistence {
38
- // In the _getInstance() implementation (see src/core/persistence/index.ts),
39
- // we expect each "externs.Persistence" object passed to us by the user to
40
- // be able to be instantiated (as a class) using "new". That function also
41
- // expects the constructor to be empty. Since ReactNativeStorage requires the
42
- // underlying storage layer, we need to be able to create subclasses
43
- // (closures, esentially) that have the storage layer but empty constructor.
44
- return class implements PersistenceInternal {
45
- static type = "LOCAL" as const;
46
- readonly type: PersistenceType = PersistenceType.LOCAL;
47
-
48
- async _isAvailable(): Promise<boolean> {
49
- try {
50
- if (!storage) {
51
- return false;
52
- }
53
- await storage.setItem(STORAGE_AVAILABLE_KEY, "1");
54
- await storage.removeItem(STORAGE_AVAILABLE_KEY);
55
- return true;
56
- } catch {
57
- return false;
58
- }
59
- }
60
-
61
- _set(key: string, value: PersistenceValue): Promise<void> {
62
- return storage.setItem(key, JSON.stringify(value));
63
- }
64
-
65
- async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
66
- const json = await storage.getItem(key);
67
- return json ? JSON.parse(json) : null;
68
- }
69
-
70
- _remove(key: string): Promise<void> {
71
- return storage.removeItem(key);
72
- }
73
-
74
- _addListener(_key: string, _listener: StorageEventListener): void {
75
- // Listeners are not supported for React Native storage.
76
- return;
77
- }
78
-
79
- _removeListener(_key: string, _listener: StorageEventListener): void {
80
- // Listeners are not supported for React Native storage.
81
- return;
82
- }
83
- };
84
- }
1
+ import {Persistence, ReactNativeAsyncStorage} from "firebase/auth";
2
+
3
+ export const enum PersistenceType {
4
+ SESSION = "SESSION",
5
+ LOCAL = "LOCAL",
6
+ NONE = "NONE"
7
+ }
8
+
9
+ export type PersistedBlob = Record<string, unknown>;
10
+
11
+ export interface Instantiator<T> {
12
+ (blob: PersistedBlob): T;
13
+ }
14
+
15
+ export type PersistenceValue = PersistedBlob | string;
16
+
17
+ export const STORAGE_AVAILABLE_KEY = "__sak";
18
+
19
+ export interface StorageEventListener {
20
+ (value: PersistenceValue | null): void;
21
+ }
22
+
23
+ export interface PersistenceInternal extends Persistence {
24
+ type: PersistenceType;
25
+ _isAvailable(): Promise<boolean>;
26
+ _set(key: string, value: PersistenceValue): Promise<void>;
27
+ _get<T extends PersistenceValue>(key: string): Promise<T | null>;
28
+ _remove(key: string): Promise<void>;
29
+ _addListener(key: string, listener: StorageEventListener): void;
30
+ _removeListener(key: string, listener: StorageEventListener): void;
31
+ // Should this persistence allow migration up the chosen hierarchy?
32
+ _shouldAllowMigration?: boolean;
33
+ }
34
+
35
+ export function getReactNativePersistence(
36
+ storage: ReactNativeAsyncStorage
37
+ ): Persistence {
38
+ // In the _getInstance() implementation (see src/core/persistence/index.ts),
39
+ // we expect each "externs.Persistence" object passed to us by the user to
40
+ // be able to be instantiated (as a class) using "new". That function also
41
+ // expects the constructor to be empty. Since ReactNativeStorage requires the
42
+ // underlying storage layer, we need to be able to create subclasses
43
+ // (closures, esentially) that have the storage layer but empty constructor.
44
+ return class implements PersistenceInternal {
45
+ static type = "LOCAL" as const;
46
+ readonly type: PersistenceType = PersistenceType.LOCAL;
47
+
48
+ async _isAvailable(): Promise<boolean> {
49
+ try {
50
+ if (!storage) {
51
+ return false;
52
+ }
53
+ await storage.setItem(STORAGE_AVAILABLE_KEY, "1");
54
+ await storage.removeItem(STORAGE_AVAILABLE_KEY);
55
+ return true;
56
+ } catch {
57
+ return false;
58
+ }
59
+ }
60
+
61
+ _set(key: string, value: PersistenceValue): Promise<void> {
62
+ return storage.setItem(key, JSON.stringify(value));
63
+ }
64
+
65
+ async _get<T extends PersistenceValue>(key: string): Promise<T | null> {
66
+ const json = await storage.getItem(key);
67
+ return json ? JSON.parse(json) : null;
68
+ }
69
+
70
+ _remove(key: string): Promise<void> {
71
+ return storage.removeItem(key);
72
+ }
73
+
74
+ _addListener(_key: string, _listener: StorageEventListener): void {
75
+ // Listeners are not supported for React Native storage.
76
+ return;
77
+ }
78
+
79
+ _removeListener(_key: string, _listener: StorageEventListener): void {
80
+ // Listeners are not supported for React Native storage.
81
+ return;
82
+ }
83
+ };
84
+ }