2022-06-19 00:25:21 +00:00
|
|
|
import constants from "./constants";
|
2019-11-02 19:40:59 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
import {createRouter, createWebHashHistory} from "vue-router";
|
2019-11-16 17:24:03 +00:00
|
|
|
import SignIn from "../components/Windows/SignIn.vue";
|
|
|
|
import Connect from "../components/Windows/Connect.vue";
|
|
|
|
import Settings from "../components/Windows/Settings.vue";
|
|
|
|
import Help from "../components/Windows/Help.vue";
|
|
|
|
import Changelog from "../components/Windows/Changelog.vue";
|
|
|
|
import NetworkEdit from "../components/Windows/NetworkEdit.vue";
|
2019-12-31 16:21:34 +00:00
|
|
|
import SearchResults from "../components/Windows/SearchResults.vue";
|
2019-11-16 17:24:03 +00:00
|
|
|
import RoutedChat from "../components/RoutedChat.vue";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {store} from "./store";
|
2019-10-17 16:56:44 +00:00
|
|
|
|
2022-02-19 00:45:25 +00:00
|
|
|
import AppearanceSettings from "../components/Settings/Appearance.vue";
|
|
|
|
import GeneralSettings from "../components/Settings/General.vue";
|
2022-02-20 01:05:09 +00:00
|
|
|
import AccountSettings from "../components/Settings/Account.vue";
|
2022-02-19 00:45:25 +00:00
|
|
|
import NotificationSettings from "../components/Settings/Notifications.vue";
|
2022-06-19 00:25:21 +00:00
|
|
|
import {ClientChan} from "./types";
|
2022-02-19 00:45:25 +00:00
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
const router = createRouter({
|
|
|
|
history: createWebHashHistory(),
|
2019-10-17 16:56:44 +00:00
|
|
|
routes: [
|
2019-10-20 16:17:21 +00:00
|
|
|
{
|
2019-11-03 17:09:10 +00:00
|
|
|
name: "SignIn",
|
2019-10-20 16:17:21 +00:00
|
|
|
path: "/sign-in",
|
|
|
|
component: SignIn,
|
2019-11-07 16:48:45 +00:00
|
|
|
beforeEnter(to, from, next) {
|
|
|
|
// Prevent navigating to sign-in when already signed in
|
|
|
|
if (store.state.appLoaded) {
|
|
|
|
next(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
2019-10-20 16:17:21 +00:00
|
|
|
},
|
2020-07-20 08:38:12 +00:00
|
|
|
{
|
|
|
|
name: "Connect",
|
|
|
|
path: "/connect",
|
|
|
|
component: Connect,
|
|
|
|
props: (route) => ({queryParams: route.query}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "/settings",
|
|
|
|
component: Settings,
|
2022-02-19 00:45:25 +00:00
|
|
|
children: [
|
|
|
|
{
|
|
|
|
name: "General",
|
|
|
|
path: "",
|
|
|
|
component: GeneralSettings,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Appearance",
|
|
|
|
path: "appearance",
|
|
|
|
component: AppearanceSettings,
|
|
|
|
},
|
|
|
|
{
|
2022-02-20 01:05:09 +00:00
|
|
|
name: "Account",
|
|
|
|
path: "account",
|
|
|
|
component: AccountSettings,
|
2022-06-19 00:25:21 +00:00
|
|
|
props: true,
|
2022-02-19 00:45:25 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Notifications",
|
|
|
|
path: "notifications",
|
|
|
|
component: NotificationSettings,
|
|
|
|
},
|
|
|
|
],
|
2020-07-20 08:38:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Help",
|
|
|
|
path: "/help",
|
|
|
|
component: Help,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Changelog",
|
|
|
|
path: "/changelog",
|
|
|
|
component: Changelog,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NetworkEdit",
|
|
|
|
path: "/edit-network/:uuid",
|
|
|
|
component: NetworkEdit,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RoutedChat",
|
|
|
|
path: "/chan-:id",
|
|
|
|
component: RoutedChat,
|
|
|
|
},
|
2021-01-26 21:43:56 +00:00
|
|
|
{
|
|
|
|
name: "SearchResults",
|
2021-04-29 23:07:31 +00:00
|
|
|
path: "/chan-:id/search",
|
2021-01-26 21:43:56 +00:00
|
|
|
component: SearchResults,
|
|
|
|
},
|
2019-11-03 15:56:41 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2020-07-20 08:38:12 +00:00
|
|
|
router.beforeEach((to, from, next) => {
|
|
|
|
// If user is not yet signed in, wait for appLoaded state to change
|
|
|
|
// unless they are trying to open SignIn (which can be triggered in auth.js)
|
|
|
|
if (!store.state.appLoaded && to.name !== "SignIn") {
|
|
|
|
store.watch(
|
|
|
|
(state) => state.appLoaded,
|
|
|
|
() => next()
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2019-11-07 16:48:45 +00:00
|
|
|
router.beforeEach((to, from, next) => {
|
2019-11-08 11:58:58 +00:00
|
|
|
// Disallow navigating to non-existing routes
|
2020-07-20 08:38:12 +00:00
|
|
|
if (!to.matched.length) {
|
2019-11-08 11:58:58 +00:00
|
|
|
next(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:56:18 +00:00
|
|
|
// Disallow navigating to invalid channels
|
2019-11-11 19:18:55 +00:00
|
|
|
if (to.name === "RoutedChat" && !store.getters.findChannel(Number(to.params.id))) {
|
2019-11-08 12:56:18 +00:00
|
|
|
next(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:49:53 +00:00
|
|
|
// Disallow navigating to invalid networks
|
2022-06-19 00:25:21 +00:00
|
|
|
if (to.name === "NetworkEdit" && !store.getters.findNetwork(String(to.params.uuid))) {
|
2019-11-07 16:48:45 +00:00
|
|
|
next(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2019-11-23 15:18:44 +00:00
|
|
|
router.afterEach((to) => {
|
2019-11-05 19:29:51 +00:00
|
|
|
if (store.state.appLoaded) {
|
2019-11-07 23:47:45 +00:00
|
|
|
if (window.innerWidth <= constants.mobileViewportPixels) {
|
|
|
|
store.commit("sidebarOpen", false);
|
|
|
|
}
|
2019-11-03 15:56:41 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 19:18:55 +00:00
|
|
|
if (store.state.activeChannel) {
|
|
|
|
const channel = store.state.activeChannel.channel;
|
2019-11-23 15:18:44 +00:00
|
|
|
|
|
|
|
if (to.name !== "RoutedChat") {
|
2022-06-19 00:25:21 +00:00
|
|
|
store.commit("activeChannel", undefined);
|
2019-11-23 15:18:44 +00:00
|
|
|
}
|
2019-11-03 15:56:41 +00:00
|
|
|
|
2019-11-11 19:18:55 +00:00
|
|
|
// When switching out of a channel, mark everything as read
|
2022-06-19 00:25:21 +00:00
|
|
|
if (channel.messages?.length > 0) {
|
2019-11-11 19:18:55 +00:00
|
|
|
channel.firstUnread = channel.messages[channel.messages.length - 1].id;
|
2019-11-03 15:56:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
if (channel.messages?.length > 100) {
|
2019-11-11 19:18:55 +00:00
|
|
|
channel.messages.splice(0, channel.messages.length - 100);
|
|
|
|
channel.moreHistoryAvailable = true;
|
|
|
|
}
|
2019-11-03 15:56:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
async function navigate(routeName: string, params: any = {}) {
|
|
|
|
if (router.currentRoute.value.name) {
|
|
|
|
await router.push({name: routeName, params});
|
2019-11-24 13:54:24 +00:00
|
|
|
} else {
|
|
|
|
// If current route is null, replace the history entry
|
|
|
|
// This prevents invalid entries from lingering in history,
|
|
|
|
// and then the route guard preventing proper navigation
|
2022-06-19 00:25:21 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
|
|
await router.replace({name: routeName, params}).catch(() => {});
|
2019-11-24 13:54:24 +00:00
|
|
|
}
|
2019-11-11 19:18:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-19 00:25:21 +00:00
|
|
|
function switchToChannel(channel: ClientChan) {
|
|
|
|
void navigate("RoutedChat", {id: channel.id});
|
2019-11-16 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-03 18:02:22 +00:00
|
|
|
if ("serviceWorker" in navigator) {
|
|
|
|
navigator.serviceWorker.addEventListener("message", (event) => {
|
|
|
|
if (event.data && event.data.type === "open") {
|
2022-06-19 00:25:21 +00:00
|
|
|
const id = parseInt(event.data.channel.substring(5), 10); // remove "chan-" prefix
|
2020-01-03 18:02:22 +00:00
|
|
|
|
|
|
|
const channelTarget = store.getters.findChannel(id);
|
|
|
|
|
|
|
|
if (channelTarget) {
|
|
|
|
switchToChannel(channelTarget.channel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-20 08:38:12 +00:00
|
|
|
export {router, navigate, switchToChannel};
|