@orsetra/shared-ui 1.11.0 → 1.11.1

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,10 +1,11 @@
1
1
  "use client"
2
2
 
3
- import React, { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react"
3
+ import React, { createContext, useContext, useEffect, useMemo, useRef, useSyncExternalStore } from "react"
4
4
  import {
5
- chooseEnvironment, environmentLabel, environmentStatusOf, findEnvironment, latestOnly, selectableEnvironments,
5
+ environmentStatusOf, findEnvironment,
6
6
  type EnvironmentLoadPhase, type EnvironmentOption, type EnvironmentStatus,
7
7
  } from "../lib/environment-context"
8
+ import { NOTHING_OPEN, createEnvironmentController } from "../lib/environment-controller"
8
9
 
9
10
  export interface EnvironmentContextValue {
10
11
  status: EnvironmentStatus
@@ -45,19 +46,6 @@ export interface EnvironmentContextProviderProps {
45
46
  requestedEnv?: string | null
46
47
  }
47
48
 
48
- interface Loaded {
49
- projectId: string
50
- phase: EnvironmentLoadPhase
51
- environments: EnvironmentOption[]
52
- }
53
-
54
- interface ReadOptions {
55
- announce: boolean
56
- reportLost: boolean
57
- }
58
-
59
- const NOTHING_LOADED: Loaded = { projectId: "", phase: "loading", environments: [] }
60
-
61
49
  export function EnvironmentContextProvider({
62
50
  children,
63
51
  enabled = true,
@@ -69,76 +57,29 @@ export function EnvironmentContextProvider({
69
57
  requestedEnv,
70
58
  }: EnvironmentContextProviderProps) {
71
59
  const project = enabled ? (projectId ?? "") : ""
72
- const [loaded, setLoaded] = useState<Loaded>(NOTHING_LOADED)
73
- const [lost, setLost] = useState<string | null>(null)
74
60
 
75
- const latest = useRef(latestOnly()).current
76
61
  const live = useRef({ currentEnv, currentEnvAlias, onSelect, fetchEnvironments, requestedEnv })
77
62
  live.current = { currentEnv, currentEnvAlias, onSelect, fetchEnvironments, requestedEnv }
78
- const requestHonoured = useRef(false)
79
- const previousProject = useRef("")
80
-
81
- const read = useCallback(async (target: string, { announce, reportLost }: ReadOptions): Promise<EnvironmentOption[]> => {
82
- const ticket = latest.begin()
83
- if (announce) {
84
- setLoaded({ projectId: target, phase: "loading", environments: [] })
85
- setLost(null)
86
- }
87
-
88
- let environments: EnvironmentOption[]
89
- try {
90
- const answer = await live.current.fetchEnvironments(target)
91
- environments = selectableEnvironments(answer?.envs, target)
92
- } catch (error) {
93
- if (announce && latest.isCurrent(ticket)) setLoaded({ projectId: target, phase: "failed", environments: [] })
94
- throw error
95
- }
96
- if (!latest.isCurrent(ticket)) return environments
97
63
 
98
- const requested = requestHonoured.current ? null : live.current.requestedEnv
99
- requestHonoured.current = true
100
- const remembered = live.current.currentEnv
101
- const knownAlias = live.current.currentEnvAlias
102
- const choice = chooseEnvironment(environments, { requested, remembered })
64
+ const controller = useMemo(() => createEnvironmentController({
65
+ fetchEnvironments: target => live.current.fetchEnvironments(target),
66
+ remembered: () => ({ name: live.current.currentEnv, alias: live.current.currentEnvAlias }),
67
+ requested: () => live.current.requestedEnv,
68
+ select: (name, alias) => live.current.onSelect(name, alias),
69
+ }), [])
103
70
 
104
- setLoaded({ projectId: target, phase: "loaded", environments })
105
- const renamed = !!choice.name && knownAlias !== undefined && choice.alias !== knownAlias
106
- if (choice.name !== remembered || renamed) live.current.onSelect(choice.name, choice.alias)
107
- setLost(reportLost && choice.lost ? (knownAlias || choice.lost) : null)
108
- return environments
109
- }, [latest])
71
+ const snapshot = useSyncExternalStore(controller.subscribe, controller.snapshot, () => NOTHING_OPEN)
110
72
 
111
73
  useEffect(() => {
112
- const switched = !!previousProject.current && previousProject.current !== project
113
- previousProject.current = project
114
- if (!project) {
115
- latest.begin()
116
- setLoaded(NOTHING_LOADED)
117
- setLost(null)
118
- return
119
- }
120
- read(project, { announce: true, reportLost: !switched }).catch(() => {})
121
- }, [project, read, latest])
74
+ void controller.open(project)
75
+ }, [controller, project])
122
76
 
123
- const known = loaded.projectId === project
124
- const environments = known ? loaded.environments : NOTHING_LOADED.environments
125
- const phase: EnvironmentLoadPhase = known ? loaded.phase : "loading"
77
+ const known = snapshot.projectId === project
78
+ const environments = known ? snapshot.environments : NOTHING_OPEN.environments
79
+ const phase: EnvironmentLoadPhase = known ? snapshot.phase : "loading"
80
+ const lost = known ? snapshot.lost : null
126
81
  const status = environmentStatusOf({ enabled, projectId: project, phase, count: environments.length })
127
82
 
128
- const select = useCallback((name: string) => {
129
- const chosen = findEnvironment(environments, name)
130
- if (!chosen) return
131
- setLost(null)
132
- live.current.onSelect(chosen.name, environmentLabel(chosen))
133
- }, [environments])
134
-
135
- const reload = useCallback(async (): Promise<EnvironmentOption[]> => {
136
- if (!project) return []
137
- return read(project, { announce: false, reportLost: true })
138
- }, [project, read])
139
-
140
- const dismissLost = useCallback(() => setLost(null), [])
141
-
142
83
  const current = useMemo<EnvironmentOption | null>(() => {
143
84
  if (status === "ready") return findEnvironment(environments, currentEnv)
144
85
  if (status === "unavailable" && currentEnv) return { name: currentEnv, alias: currentEnvAlias || currentEnv }
@@ -146,8 +87,17 @@ export function EnvironmentContextProvider({
146
87
  }, [status, environments, currentEnv, currentEnvAlias])
147
88
 
148
89
  const value = useMemo<EnvironmentContextValue>(
149
- () => ({ status, projectId: project, environments, current, lost, select, reload, dismissLost }),
150
- [status, project, environments, current, lost, select, reload, dismissLost],
90
+ () => ({
91
+ status,
92
+ projectId: project,
93
+ environments,
94
+ current,
95
+ lost,
96
+ select: controller.select,
97
+ reload: controller.reload,
98
+ dismissLost: controller.dismissLost,
99
+ }),
100
+ [status, project, environments, current, lost, controller],
151
101
  )
152
102
 
153
103
  return <EnvironmentContext.Provider value={value}>{children}</EnvironmentContext.Provider>
package/index.ts CHANGED
@@ -5,6 +5,7 @@ export * from './lib/error-utils'
5
5
  export { BaseService } from './lib/base-service'
6
6
  export * from './lib/config-selection'
7
7
  export * from './lib/environment-context'
8
+ export * from './lib/environment-controller'
8
9
  export { createPlatformConfigSource, templateFilter, configOfHit, environmentWithTargets, type ConfigSource } from './lib/config-source'
9
10
  export { default as HttpClient, useHttpClient, ApiError } from './lib/http-client'
10
11
 
@@ -0,0 +1,109 @@
1
+ import {
2
+ chooseEnvironment, environmentLabel, findEnvironment, latestOnly, selectableEnvironments,
3
+ type EnvironmentLoadPhase, type EnvironmentOption,
4
+ } from "./environment-context"
5
+
6
+ export interface EnvironmentSnapshot {
7
+ projectId: string
8
+ phase: EnvironmentLoadPhase
9
+ environments: EnvironmentOption[]
10
+ lost: string | null
11
+ }
12
+
13
+ export interface EnvironmentPorts {
14
+ fetchEnvironments: (projectId: string) => Promise<{ envs?: EnvironmentOption[] | null } | null | undefined>
15
+ remembered: () => { name: string; alias?: string }
16
+ requested: () => string | null | undefined
17
+ select: (name: string, alias: string) => void
18
+ }
19
+
20
+ export interface EnvironmentController {
21
+ snapshot: () => EnvironmentSnapshot
22
+ subscribe: (listener: () => void) => () => void
23
+ open: (projectId: string) => Promise<void>
24
+ reload: () => Promise<EnvironmentOption[]>
25
+ select: (name: string) => void
26
+ dismissLost: () => void
27
+ }
28
+
29
+ interface ReadOptions {
30
+ announce: boolean
31
+ reportLost: boolean
32
+ }
33
+
34
+ const NO_ENVIRONMENTS: EnvironmentOption[] = []
35
+
36
+ export const NOTHING_OPEN: EnvironmentSnapshot = { projectId: "", phase: "loading", environments: NO_ENVIRONMENTS, lost: null }
37
+
38
+ export function createEnvironmentController(ports: EnvironmentPorts): EnvironmentController {
39
+ const latest = latestOnly()
40
+ const listeners = new Set<() => void>()
41
+ let state = NOTHING_OPEN
42
+ let requestHonoured = false
43
+
44
+ function publish(next: EnvironmentSnapshot) {
45
+ state = next
46
+ listeners.forEach(listener => listener())
47
+ }
48
+
49
+ async function read(projectId: string, { announce, reportLost }: ReadOptions): Promise<EnvironmentOption[]> {
50
+ const ticket = latest.begin()
51
+ if (announce) publish({ projectId, phase: "loading", environments: NO_ENVIRONMENTS, lost: null })
52
+
53
+ let environments: EnvironmentOption[]
54
+ try {
55
+ const answer = await ports.fetchEnvironments(projectId)
56
+ environments = selectableEnvironments(answer?.envs, projectId)
57
+ } catch (error) {
58
+ if (announce && latest.isCurrent(ticket)) publish({ projectId, phase: "failed", environments: NO_ENVIRONMENTS, lost: null })
59
+ throw error
60
+ }
61
+ if (!latest.isCurrent(ticket)) return environments
62
+
63
+ const requested = requestHonoured ? null : ports.requested()
64
+ requestHonoured = true
65
+ const remembered = ports.remembered()
66
+ const choice = chooseEnvironment(environments, { requested, remembered: remembered.name })
67
+
68
+ const lost = reportLost && choice.lost ? (remembered.alias || choice.lost) : null
69
+ publish({ projectId, phase: "loaded", environments, lost })
70
+ const renamed = !!choice.name && remembered.alias !== undefined && choice.alias !== remembered.alias
71
+ if (choice.name !== remembered.name || renamed) ports.select(choice.name, choice.alias)
72
+ return environments
73
+ }
74
+
75
+ return {
76
+ snapshot: () => state,
77
+
78
+ subscribe(listener) {
79
+ listeners.add(listener)
80
+ return () => { listeners.delete(listener) }
81
+ },
82
+
83
+ async open(projectId) {
84
+ const switched = !!state.projectId && state.projectId !== projectId
85
+ if (!projectId) {
86
+ latest.begin()
87
+ publish(NOTHING_OPEN)
88
+ return
89
+ }
90
+ await read(projectId, { announce: true, reportLost: !switched }).catch(() => {})
91
+ },
92
+
93
+ async reload() {
94
+ if (!state.projectId) return []
95
+ return read(state.projectId, { announce: false, reportLost: true })
96
+ },
97
+
98
+ select(name) {
99
+ const chosen = findEnvironment(state.environments, name)
100
+ if (!chosen) return
101
+ if (state.lost) publish({ ...state, lost: null })
102
+ ports.select(chosen.name, environmentLabel(chosen))
103
+ },
104
+
105
+ dismissLost() {
106
+ if (state.lost) publish({ ...state, lost: null })
107
+ },
108
+ }
109
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orsetra/shared-ui",
3
- "version": "1.11.0",
3
+ "version": "1.11.1",
4
4
  "description": "Shared UI components for Orsetra platform",
5
5
  "main": "./index.ts",
6
6
  "types": "./index.ts",