Skip to content

Commit

Permalink
Further improve route handling from localStorage
Browse files Browse the repository at this point in the history
Closes #1496
  • Loading branch information
MrBurrBurr committed Jan 17, 2025
1 parent 9c85d24 commit 0fe6207
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ router.beforeEach(async (routeTo, routeFrom, next) => {

router.afterEach(to => {
if (to.name === 'setup') return;
storage.set('last-visited-page', { name: to.name, query: to.query, params: to.params });
storage.set('last-visited-page', to.name);
});

router.onError(err => {
Expand Down
128 changes: 81 additions & 47 deletions src/router/routes.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,77 @@
import store from '../store';
import * as storage from '../utils/storage';

const routes = {
asfBans: 'asf-bans',
asfConfig: 'asf-config',
bot: 'bot',
bot2fa: 'bot-2fa',
bot2faDelete: 'bot-2fa-delete',
botBgr: 'bot-bgr',
botConfig: 'bot-config',
botCopy: 'bot-copy',
botCreate: 'bot-create',
botDelete: 'bot-delete',
botInput: 'bot-input',
bots: 'bots',
commands: 'commands',
home: 'home',
log: 'log',
massEditor: 'mass-editor',
notFound: '404',
passwordEncrypt: 'password-encrypt',
passwordHash: 'password-hash',
plugins: 'plugins',
releases: 'releases',
setup: 'setup',
uiConfig: 'ui-config',
welcome: 'welcome',
};

let defaultView = store.getters['settings/defaultView'];
if (defaultView === '_last-visited-page') defaultView = storage.get('last-visited-page', { name: 'home' });
if (defaultView === '_last-visited-page') defaultView = storage.get('last-visited-page', routes.home);
if (!Object.values(routes).includes(defaultView)) {
defaultView = routes.bots;
store.dispatch('settings/setDefaultView', routes.bots);
}

export default [
{
path: '/',
redirect: (typeof defaultView === 'string') ? { name: defaultView } : defaultView,
redirect: { name: defaultView },
},
{
path: '/home',
name: 'home',
name: routes.home,
async beforeEnter(to, from, next) {
const setupComplete = storage.get('setup-complete', false);
const botsDetected = await store.dispatch('bots/detectBots');

if (!setupComplete && from.name !== 'welcome' && !botsDetected) {
return next({ name: 'welcome' });
if (!setupComplete && from.name !== routes.welcome && !botsDetected) {
return next({ name: routes.welcome });
}

if (from.name === 'welcome' && !botsDetected) {
return next({ name: 'bot-create' });
if (from.name === routes.welcome && !botsDetected) {
return next({ name: routes.botCreate });
}

if (botsDetected) {
storage.set('setup-complete', true);
let defaultView = store.getters['settings/defaultView'];
if (defaultView === '_last-visited-page' || defaultView === 'home') defaultView = storage.get('last-visited-page', { name: 'bots' });
const page = (typeof defaultView === 'string') ? { name: defaultView } : defaultView;
return next(page);
if (defaultView === '_last-visited-page') defaultView = storage.get('last-visited-page', routes.bots);
if (!Object.values(routes).includes(defaultView) || defaultView === routes.home) {
defaultView = routes.bots;
store.dispatch('settings/setDefaultView', routes.bots);
}
return next({ name: defaultView });
}

return next({ name: 'bots' });
return next({ name: routes.bots });
},
},
{
path: '/setup',
name: 'setup',
name: routes.setup,
component: () => import('../views/Setup.vue'),
meta: { noPasswordRequired: true },
params: {
Expand All @@ -47,199 +81,199 @@ export default [
},
{
path: '/ui-config',
name: 'ui-config',
name: routes.uiConfig,
component: () => import('../views/UIConfig.vue'),
},
{
path: '/welcome',
name: 'welcome',
name: routes.welcome,
component: () => import('../views/Welcome.vue'),
meta: { noPasswordRequired: true },
},
{
path: '/releases',
name: 'releases',
name: routes.releases,
component: () => import('../views/Releases.vue'),
},
{
path: '/plugins',
name: 'plugins',
name: routes.plugins,
component: () => import('../views/Plugins.vue'),
},
{
path: '/bots',
name: 'bots',
name: routes.bots,
component: () => import('../views/Bots.vue'),
},
{
path: '/bot/new',
name: 'bot-create',
name: routes.botCreate,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/BotCreate.vue'),
},
meta: {
modal: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot',
name: 'bot',
name: routes.bot,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/Bot.vue'),
},
meta: {
modal: true,
arrows: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/config',
name: 'bot-config',
name: routes.botConfig,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/BotConfig.vue'),
},
meta: {
modal: true,
arrows: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/config/:label/encrypt',
name: 'password-encrypt',
name: routes.passwordEncrypt,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/PasswordEncrypt.vue'),
},
meta: {
modal: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/bgr',
name: 'bot-bgr',
name: routes.botBgr,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/BotBGR.vue'),
},
meta: {
modal: true,
arrows: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/2fa',
name: 'bot-2fa',
name: routes.bot2fa,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/Bot2FA.vue'),
},
meta: {
modal: true,
arrows: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/2fa/delete',
name: 'bot-2fa-delete',
name: routes.bot2faDelete,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/Bot2FADelete.vue'),
},
meta: {
modal: true,
arrows: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/input/:type',
name: 'bot-input',
name: routes.botInput,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/BotInput.vue'),
},
meta: {
modal: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/delete',
name: 'bot-delete',
name: routes.botDelete,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/BotDelete.vue'),
},
meta: {
modal: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot/:bot/copy',
name: 'bot-copy',
name: routes.botCopy,
components: {
default: () => import('../views/Bots.vue'),
modal: () => import('../views/modals/BotCopy.vue'),
},
meta: {
modal: true,
closeRoute: 'bots',
closeRoute: routes.bots,
},
},
{
path: '/bot',
redirect: { name: 'bots' },
redirect: { name: routes.bots },
},
{
path: '/commands',
name: 'commands',
name: routes.commands,
component: () => import('../views/Commands.vue'),
},
{
path: '/log',
name: 'log',
name: routes.log,
component: () => import('../views/Log.vue'),
},
{
path: '/asf-config',
name: 'asf-config',
name: routes.asfConfig,
component: () => import('../views/ASFConfig.vue'),
},
{
path: '/asf-bans',
name: 'asf-bans',
name: routes.asfBans,
component: () => import('../views/ASFBans.vue'),
},
{
path: '/asf-config/:label/hash',
name: 'password-hash',
name: routes.passwordHash,
components: {
default: () => import('../views/ASFConfig.vue'),
modal: () => import('../views/modals/PasswordHash.vue'),
},
meta: {
modal: true,
closeRoute: 'asf-config',
closeRoute: routes.asfConfig,
},
},
{
path: '/mass-editor',
name: 'mass-editor',
name: routes.massEditor,
component: () => import('../views/MassEditor.vue'),
},
{
path: '*',
name: '404',
redirect: { name: 'home' },
name: routes.notFound,
redirect: { name: routes.home },
},
];

0 comments on commit 0fe6207

Please sign in to comment.