environments.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {
  2. collection,
  3. doc,
  4. getFirestore,
  5. onSnapshot,
  6. setDoc,
  7. } from "firebase/firestore"
  8. import { currentUser$ } from "./auth"
  9. import {
  10. Environment,
  11. environments$,
  12. globalEnv$,
  13. replaceEnvironments,
  14. setGlobalEnvVariables,
  15. } from "~/newstore/environments"
  16. import { settingsStore } from "~/newstore/settings"
  17. /**
  18. * Used locally to prevent infinite loop when environment sync update
  19. * is applied to the store which then fires the store sync listener.
  20. * When you want to update environments and not want to fire the update listener,
  21. * set this to true and then set it back to false once it is done
  22. */
  23. let loadedEnvironments = false
  24. /**
  25. * Used locally to prevent infinite loop when global env sync update
  26. * is applied to the store which then fires the store sync listener.
  27. * When you want to update global env and not want to fire the update listener,
  28. * set this to true and then set it back to false once it is done
  29. */
  30. let loadedGlobals = true
  31. async function writeEnvironments(environment: Environment[]) {
  32. if (currentUser$.value == null)
  33. throw new Error("Cannot write environments when signed out")
  34. const ev = {
  35. updatedOn: new Date(),
  36. author: currentUser$.value.uid,
  37. author_name: currentUser$.value.displayName,
  38. author_image: currentUser$.value.photoURL,
  39. environment,
  40. }
  41. try {
  42. await setDoc(
  43. doc(
  44. getFirestore(),
  45. "users",
  46. currentUser$.value.uid,
  47. "envrionments",
  48. "sync"
  49. ),
  50. ev
  51. )
  52. } catch (e) {
  53. console.error("error updating", ev, e)
  54. throw e
  55. }
  56. }
  57. async function writeGlobalEnvironment(variables: Environment["variables"]) {
  58. if (currentUser$.value == null)
  59. throw new Error("Cannot write global environment when signed out")
  60. const ev = {
  61. updatedOn: new Date(),
  62. author: currentUser$.value.uid,
  63. author_name: currentUser$.value.displayName,
  64. author_image: currentUser$.value.photoURL,
  65. variables,
  66. }
  67. try {
  68. await setDoc(
  69. doc(getFirestore(), "users", currentUser$.value.uid, "globalEnv", "sync"),
  70. ev
  71. )
  72. } catch (e) {
  73. console.error("error updating", ev, e)
  74. throw e
  75. }
  76. }
  77. export function initEnvironments() {
  78. environments$.subscribe((envs) => {
  79. if (
  80. currentUser$.value &&
  81. settingsStore.value.syncEnvironments &&
  82. loadedEnvironments
  83. ) {
  84. writeEnvironments(envs)
  85. }
  86. })
  87. globalEnv$.subscribe((vars) => {
  88. if (
  89. currentUser$.value &&
  90. settingsStore.value.syncEnvironments &&
  91. loadedGlobals
  92. ) {
  93. writeGlobalEnvironment(vars)
  94. }
  95. })
  96. let envSnapshotStop: (() => void) | null = null
  97. let globalsSnapshotStop: (() => void) | null = null
  98. currentUser$.subscribe((user) => {
  99. if (!user) {
  100. // User logged out, clean up snapshot listener
  101. if (envSnapshotStop) {
  102. envSnapshotStop()
  103. envSnapshotStop = null
  104. }
  105. if (globalsSnapshotStop) {
  106. globalsSnapshotStop()
  107. globalsSnapshotStop = null
  108. }
  109. } else if (user) {
  110. envSnapshotStop = onSnapshot(
  111. collection(getFirestore(), "users", user.uid, "environments"),
  112. (environmentsRef) => {
  113. const environments: any[] = []
  114. environmentsRef.forEach((doc) => {
  115. const environment = doc.data()
  116. environment.id = doc.id
  117. environments.push(environment)
  118. })
  119. loadedEnvironments = false
  120. if (environments.length > 0) {
  121. replaceEnvironments(environments[0].environment)
  122. }
  123. loadedEnvironments = true
  124. }
  125. )
  126. globalsSnapshotStop = onSnapshot(
  127. collection(getFirestore(), "users", user.uid, "globalEnv"),
  128. (globalsRef) => {
  129. if (globalsRef.docs.length === 0) {
  130. loadedGlobals = true
  131. return
  132. }
  133. const doc = globalsRef.docs[0].data()
  134. loadedGlobals = false
  135. setGlobalEnvVariables(doc.variables)
  136. loadedGlobals = true
  137. }
  138. )
  139. }
  140. })
  141. }