-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace-context-status-controller.js
More file actions
97 lines (80 loc) · 2.45 KB
/
workspace-context-status-controller.js
File metadata and controls
97 lines (80 loc) · 2.45 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const hasTokenValue = token => typeof token === 'string' && token.trim().length > 0
const createWorkspaceContextStatusController = ({
statusNode,
toNonEmptyWorkspaceText,
getWorkspacePrTitle,
getWorkspaceHeadBranch,
getWorkspaceScopeMarker,
getActiveWorkspaceRecordId,
getWorkspaceRepositoryFullName,
getSelectedRepositoryFullName,
}) => {
let hasValidatedGitHubPat = false
let hasCompletedRepositoryLoad = false
const appGrid =
statusNode instanceof HTMLElement ? statusNode.closest('.app-grid') : null
const getWorkspaceName = () => {
const prTitle = toNonEmptyWorkspaceText(getWorkspacePrTitle?.())
if (prTitle) {
return prTitle
}
const headBranch = toNonEmptyWorkspaceText(getWorkspaceHeadBranch?.())
if (headBranch) {
return headBranch
}
return toNonEmptyWorkspaceText(getActiveWorkspaceRecordId?.()) || 'unknown'
}
const render = () => {
if (!(statusNode instanceof HTMLElement)) {
return
}
if (appGrid instanceof HTMLElement) {
appGrid.classList.toggle(
'app-grid--workspace-context-visible',
hasValidatedGitHubPat,
)
}
statusNode.toggleAttribute('hidden', !hasValidatedGitHubPat)
if (!hasValidatedGitHubPat) {
return
}
const workspaceName = getWorkspaceName()
const workspaceScope =
toNonEmptyWorkspaceText(getWorkspaceScopeMarker?.()).toLowerCase() || 'local'
const repository =
workspaceScope === 'local'
? 'local'
: toNonEmptyWorkspaceText(getWorkspaceRepositoryFullName?.()) ||
toNonEmptyWorkspaceText(getSelectedRepositoryFullName?.()) ||
'unknown'
statusNode.textContent = `${workspaceName} • ${repository}`
}
const renderForRepositoryChange = () => {
render()
}
const syncTokenState = token => {
if (!hasTokenValue(token)) {
hasValidatedGitHubPat = false
hasCompletedRepositoryLoad = false
} else if (hasCompletedRepositoryLoad) {
hasValidatedGitHubPat = true
}
render()
}
const syncWritableRepositoriesState = ({ token, isLoadingRepositories = false }) => {
if (!isLoadingRepositories) {
hasCompletedRepositoryLoad = true
}
if (hasTokenValue(token) && !isLoadingRepositories) {
hasValidatedGitHubPat = true
}
render()
}
return {
render,
renderForRepositoryChange,
syncTokenState,
syncWritableRepositoriesState,
}
}
export { createWorkspaceContextStatusController }