-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathSidebarNavigation.vue
269 lines (232 loc) · 7.86 KB
/
SidebarNavigation.vue
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<template>
<HideDuringScreenshot
id="sidebar"
data-cy="sidebar"
class="flex flex-col bg-gray-1000 border-gray-900 border-r-1 transition-all duration-300 relative"
:class="isNavBarExpanded ? 'w-248px' : 'w-64px'"
>
<button
v-if="navIsAlwaysCollapsed"
class="cursor-pointer left-full top-0 bottom-0 w-16px z-1 absolute group hocus:outline-transparent"
type="button"
:aria-label="isNavBarExpanded ? t('sidebar.toggleLabel.expanded') : t('sidebar.toggleLabel.collapsed')"
data-cy="toggle-sidebar"
:aria-expanded="isNavBarExpanded"
aria-controls="sidebar"
@click="toggleNavbarIfAllowed"
>
<div
data-cy="sidebar-nav-indicator"
class="flex h-full transform origin-left transition-transform w-16px scale-x-0 duration-300 items-center group-hocus:scale-x-100"
>
<div class="h-full bg-indigo-400 w-3px" />
<i-cy-chevron-right_x16
class="h-16px w-16px icon-dark-indigo-400"
:class="isNavBarExpanded ? 'transform rotate-180': ''"
/>
</div>
</button>
<div class="flex flex-col flex-1 ">
<SidebarNavigationHeader
v-if="props.gql"
:gql="props.gql"
:is-nav-bar-expanded="isNavBarExpanded"
/>
<nav
class="space-y-1 bg-gray-1000 flex-1"
:aria-label="t('sidebar.nav.ariaLabel')"
>
<RouterLink
v-for="item in navigation"
v-slot="{ isExactActive }"
:key="item.name"
:to="item.href"
:data-cy="`sidebar-link-${item.name.toLowerCase()}-page`"
>
<SidebarNavigationRow
:active="isExactActive"
:icon="item.icon"
:name="item.name"
:is-nav-bar-expanded="isNavBarExpanded"
:badge="item.badge"
/>
</RouterLink>
</nav>
<Tooltip
placement="right"
:disabled="isNavBarExpanded"
:distance="8"
:skidding="-16"
>
<button
data-cy="keyboard-modal-trigger"
type="button"
class="border border-transparent rounded
cursor-pointer h-32px m-16px
p-7px transform transition-all
right-0 bottom-0 w-32px duration-300
inline-block absolute hover:border-gray-500"
:class="{ '-translate-y-48px': !isNavBarExpanded }"
:aria-label="t('sidebar.keyboardShortcuts.title')"
@click="bindingsOpen = true"
>
<i-cy-command-key_x16 class="h-16px w-16px icon-dark-gray-500" />
</button>
<template #popper>
{{ t('sidebar.keyboardShortcuts.title') }}
</template>
</Tooltip>
<KeyboardBindingsModal
:show="bindingsOpen"
@close="bindingsOpen = false"
/>
<img
:src="CypressLogo"
class="h-32px m-16px w-32px"
alt="Cypress"
>
</div>
</HideDuringScreenshot>
</template>
<script lang="ts" setup>
import { computed, FunctionalComponent, ref, watchEffect } from 'vue'
import { gql, useMutation } from '@urql/vue'
import SidebarNavigationRow, { Badge } from './SidebarNavigationRow.vue'
import KeyboardBindingsModal from './KeyboardBindingsModal.vue'
import {
IconTechnologyCodeEditor,
IconTechnologyTestResults,
IconObjectGear,
IconObjectBug,
} from '@cypress-design/vue-icon'
import Tooltip from '@packages/frontend-shared/src/components/Tooltip.vue'
import HideDuringScreenshot from '../runner/screenshot/HideDuringScreenshot.vue'
import { SidebarNavigationFragment, SideBarNavigation_SetPreferencesDocument } from '../generated/graphql'
import CypressLogo from '@packages/frontend-shared/src/assets/logos/cypress_s.png'
import { useI18n } from '@cy/i18n'
import { useRoute } from 'vue-router'
import SidebarNavigationHeader from './SidebarNavigationHeader.vue'
import { useDebounceFn, useWindowSize } from '@vueuse/core'
import { useLoginConnectStore } from '@packages/frontend-shared/src/store/login-connect-store'
import { isAllowedFeature } from '@packages/frontend-shared/src/utils/isAllowedFeature'
const { t } = useI18n()
gql`
fragment SidebarNavigation_Settings on Query {
localSettings {
preferences {
isSideNavigationOpen
isSpecsListOpen
autoScrollingEnabled
reporterWidth
specListWidth
}
}
}
`
gql`
fragment SidebarNavigation on Query {
...SidebarNavigation_Settings
currentProject {
id
cloudProject {
__typename
... on CloudProject {
id
runByNumber(runNumber: $runNumber) @include(if: $hasCurrentRun){
id
status
totalFailed
}
}
}
}
...SidebarNavigationHeader
}
`
gql`
mutation SideBarNavigation_SetPreferences ($value: String!) {
setPreferences (value: $value, type: global) {
...SidebarNavigation_Settings
}
}`
const props = defineProps<{
gql: SidebarNavigationFragment | undefined
isLoading: boolean
online: boolean
}>()
const NAV_EXPAND_MIN_SCREEN_WIDTH = 1024
const loginConnectStore = useLoginConnectStore()
const debugBadge = ref<Badge | undefined>()
const setDebugBadge = useDebounceFn((badge) => {
debugBadge.value = badge
}, 500)
watchEffect(() => {
if (props.isLoading && loginConnectStore.project.isProjectConnected) {
setDebugBadge(undefined)
return
}
const showNewBadge = isAllowedFeature('debugNewBadge', loginConnectStore)
const newBadge: Badge = { value: t('sidebar.debug.new'), status: 'success', label: t('sidebar.debug.debugFeature') }
if (props.gql?.currentProject?.cloudProject?.__typename === 'CloudProject'
&& props.gql.currentProject.cloudProject.runByNumber
&& props.online
) {
const { status, totalFailed } = props.gql.currentProject.cloudProject.runByNumber || {}
if (status === 'NOTESTS' || status === 'RUNNING') {
setDebugBadge(showNewBadge ? newBadge : undefined)
return
}
if (status === 'PASSED') {
setDebugBadge({ value: '0', status: 'success', label: t('sidebar.debug.passed') })
return
}
let countToDisplay = '0'
if (totalFailed) {
countToDisplay = totalFailed < 99
? String(totalFailed)
: '99+'
}
if (status === 'FAILED') {
setDebugBadge({
value: countToDisplay,
status: 'failed',
label: t('sidebar.debug.failed', totalFailed || 0),
})
return
}
const errorLabel = totalFailed && totalFailed > 0
? t('sidebar.debug.erroredWithFailures', totalFailed)
: t('sidebar.debug.errored')
setDebugBadge({ value: countToDisplay, status: 'error', label: errorLabel })
return
}
setDebugBadge(showNewBadge ? newBadge : undefined)
})
const navigation = computed<{ name: string, icon: FunctionalComponent, href: string, badge?: Badge }[]>(() => {
return [
{ name: 'Specs', icon: IconTechnologyCodeEditor, href: '/specs' },
{ name: 'Runs', icon: IconTechnologyTestResults, href: '/runs' },
{ name: 'Debug', icon: IconObjectBug, href: '/debug', badge: debugBadge.value },
{ name: 'Settings', icon: IconObjectGear, href: '/settings' },
]
})
const setPreferences = useMutation(SideBarNavigation_SetPreferencesDocument)
const bindingsOpen = ref(false)
const route = useRoute()
const navIsAlwaysCollapsed = computed(() => width.value >= NAV_EXPAND_MIN_SCREEN_WIDTH && route.meta?.navBarExpandedAllowed !== false)
const isNavBarExpanded = ref(true)
const { width } = useWindowSize()
watchEffect(() => {
if (width.value < NAV_EXPAND_MIN_SCREEN_WIDTH || route.meta?.navBarExpandedAllowed === false) {
isNavBarExpanded.value = false
} else {
isNavBarExpanded.value = props.gql?.localSettings.preferences.isSideNavigationOpen ?? true
}
})
const toggleNavbarIfAllowed = () => {
if (width.value < NAV_EXPAND_MIN_SCREEN_WIDTH || route.meta?.navBarExpandedAllowed === false) {
return
}
setPreferences.executeMutation({ value: JSON.stringify({ isSideNavigationOpen: !isNavBarExpanded.value }) })
}
</script>