DocSpace-client/packages/components/utils/banner.js

66 lines
1.7 KiB
JavaScript

import difference from "lodash/difference";
import { LANGUAGE } from "COMMON_DIR/constants";
import { getLanguage } from "COMMON_DIR/utils";
import { getCookie } from "../utils/cookie";
export const getBannerAttribute = () => {
const bar = document.getElementById("bar-banner");
const mainBar = document.getElementById("main-bar");
const rects = mainBar ? mainBar.getBoundingClientRect() : null;
const headerHeight = bar ? 108 + 50 : mainBar ? rects.height + 40 : 48 + 50;
const sectionHeaderTop = bar
? "108px"
: rects
? rects.height + 40 + "px"
: "48px";
const sectionHeaderMarginTop = bar
? "106px"
: rects
? rects.height + 36 + "px"
: "46px";
const loadLanguagePath = async () => {
if (!window.firebaseHelper) return;
const lng = getCookie(LANGUAGE) || "en";
const language = getLanguage(lng instanceof Array ? lng[0] : lng);
const bar = (localStorage.getItem("bar") || "")
.split(",")
.filter((bar) => bar.length > 0);
const closed = JSON.parse(localStorage.getItem("barClose"));
const banner = difference(bar, closed);
let index = Number(localStorage.getItem("barIndex") || 0);
if (index >= banner.length) {
index -= 1;
}
const currentBar = banner[index];
let htmlUrl =
currentBar && window.firebaseHelper.config.authDomain
? `https://${window.firebaseHelper.config.authDomain}/${language}/${currentBar}/index.html`
: null;
if (htmlUrl) {
await fetch(htmlUrl).then((data) => {
if (data.ok) return;
htmlUrl = null;
});
}
return [htmlUrl, currentBar];
};
return {
headerHeight,
sectionHeaderTop,
sectionHeaderMarginTop,
loadLanguagePath,
};
};