slate/common/navigation-data.js

209 lines
4.4 KiB
JavaScript
Raw Normal View History

import * as Strings from "~/common/strings";
2021-07-27 15:01:44 +03:00
import * as Environment from "~/common/environment";
2021-05-06 03:08:14 +03:00
export const getById = (id, viewer) => {
let target;
if (id) {
target = navigation.find((each) => each.id === id);
}
if (!target) {
return { ...errorPage };
}
if (viewer && target.id === authPage.id) {
2021-07-27 15:01:44 +03:00
return { ...dataPage }; //NOTE(martina): authenticated users should be redirected to the home page rather than the
2021-05-06 03:08:14 +03:00
}
if (!viewer && !target.externalAllowed) {
return { ...authPage }; //NOTE(martina): redirect to sign in page if try to access internal page while logged out
}
return { ...target };
};
export const getByHref = (href, viewer) => {
let pathname;
if (href) {
pathname = href.split("?")[0];
}
if (!pathname) {
return { page: { ...errorPage } };
}
if (pathname === "/_") {
2021-07-27 15:01:44 +03:00
return { page: { ...dataPage } };
2021-05-06 03:08:14 +03:00
}
let page = navigation.find((each) => pathname.startsWith(each.pathname));
let details;
if (page) {
page = { ...page };
if (page.id === "NAV_SLATE" || page.id === "NAV_PROFILE") {
details = {
id: pathname.replace(page.pathname, ""),
};
}
2021-05-06 03:08:14 +03:00
} else {
let params = pathname.slice(1).split("/");
if (params.length === 1) {
page = { ...profilePage };
details = {
username: params[0],
};
} else if (params.length === 2) {
page = { ...slatePage };
details = {
username: params[0],
slatename: params[1],
};
}
}
page.pathname = href;
let redirected = false;
2021-06-11 08:33:46 +03:00
if (viewer && page.id === authPage.id) {
2021-05-06 03:08:14 +03:00
redirected = true;
2021-07-27 15:01:44 +03:00
page = { ...dataPage };
2021-05-06 03:08:14 +03:00
}
if (!viewer && !page.externalAllowed) {
redirected = true;
page = { ...authPage }; //NOTE(martina): redirect to sign in page if try to access internal page while logged out
}
2021-05-06 03:08:14 +03:00
if (!page) {
window.location.replace("/_/404");
}
//NOTE(martina): to transform query params into more easily usable key value pairs in page
if (!redirected) {
let params = Strings.getParamsFromUrl(href);
if (page.id === "NAV_PROFILE" && page.cid) {
params.tab = "FILES";
}
page.params = params;
}
2021-06-11 08:33:46 +03:00
return { page, details, redirected };
2021-05-06 03:08:14 +03:00
};
2021-05-06 03:08:14 +03:00
const authPage = {
id: "NAV_SIGN_IN",
name: "Sign in",
pageTitle: "Sign in & Sign up",
ignore: true,
pathname: "/_/auth",
2021-06-09 01:53:30 +03:00
externalAllowed: true,
2021-05-06 03:08:14 +03:00
};
const dataPage = {
id: "NAV_DATA",
2021-05-25 01:19:48 +03:00
name: "My Slate",
pageTitle: "My Slate",
2021-05-06 03:08:14 +03:00
pathname: "/_/data",
mainNav: true,
};
2021-07-27 15:01:44 +03:00
const collectionsPage = {
id: "NAV_SLATES",
name: "Collections",
pageTitle: "Your Collections",
ignore: true,
pathname: "/_/collections",
mainNav: true,
};
2021-05-06 03:08:14 +03:00
const activityPage = {
id: "NAV_ACTIVITY",
name: "Activity",
pageTitle: "Activity",
ignore: true,
externalAllowed: true,
pathname: "/_/activity",
2021-07-27 15:01:44 +03:00
mainNav: Environment.ACTIVITY_FEATURE_FLAG,
2021-05-06 03:08:14 +03:00
};
const slatePage = {
id: "NAV_SLATE",
name: "Collection",
pageTitle: "A Collection",
ignore: true,
externalAllowed: true,
pathname: "/$/slate/",
};
const profilePage = {
id: "NAV_PROFILE",
name: "Profile",
pageTitle: "A Profile",
ignore: true,
externalAllowed: true,
pathname: "/$/user/",
};
const errorPage = {
id: "NAV_ERROR",
name: "404",
pageTitle: "404 Not found",
ignore: true,
externalAllowed: true,
pathname: "/_/404",
};
export const navigation = [
2021-05-06 03:08:14 +03:00
errorPage,
authPage,
2021-07-27 15:01:44 +03:00
...(Environment.ACTIVITY_FEATURE_FLAG ? [activityPage] : []),
collectionsPage,
2021-05-06 03:08:14 +03:00
dataPage,
// {
// id: "NAV_SEARCH",
// name: "Search",
// pageTitle: "Search Slate",
// ignore: true,
// pathname: "/_/search",
// mainNav: true,
// },
2020-12-09 07:22:17 +03:00
{
2020-12-19 08:25:50 +03:00
id: "NAV_DIRECTORY",
2020-12-18 05:59:41 +03:00
name: "Directory",
2021-05-06 03:08:14 +03:00
pageTitle: "Your Following",
pathname: "/_/directory",
},
2021-05-06 03:08:14 +03:00
slatePage,
2020-09-09 20:56:35 +03:00
{
2020-12-19 08:25:50 +03:00
id: "NAV_FILECOIN",
name: "Filecoin",
2020-09-09 20:56:35 +03:00
pageTitle: "Archive on Filecoin",
2021-05-06 03:08:14 +03:00
pathname: "/_/filecoin",
},
{
2020-12-19 08:25:50 +03:00
id: "NAV_STORAGE_DEAL",
name: "Storage Deal",
2021-05-06 03:08:14 +03:00
pageTitle: "Filecoin Storage Deal",
pathname: "/_/storage-deal",
},
{
2020-12-19 08:25:50 +03:00
id: "NAV_API",
2020-11-16 00:54:14 +03:00
name: "API",
pageTitle: "Developer API",
2021-05-06 03:08:14 +03:00
pathname: "/_/api",
2020-09-23 05:39:46 +03:00
},
{
2020-12-19 08:25:50 +03:00
id: "NAV_SETTINGS",
2021-05-06 03:08:14 +03:00
name: "Settings",
pageTitle: "Profile & Account Settings",
ignore: true,
2021-05-06 03:08:14 +03:00
pathname: "/_/settings",
},
profilePage,
// {
// id: "NAV_FILE",
// name: "File",
// pageTitle: "A File",
// ignore: true,
// externalAllowed: true,
// },
];