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.

177 lines
4.5 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-06-20 00:43:36 +05:30
import { 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-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-03-17 16:20:25 +05:30
export default {
2023-05-27 22:25:52 +05:30
i18n: {
contextNavigation: s__('Navigation|Context navigation'),
switchTo: s__('Navigation|Switch to...'),
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: {
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-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-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
},
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: {
/**
* This needs to be exposed publicly so that we can auto-focus the search input when the parent
* GlCollapse is shown.
*/
focusInput() {
this.$refs['search-box'].focusInput();
},
handleError(e) {
Sentry.captureException(e);
this.hasError = true;
},
},
DEFAULT_DEBOUNCE_AND_THROTTLE_MS,
2023-03-17 16:20:25 +05:30
};
</script>
<template>
<div>
2023-05-27 22:25:52 +05:30
<div class="gl-p-1 gl-border-b gl-border-gray-50 gl-bg-white">
<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>
<nav v-else :aria-label="$options.i18n.contextNavigation">
2023-03-17 16:20:25 +05:30
<ul class="gl-p-0 gl-list-style-none">
2023-05-27 22:25:52 +05:30
<li v-if="!isSearch">
2023-03-17 16:20:25 +05:30
<div aria-hidden="true" class="gl-font-weight-bold gl-px-3 gl-py-3">
{{ $options.i18n.switchTo }}
</div>
<ul :aria-label="$options.i18n.switchTo" class="gl-p-0">
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
:username="username"
:view-all-link="groupsPath"
:is-search="isSearch"
:search-results="groups"
/>
2023-03-17 16:20:25 +05:30
</ul>
</nav>
</div>
</template>