Skip to content

Commit

Permalink
[sc-7748] Don't load all KBs from all accounts after login (#1100)
Browse files Browse the repository at this point in the history
* [sc-7748] Don't load all KBs from all accounts after login

* Fix 1 account case and prod build

* Move standalone logic in the services and guards

* Disable login button while logging in

Login validation can take some time, and it's a bit confusing to see nothing happening right after clicking on the button when that's happening (typically for users with 1 account and 1 KB). Disabling the button while validating allows the user to understand the click was taken into account.

* Better error message
  • Loading branch information
mpellerin42 authored Nov 7, 2023
1 parent 9e39089 commit b5014ee
Show file tree
Hide file tree
Showing 21 changed files with 266 additions and 258 deletions.
2 changes: 2 additions & 0 deletions apps/contributor/src/app/app-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SearchComponent,
SelectAccountComponent,
SelectKbComponent,
selectKbGuard,
UploadDataComponent,
} from '@flaps/common';

Expand Down Expand Up @@ -95,6 +96,7 @@ export const routes: Routes = [
{
path: ':account',
component: SelectKbComponent,
canActivate: [selectKbGuard],
},
],
},
Expand Down
6 changes: 4 additions & 2 deletions apps/dashboard/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
rootGuard,
SearchComponent,
SelectAccountComponent,
selectAccountKbGuard,
selectAccountGuard,
SelectKbComponent,
selectKbGuard,
UploadDataComponent,
} from '@flaps/common';
import { authGuard } from '@flaps/core';
Expand Down Expand Up @@ -183,11 +184,12 @@ const routes: Routes = [
{
path: 'select',
component: SelectAccountComponent,
canActivate: [authGuard, selectAccountKbGuard],
canActivate: [authGuard, selectAccountGuard],
children: [
{
path: ':account',
component: SelectKbComponent,
canActivate: [selectKbGuard],
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/assets/i18n/ca.json
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@
"stash.email": "Servidor de correu",
"stash.empty_kb": "La Knowledge Box encara està buida!",
"stash.enable_stash_email": "Habilitar correu electrònic",
"stash.home.link": "Tauler d'inici",
"stash.home.link": "Inici",
"stash.home.upload-block.description": "Carregueu fitxers, carpetes, enllaços",
"stash.home.upload-block.description-desktop": " o trieu una font per treballar amb Nuclia amb la vostra pròpia base de dades.",
"stash.home.upload-block.title": "Treballa amb les teves dades",
Expand All @@ -690,7 +690,7 @@
"stash.logotip": "Logotip",
"stash.manage_users": "Gestionar usuaris",
"stash.name": "Nom de la Knowledge Box",
"stash.name_your_kb": "Anomena el teu KB",
"stash.name_your_kb": "Anomena el teu Knowledge Box",
"stash.new_service_access_token": "Creeu una nova clau d'API",
"stash.no_knowledge_boxes": "No hi ha cap cas de Knowledge Boxes en aquest compte",
"stash.preferences.receive_help": "Rebre ajuda",
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/assets/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"stash.email": "E-Mail-Server",
"stash.empty_kb": "Die Knowledge Box ist leer",
"stash.enable_stash_email": "E-Mail aktivieren",
"stash.home.link": "Start-Dashboard",
"stash.home.link": "Startseite",
"stash.home.upload-block.description": "Laden Sie Dateien, Ordner und Links hoch",
"stash.home.upload-block.description-desktop": " oder wählen Sie eine Quelle, um mit Nuclia mit Ihrer eigenen Datenbank zu arbeiten.",
"stash.home.upload-block.title": "Arbeiten Sie mit Ihren Daten",
Expand All @@ -691,7 +691,7 @@
"stash.logotip": "Logo",
"stash.manage_users": "Benutzer verwalten",
"stash.name": "Name der Knowledge Box",
"stash.name_your_kb": "Benennen Sie Ihre KB",
"stash.name_your_kb": "Benennen Sie Ihre Knowledge Box",
"stash.new_service_access_token": "Erstellen Sie einen neuen API-Schlüssel",
"stash.no_knowledge_boxes": "In diesem Konto sind keine Knowledge Boxes vorhanden",
"stash.preferences.receive_help": "Hilfe erhalten",
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"stash.email": "Email server",
"stash.empty_kb": "The Knowledge Box is empty",
"stash.enable_stash_email": "Enable email",
"stash.home.link": "Home dashboard",
"stash.home.link": "Home",
"stash.home.upload-block.description": "Upload files, folders, links",
"stash.home.upload-block.description-desktop": " or choose a source to work with Nuclia with your own database.",
"stash.home.upload-block.title": "Work with your data",
Expand All @@ -691,7 +691,7 @@
"stash.logotip": "Logo",
"stash.manage_users": "Manage users",
"stash.name": "Knowledge Box name",
"stash.name_your_kb": "Name your KB",
"stash.name_your_kb": "Name your Knowledge Box",
"stash.new_service_access_token": "Create a new API key",
"stash.no_knowledge_boxes": "There are no Knowledge Boxes in this account",
"stash.preferences.receive_help": "Received help",
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/assets/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@
"stash.email": "Servidor de correo",
"stash.empty_kb": "!La Knowledge Box aun está vacía!",
"stash.enable_stash_email": "Habilitar correo electrónico",
"stash.home.link": "Panel de inicio",
"stash.home.link": "Inicio",
"stash.home.upload-block.description": "Subir archivos, carpetas, enlaces.",
"stash.home.upload-block.description-desktop": " o elige una fuente para trabajar con Nuclia con tu propia base de datos.",
"stash.home.upload-block.title": "Trabaja con tus datos",
Expand All @@ -690,7 +690,7 @@
"stash.logotip": "Logotipo",
"stash.manage_users": "Gestionar usuarios",
"stash.name": "Nombre de la Knowledge Box",
"stash.name_your_kb": "Nombra tu base de conocimiento",
"stash.name_your_kb": "Nombra tu Knowledge Box",
"stash.new_service_access_token": "Crear una nueva clave API",
"stash.no_knowledge_boxes": "No hay Knowledge Boxes en esta cuenta.",
"stash.preferences.receive_help": "Recibir ayuda",
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/assets/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"stash.email": "Serveur de messagerie",
"stash.empty_kb": "La Knowledge Box est vide",
"stash.enable_stash_email": "Activer l'e-mail",
"stash.home.link": "Tableau de bord d'accueil",
"stash.home.link": "Accueil",
"stash.home.upload-block.description": "Téléchargez des fichiers, des dossiers, des liens",
"stash.home.upload-block.description-desktop": " ou choisissez une source pour travailler avec Nuclia avec votre propre base de données.",
"stash.home.upload-block.title": "Travaillez avec vos données",
Expand All @@ -691,7 +691,7 @@
"stash.logotip": "Logo",
"stash.manage_users": "Gérer les utilisateurs",
"stash.name": "Nom de la Knowledge Box",
"stash.name_your_kb": "Nommez votre base de connaissances",
"stash.name_your_kb": "Nommez votre Knowledge Box",
"stash.new_service_access_token": "Créer une nouvelle clé API",
"stash.no_knowledge_boxes": "Il n'y a pas de Knowledge Boxes dans ce compte",
"stash.preferences.receive_help": "Reçu de l'aide",
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/assets/i18n/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@
"stash.email": "Servidor de e-mail",
"stash.empty_kb": "A Knowledge Box está vazia",
"stash.enable_stash_email": "Ativar e-mail",
"stash.home.link": "Painel Inicial",
"stash.home.link": "Casa",
"stash.home.upload-block.description": "Carregar arquivos, pastas, links",
"stash.home.upload-block.description-desktop": " ou escolha uma fonte para trabalhar com a Nuclia com seu próprio banco de dados.",
"stash.home.upload-block.title": "Trabalhe com seus dados",
Expand All @@ -691,7 +691,7 @@
"stash.logotip": "Logotipo",
"stash.manage_users": "Gerenciar usuários",
"stash.name": "Nome da Knowledge Box",
"stash.name_your_kb": "Nomeie sua base de conhecimento",
"stash.name_your_kb": "Nomeie sua Knowledge Box",
"stash.new_service_access_token": "Crie uma nova chave de API",
"stash.no_knowledge_boxes": "Não há Knowledge Boxes nesta conta",
"stash.preferences.receive_help": "Ajuda recebida",
Expand Down
3 changes: 2 additions & 1 deletion libs/common/src/lib/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './auth.interceptor';
export * from './permission.guard';
export * from './root.guard';
export * from './select-account-kb.guard';
export * from './select-account.guard';
export * from './select-kb.guard';
67 changes: 0 additions & 67 deletions libs/common/src/lib/guards/select-account-kb.guard.ts

This file was deleted.

49 changes: 49 additions & 0 deletions libs/common/src/lib/guards/select-account.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { inject } from '@angular/core';
import { SelectAccountKbService } from '@flaps/common';
import { catchError, of, switchMap } from 'rxjs';
import { map } from 'rxjs/operators';

export const selectAccountGuard = (route: ActivatedRouteSnapshot) => {
const selectService: SelectAccountKbService = inject(SelectAccountKbService);
const router: Router = inject(Router);

const selectedAccount = route.children[0]?.paramMap.get('account');

if (selectedAccount) {
// when loading `/select/{account}` (happens when there is only 1 account and when we reload the KB selection page)
// set account in state and continue
return selectService.accounts.pipe(
switchMap((accounts) => {
const selectAccount$ = selectService.selectAccount(selectedAccount);
if (!accounts) {
// on reload, we need to load accounts list
return selectService.loadAccounts().pipe(switchMap(() => selectAccount$));
} else {
return selectAccount$;
}
}),
map(() => true),
);
}
return selectService.loadAccounts().pipe(
switchMap((accounts) => {
// No accounts
if (accounts.length === 0) {
return of(router.createUrlTree(['/user/onboarding']));
} else if (accounts.length === 1) {
const accountSlug = accounts[0].slug;
// redirect to kb selection
return selectService
.selectAccount(accountSlug)
.pipe(map((account) => router.createUrlTree([`/select/${accountSlug}`])));
} else {
// several accounts, we continue to account selection
return of(true);
}
}),
catchError(() => {
return of(router.createUrlTree(['/user/logout']));
}),
);
};
36 changes: 36 additions & 0 deletions libs/common/src/lib/guards/select-kb.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ActivatedRouteSnapshot, Router } from '@angular/router';
import { NavigationService, SelectAccountKbService } from '@flaps/common';
import { inject } from '@angular/core';
import { SDKService } from '@flaps/core';
import { of, switchMap } from 'rxjs';
import { map } from 'rxjs/operators';

export const selectKbGuard = (route: ActivatedRouteSnapshot) => {
const selectService: SelectAccountKbService = inject(SelectAccountKbService);
const navigation: NavigationService = inject(NavigationService);
const sdk: SDKService = inject(SDKService);
const router: Router = inject(Router);

const accountSlug = route.paramMap.get('account');

return accountSlug
? selectService.loadKbs(accountSlug).pipe(
switchMap((kbs) => {
if (kbs.length === 0) {
return selectService.standalone
? of(true)
: sdk.currentAccount.pipe(
map((account) =>
account.can_manage_account ? router.createUrlTree([navigation.getAccountUrl(accountSlug)]) : true,
),
);
} else if (kbs.length === 1 && !selectService.standalone) {
// if there's only one KB, and we're not in NucliaDB admin app, then we automatically select the KB
return of(router.createUrlTree([navigation.getKbUrl(accountSlug, kbs[0].slug || '')]));
} else {
return of(true);
}
}),
)
: of(router.createUrlTree(['/select']));
};
Loading

0 comments on commit b5014ee

Please sign in to comment.