debian-mirror-gitlab/app/assets/javascripts/super_sidebar/components/context_switcher.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
5.6 KiB
Vue
Raw Normal View History

2023-03-17 16:20:25 +05:30
<script>
2023-05-27 22:25:52 +05:30
import * as Sentry from '@sentry/browser';
2023-07-09 08:55:56 +05:30
import { GlDisclosureDropdown, GlSearchBoxByType, GlLoadingIcon, GlAlert } from '@gitlab/ui';
2023-03-17 16:20:25 +05:30
import { s__ } from '~/locale';
2023-05-27 22:25:52 +05:30
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
import searchUserProjectsAndGroups from '../graphql/queries/search_user_groups_and_projects.query.graphql';
import { trackContextAccess, formatContextSwitcherItems } from '../utils';
2023-07-09 08:55:56 +05:30
import { maxSize, applyMaxSize } from '../popper_max_size_modifier';
2023-03-17 16:20:25 +05:30
import NavItem from './nav_item.vue';
2023-05-27 22:25:52 +05:30
import ProjectsList from './projects_list.vue';
import GroupsList from './groups_list.vue';
2023-07-09 08:55:56 +05:30
import ContextSwitcherToggle from './context_switcher_toggle.vue';
2023-03-17 16:20:25 +05:30
export default {
2023-05-27 22:25:52 +05:30
i18n: {
contextNavigation: s__('Navigation|Context navigation'),
2023-07-09 08:55:56 +05:30
switchTo: s__('Navigation|Switch context'),
2023-06-20 00:43:36 +05:30
searchPlaceholder: s__('Navigation|Search your projects or groups'),
searchingLabel: s__('Navigation|Retrieving search results'),
searchError: s__('Navigation|There was an error fetching search results.'),
2023-05-27 22:25:52 +05:30
},
apollo: {
groupsAndProjects: {
query: searchUserProjectsAndGroups,
manual: true,
variables() {
return {
username: this.username,
search: this.searchString,
};
},
result(response) {
2023-06-20 00:43:36 +05:30
this.hasError = false;
2023-05-27 22:25:52 +05:30
try {
const {
data: {
projects: { nodes: projects },
user: {
groups: { nodes: groups },
},
},
} = response;
this.projects = formatContextSwitcherItems(projects);
this.groups = formatContextSwitcherItems(groups);
} catch (e) {
2023-06-20 00:43:36 +05:30
this.handleError(e);
2023-05-27 22:25:52 +05:30
}
},
error(e) {
2023-06-20 00:43:36 +05:30
this.handleError(e);
2023-05-27 22:25:52 +05:30
},
skip() {
return !this.searchString;
},
},
},
2023-03-17 16:20:25 +05:30
components: {
2023-07-09 08:55:56 +05:30
GlDisclosureDropdown,
ContextSwitcherToggle,
2023-03-17 16:20:25 +05:30
GlSearchBoxByType,
2023-06-20 00:43:36 +05:30
GlLoadingIcon,
GlAlert,
2023-03-17 16:20:25 +05:30
NavItem,
2023-05-27 22:25:52 +05:30
ProjectsList,
GroupsList,
2023-03-17 16:20:25 +05:30
},
2023-05-27 22:25:52 +05:30
props: {
2023-06-20 00:43:36 +05:30
persistentLinks: {
type: Array,
required: true,
},
2023-05-27 22:25:52 +05:30
username: {
type: String,
required: true,
},
projectsPath: {
type: String,
required: true,
},
groupsPath: {
type: String,
required: true,
},
currentContext: {
type: Object,
required: false,
default: () => ({}),
},
2023-07-09 08:55:56 +05:30
contextHeader: {
type: Object,
required: true,
},
2023-03-17 16:20:25 +05:30
},
2023-05-27 22:25:52 +05:30
data() {
return {
searchString: '',
projects: [],
groups: [],
2023-06-20 00:43:36 +05:30
hasError: false,
2023-07-09 08:55:56 +05:30
isOpen: false,
2023-05-27 22:25:52 +05:30
};
2023-03-17 16:20:25 +05:30
},
2023-05-27 22:25:52 +05:30
computed: {
isSearch() {
return Boolean(this.searchString);
},
2023-06-20 00:43:36 +05:30
isSearching() {
return this.$apollo.queries.groupsAndProjects.loading;
},
2023-05-27 22:25:52 +05:30
},
2023-07-09 08:55:56 +05:30
watch: {
isOpen(isOpen) {
this.$emit('toggle', isOpen);
if (isOpen) {
this.focusInput();
}
},
},
2023-05-27 22:25:52 +05:30
created() {
if (this.currentContext.namespace) {
trackContextAccess(this.username, this.currentContext);
}
2023-03-17 16:20:25 +05:30
},
2023-06-20 00:43:36 +05:30
methods: {
2023-07-09 08:55:56 +05:30
close() {
this.$refs['disclosure-dropdown'].close();
},
2023-06-20 00:43:36 +05:30
focusInput() {
this.$refs['search-box'].focusInput();
},
handleError(e) {
Sentry.captureException(e);
this.hasError = true;
},
2023-07-09 08:55:56 +05:30
onDisclosureDropdownShown() {
this.isOpen = true;
},
onDisclosureDropdownHidden() {
this.isOpen = false;
},
2023-06-20 00:43:36 +05:30
},
DEFAULT_DEBOUNCE_AND_THROTTLE_MS,
2023-07-09 08:55:56 +05:30
popperOptions: {
modifiers: [maxSize, applyMaxSize],
},
2023-03-17 16:20:25 +05:30
};
</script>
<template>
2023-07-09 08:55:56 +05:30
<gl-disclosure-dropdown
ref="disclosure-dropdown"
class="context-switcher gl-w-full"
placement="center"
:popper-options="$options.popperOptions"
@shown="onDisclosureDropdownShown"
@hidden="onDisclosureDropdownHidden"
>
<template #toggle>
<context-switcher-toggle :context="contextHeader" :expanded="isOpen" />
</template>
<div aria-hidden="true" class="gl-font-sm gl-font-weight-bold gl-px-4 gl-pt-3 gl-pb-4">
{{ $options.i18n.switchTo }}
</div>
<div class="gl-p-1 gl-border-t gl-border-b gl-border-gray-50 gl-bg-white">
2023-05-27 22:25:52 +05:30
<gl-search-box-by-type
2023-06-20 00:43:36 +05:30
ref="search-box"
2023-05-27 22:25:52 +05:30
v-model="searchString"
class="context-switcher-search-box"
:placeholder="$options.i18n.searchPlaceholder"
2023-06-20 00:43:36 +05:30
:debounce="$options.DEFAULT_DEBOUNCE_AND_THROTTLE_MS"
2023-05-27 22:25:52 +05:30
borderless
/>
</div>
2023-06-20 00:43:36 +05:30
<gl-loading-icon
v-if="isSearching"
class="gl-mt-5"
size="md"
:label="$options.i18n.searchingLabel"
/>
<gl-alert v-else-if="hasError" variant="danger" :dismissible="false" class="gl-m-2">
{{ $options.i18n.searchError }}
</gl-alert>
2023-07-09 08:55:56 +05:30
<nav v-else :aria-label="$options.i18n.contextNavigation" data-qa-selector="context_navigation">
<ul class="gl-p-0 gl-m-0 gl-list-style-none">
2023-05-27 22:25:52 +05:30
<li v-if="!isSearch">
2023-07-09 08:55:56 +05:30
<ul
:aria-label="$options.i18n.switchTo"
class="gl-border-b gl-border-gray-50 gl-px-0 gl-py-2"
>
2023-06-20 00:43:36 +05:30
<nav-item
v-for="item in persistentLinks"
:key="item.link"
:item="item"
:link-classes="{ [item.link_classes]: item.link_classes }"
/>
2023-03-17 16:20:25 +05:30
</ul>
</li>
2023-05-27 22:25:52 +05:30
<projects-list
:username="username"
:view-all-link="projectsPath"
:is-search="isSearch"
:search-results="projects"
/>
<groups-list
2023-07-09 08:55:56 +05:30
class="gl-border-t gl-border-gray-50"
2023-05-27 22:25:52 +05:30
:username="username"
:view-all-link="groupsPath"
:is-search="isSearch"
:search-results="groups"
/>
2023-03-17 16:20:25 +05:30
</ul>
</nav>
2023-07-09 08:55:56 +05:30
</gl-disclosure-dropdown>
2023-03-17 16:20:25 +05:30
</template>