web/satellite: migrate NavigationArea component to use SFC composition api

Change-Id: Ic213e0c5af326c1a761868786b4f24244e907dcc
This commit is contained in:
Vitalii 2023-03-28 12:31:43 +03:00 committed by Storj Robot
parent d8ac6889ab
commit 6e11f92b1a

View File

@ -79,14 +79,15 @@
</div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue';
import { AnalyticsHttpApi } from '@/api/analytics';
import { RouteConfig } from '@/router';
import { NavigationLink } from '@/types/navigation';
import { APP_STATE_ACTIONS } from '@/utils/constants/actionNames';
import { APP_STATE_DROPDOWNS } from '@/utils/constants/appStatePopUps';
import { useRouter, useStore } from '@/utils/hooks';
import ProjectSelection from '@/components/navigation/ProjectSelection.vue';
import GuidesDropdown from '@/components/navigation/GuidesDropdown.vue';
@ -104,188 +105,183 @@ import ResourcesIcon from '@/../static/images/navigation/resources.svg';
import QuickStartIcon from '@/../static/images/navigation/quickStart.svg';
import ArrowIcon from '@/../static/images/navigation/arrowExpandRight.svg';
// @vue/component
@Component({
components: {
QuickStartLinks,
ResourcesLinks,
ProjectSelection,
GuidesDropdown,
AccountArea,
LogoIcon,
SmallLogoIcon,
DashboardIcon,
AccessGrantsIcon,
UsersIcon,
BucketsIcon,
ResourcesIcon,
QuickStartIcon,
ArrowIcon,
},
})
export default class NavigationArea extends Vue {
private readonly TWENTY_PIXELS = 20;
private readonly analytics: AnalyticsHttpApi = new AnalyticsHttpApi();
const store = useStore();
const nativeRouter = useRouter();
const router = reactive(nativeRouter);
public resourcesDropdownYPos = 0;
public resourcesDropdownXPos = 0;
public quickStartDropdownYPos = 0;
public quickStartDropdownXPos = 0;
public navigation: NavigationLink[] = [
RouteConfig.ProjectDashboard.withIcon(DashboardIcon),
RouteConfig.Buckets.withIcon(BucketsIcon),
RouteConfig.AccessGrants.withIcon(AccessGrantsIcon),
RouteConfig.Users.withIcon(UsersIcon),
];
const TWENTY_PIXELS = 20;
const analytics: AnalyticsHttpApi = new AnalyticsHttpApi();
const navigation: NavigationLink[] = [
RouteConfig.ProjectDashboard.withIcon(DashboardIcon),
RouteConfig.Buckets.withIcon(BucketsIcon),
RouteConfig.AccessGrants.withIcon(AccessGrantsIcon),
RouteConfig.Users.withIcon(UsersIcon),
];
public $refs!: {
resourcesContainer: HTMLDivElement;
quickStartContainer: HTMLDivElement;
navigationContainer: HTMLDivElement;
};
const resourcesDropdownYPos = ref<number>(0);
const resourcesDropdownXPos = ref<number>(0);
const quickStartDropdownYPos = ref<number>(0);
const quickStartDropdownXPos = ref<number>(0);
const resourcesContainer = ref<HTMLDivElement>();
const quickStartContainer = ref<HTMLDivElement>();
const navigationContainer = ref<HTMLDivElement>();
const windowWidth = ref<number>(window.innerWidth);
private windowWidth = window.innerWidth;
/**
* Indicates if resources dropdown shown.
*/
const isResourcesDropdownShown = computed((): boolean => {
return store.state.appStateModule.viewsState.activeDropdown === APP_STATE_DROPDOWNS.RESOURCES;
});
/**
* Mounted hook after initial render.
* Adds scroll event listener to close dropdowns.
*/
public mounted(): void {
this.$refs.navigationContainer.addEventListener('scroll', this.closeDropdowns);
window.addEventListener('resize', this.onResize);
/**
* Indicates if quick start dropdown shown.
*/
const isQuickStartDropdownShown = computed((): boolean => {
return store.state.appStateModule.viewsState.activeDropdown === APP_STATE_DROPDOWNS.QUICK_START;
});
/**
* Indicates if all projects dashboard should be used.
*/
const isAllProjectsDashboard = computed((): boolean => {
return store.state.appStateModule.isAllProjectsDashboard;
});
/**
* On screen resize handler.
*/
function onResize(): void {
windowWidth.value = window.innerWidth;
closeDropdowns();
}
/**
* Redirects to project dashboard.
*/
function onLogoClick(): void {
if (isAllProjectsDashboard.value) {
router.push(RouteConfig.AllProjectsDashboard.path);
return;
}
/**
* Mounted hook before component destroy.
* Removes scroll event listener.
*/
public beforeDestroy(): void {
this.$refs.navigationContainer.removeEventListener('scroll', this.closeDropdowns);
window.removeEventListener('resize', this.onResize);
if (router.currentRoute.name === RouteConfig.ProjectDashboard.name) {
return;
}
/**
* On screen resize handler.
*/
public onResize(): void {
this.windowWidth = window.innerWidth;
this.closeDropdowns();
router.push(RouteConfig.ProjectDashboard.path);
}
/**
* Sets resources dropdown Y position depending on container's current position.
* It is used to handle small screens.
*/
function setResourcesDropdownYPos(): void {
if (!resourcesContainer.value) {
return;
}
/**
* Redirects to project dashboard.
*/
public onLogoClick(): void {
if (this.isAllProjectsDashboard) {
this.$router.push(RouteConfig.AllProjectsDashboard.path);
return;
}
const container = resourcesContainer.value.getBoundingClientRect();
resourcesDropdownYPos.value = container.top + container.height / 2;
}
if (this.$route.name === RouteConfig.ProjectDashboard.name) {
return;
}
this.$router.push(RouteConfig.ProjectDashboard.path);
/**
* Sets resources dropdown X position depending on container's current position.
* It is used to handle small screens.
*/
function setResourcesDropdownXPos(): void {
if (!resourcesContainer.value) {
return;
}
/**
* Sets resources dropdown Y position depending on container's current position.
* It is used to handle small screens.
*/
public setResourcesDropdownYPos(): void {
const container = this.$refs.resourcesContainer.getBoundingClientRect();
this.resourcesDropdownYPos = container.top + container.height / 2;
resourcesDropdownXPos.value = resourcesContainer.value.getBoundingClientRect().width - TWENTY_PIXELS;
}
/**
* Sets quick start dropdown Y position depending on container's current position.
* It is used to handle small screens.
*/
function setQuickStartDropdownYPos(): void {
if (!quickStartContainer.value) {
return;
}
/**
* Sets resources dropdown X position depending on container's current position.
* It is used to handle small screens.
*/
public setResourcesDropdownXPos(): void {
this.resourcesDropdownXPos = this.$refs.resourcesContainer.getBoundingClientRect().width - this.TWENTY_PIXELS;
const container = quickStartContainer.value.getBoundingClientRect();
quickStartDropdownYPos.value = container.top + container.height / 2;
}
/**
* Sets quick start dropdown X position depending on container's current position.
* It is used to handle small screens.
*/
function setQuickStartDropdownXPos(): void {
if (!quickStartContainer.value) {
return;
}
/**
* Sets quick start dropdown Y position depending on container's current position.
* It is used to handle small screens.
*/
public setQuickStartDropdownYPos(): void {
const container = this.$refs.quickStartContainer.getBoundingClientRect();
this.quickStartDropdownYPos = container.top + container.height / 2;
}
quickStartDropdownXPos.value = quickStartContainer.value.getBoundingClientRect().width - TWENTY_PIXELS;
}
/**
* Sets quick start dropdown X position depending on container's current position.
* It is used to handle small screens.
*/
public setQuickStartDropdownXPos(): void {
this.quickStartDropdownXPos = this.$refs.quickStartContainer.getBoundingClientRect().width - this.TWENTY_PIXELS;
}
/**
* Toggles resources dropdown visibility.
*/
function toggleResourcesDropdown(): void {
setResourcesDropdownYPos();
setResourcesDropdownXPos();
store.dispatch(APP_STATE_ACTIONS.TOGGLE_ACTIVE_DROPDOWN, APP_STATE_DROPDOWNS.RESOURCES);
}
/**
* Toggles resources dropdown visibility.
*/
public toggleResourcesDropdown(): void {
this.setResourcesDropdownYPos();
this.setResourcesDropdownXPos();
this.$store.dispatch(APP_STATE_ACTIONS.TOGGLE_ACTIVE_DROPDOWN, APP_STATE_DROPDOWNS.RESOURCES);
}
/**
* Toggles quick start dropdown visibility.
*/
function toggleQuickStartDropdown(): void {
setQuickStartDropdownYPos();
setQuickStartDropdownXPos();
store.dispatch(APP_STATE_ACTIONS.TOGGLE_ACTIVE_DROPDOWN, APP_STATE_DROPDOWNS.QUICK_START);
}
/**
* Toggles quick start dropdown visibility.
*/
public toggleQuickStartDropdown(): void {
this.setQuickStartDropdownYPos();
this.setQuickStartDropdownXPos();
this.$store.dispatch(APP_STATE_ACTIONS.TOGGLE_ACTIVE_DROPDOWN, APP_STATE_DROPDOWNS.QUICK_START);
}
/**
* Closes dropdowns.
*/
function closeDropdowns(): void {
store.dispatch(APP_STATE_ACTIONS.CLOSE_POPUPS);
}
/**
* Closes dropdowns.
*/
public closeDropdowns(): void {
this.$store.dispatch(APP_STATE_ACTIONS.CLOSE_POPUPS);
}
/**
* Indicates if resources dropdown shown.
*/
public get isResourcesDropdownShown(): boolean {
return this.$store.state.appStateModule.viewsState.activeDropdown === APP_STATE_DROPDOWNS.RESOURCES;
}
/**
* Indicates if quick start dropdown shown.
*/
public get isQuickStartDropdownShown(): boolean {
return this.$store.state.appStateModule.viewsState.activeDropdown === APP_STATE_DROPDOWNS.QUICK_START;
}
/**
* Indicates if all projects dashboard should be used.
*/
public get isAllProjectsDashboard(): boolean {
return this.$store.state.appStateModule.isAllProjectsDashboard;
}
/**
* Sends new path click event to segment.
*/
public trackClickEvent(path: string): void {
if (path === '/account/billing') {
this.routeToOverview();
} else {
this.analytics.pageVisit(path);
}
}
/**
* Routes for new billing screens.
*/
public routeToOverview(): void {
this.$router.push(RouteConfig.Account.with(RouteConfig.Billing).with(RouteConfig.BillingOverview).path);
/**
* Sends new path click event to segment.
*/
function trackClickEvent(path: string): void {
if (path === '/account/billing') {
routeToOverview();
} else {
analytics.pageVisit(path);
}
}
/**
* Routes for new billing screens.
*/
function routeToOverview(): void {
router.push(RouteConfig.Account.with(RouteConfig.Billing).with(RouteConfig.BillingOverview).path);
}
/**
* Mounted hook after initial render.
* Adds scroll event listener to close dropdowns.
*/
onMounted(() => {
navigationContainer.value?.addEventListener('scroll', closeDropdowns);
window.addEventListener('resize', onResize);
});
/**
* Mounted hook before component destroy.
* Removes scroll event listener.
*/
onBeforeUnmount(() => {
navigationContainer.value?.removeEventListener('scroll', closeDropdowns);
window.removeEventListener('resize', onResize);
});
</script>
<style scoped lang="scss">