From 88332ddf5904644cd9d6962973a31bb4175b1a2a Mon Sep 17 00:00:00 2001 From: "Gloire Mutaliko (Salva)" <86450367+GloireMutaliko21@users.noreply.github.com> Date: Fri, 5 Jul 2024 18:45:09 +0200 Subject: [PATCH] feat: store and remember the latest selected workspace and team (#2686) --- .../app/[locale]/auth/passcode/component.tsx | 12 +++++++- .../app/[locale]/auth/password/component.tsx | 12 +++++++- .../app/[locale]/auth/workspace/component.tsx | 30 ++++++++++++++++++- apps/web/app/constants.ts | 4 +++ 4 files changed, 55 insertions(+), 3 deletions(-) diff --git a/apps/web/app/[locale]/auth/passcode/component.tsx b/apps/web/app/[locale]/auth/passcode/component.tsx index b133743f2..05e51941a 100644 --- a/apps/web/app/[locale]/auth/passcode/component.tsx +++ b/apps/web/app/[locale]/auth/passcode/component.tsx @@ -26,6 +26,7 @@ import stc from 'string-to-color'; import { ScrollArea, ScrollBar } from '@components/ui/scroll-bar'; import SocialLogins from '../social-logins-buttons'; import { useSession } from 'next-auth/react'; +import { LAST_WORSPACE_AND_TEAM, USER_SAW_OUTSTANDING_NOTIFICATION } from '@app/constants'; function AuthPasscode() { const form = useAuthenticationPasscode(); @@ -317,7 +318,8 @@ function WorkSpaceScreen({ form, className }: { form: TAuthenticationPasscode } (e: any) => { if (typeof selectedWorkspace !== 'undefined') { form.handleWorkspaceSubmit(e, form.workspaces[selectedWorkspace].token, selectedTeam); - window && window?.localStorage.removeItem('user-saw-notif'); + window && window?.localStorage.removeItem(USER_SAW_OUTSTANDING_NOTIFICATION); + window && window?.localStorage.setItem(LAST_WORSPACE_AND_TEAM, selectedTeam); } }, [selectedWorkspace, selectedTeam, form] @@ -332,6 +334,14 @@ function WorkSpaceScreen({ form, className }: { form: TAuthenticationPasscode } if (form.workspaces.length === 1 && currentTeams?.length === 1) { setSelectedTeam(currentTeams[0].team_id); + } else { + const lastSelectedTeam = window.localStorage.getItem(LAST_WORSPACE_AND_TEAM) || currentTeams[0].team_id; + const lastSelectedWorkspace = + form.workspaces.findIndex((workspace) => + workspace.current_teams.find((team) => team.team_id === lastSelectedTeam) + ) || 0; + setSelectedTeam(lastSelectedTeam); + setSelectedWorkspace(lastSelectedWorkspace); } if (form.workspaces.length === 1 && (currentTeams?.length || 0) <= 1) { diff --git a/apps/web/app/[locale]/auth/password/component.tsx b/apps/web/app/[locale]/auth/password/component.tsx index 08dcba15c..b50d94bbc 100644 --- a/apps/web/app/[locale]/auth/password/component.tsx +++ b/apps/web/app/[locale]/auth/password/component.tsx @@ -12,6 +12,7 @@ import { useRouter } from 'next/navigation'; import { useCallback, useEffect, useState } from 'react'; import { WorkSpaceComponent } from '../passcode/component'; import SocialLogins from '../social-logins-buttons'; +import { LAST_WORSPACE_AND_TEAM, USER_SAW_OUTSTANDING_NOTIFICATION } from '@app/constants'; export default function AuthPassword() { const t = useTranslations(); @@ -108,7 +109,8 @@ function WorkSpaceScreen({ form, className }: { form: TAuthenticationPassword } (e: any) => { if (typeof selectedWorkspace !== 'undefined') { form.handleWorkspaceSubmit(e, form.workspaces[selectedWorkspace].token, selectedTeam); - window && window?.localStorage.removeItem('user-saw-notif'); + window && window?.localStorage.removeItem(USER_SAW_OUTSTANDING_NOTIFICATION); + window && window?.localStorage.setItem(LAST_WORSPACE_AND_TEAM, selectedTeam); } }, [selectedWorkspace, selectedTeam, form] @@ -123,6 +125,14 @@ function WorkSpaceScreen({ form, className }: { form: TAuthenticationPassword } if (form.workspaces.length === 1 && currentTeams?.length === 1) { setSelectedTeam(currentTeams[0].team_id); + } else { + const lastSelectedTeam = window.localStorage.getItem(LAST_WORSPACE_AND_TEAM) || currentTeams[0].team_id; + const lastSelectedWorkspace = + form.workspaces.findIndex((workspace) => + workspace.current_teams.find((team) => team.team_id === lastSelectedTeam) + ) || 0; + setSelectedTeam(lastSelectedTeam); + setSelectedWorkspace(lastSelectedWorkspace); } if (form.workspaces.length === 1 && (currentTeams?.length || 0) <= 1) { diff --git a/apps/web/app/[locale]/auth/workspace/component.tsx b/apps/web/app/[locale]/auth/workspace/component.tsx index 6bc8a2778..57605412c 100644 --- a/apps/web/app/[locale]/auth/workspace/component.tsx +++ b/apps/web/app/[locale]/auth/workspace/component.tsx @@ -10,6 +10,7 @@ import { useAuthenticationSocialLogin } from '@app/hooks/auth/useAuthenticationS import { ISigninEmailConfirmWorkspaces } from '@app/interfaces'; import Cookies from 'js-cookie'; import { useSession } from 'next-auth/react'; +import { LAST_WORSPACE_AND_TEAM, USER_SAW_OUTSTANDING_NOTIFICATION } from '@app/constants'; export default function SocialLoginChooseWorspace() { const t = useTranslations(); @@ -57,6 +58,32 @@ function WorkSpaceScreen() { loadOAuthSession(); }, [session]); + useEffect(() => { + if (workspaces.length === 1) { + setSelectedWorkspace(0); + } + + const currentTeams = workspaces[0]?.current_teams; + + if (workspaces.length === 1 && currentTeams?.length === 1) { + setSelectedTeam(currentTeams[0].team_id); + } else { + const lastSelectedTeam = window.localStorage.getItem(LAST_WORSPACE_AND_TEAM) || currentTeams[0].team_id; + const lastSelectedWorkspace = + workspaces.findIndex((workspace) => + workspace.current_teams.find((team) => team.team_id === lastSelectedTeam) + ) || 0; + setSelectedTeam(lastSelectedTeam); + setSelectedWorkspace(lastSelectedWorkspace); + } + + if (workspaces.length === 1 && (currentTeams?.length || 0) <= 1) { + setTimeout(() => { + document.getElementById('continue-to-workspace')?.click(); + }, 100); + } + }, [workspaces]); + const signInToWorkspace = (e: any) => { e.preventDefault(); updateOAuthSession(); @@ -64,7 +91,8 @@ function WorkSpaceScreen() { new Array(3).fill('').forEach((_, i) => { Cookies.remove(`authjs.session-token.${i}`); }); - window && window?.localStorage.removeItem('user-saw-notif'); + window && window?.localStorage.removeItem(USER_SAW_OUTSTANDING_NOTIFICATION); + window && window?.localStorage.setItem(LAST_WORSPACE_AND_TEAM, selectedTeam); }; const updateOAuthSession = useCallback(() => { diff --git a/apps/web/app/constants.ts b/apps/web/app/constants.ts index bc7dd312e..e9ba40c66 100644 --- a/apps/web/app/constants.ts +++ b/apps/web/app/constants.ts @@ -264,6 +264,10 @@ export const languagesFlags = [ } ]; +// Local storage keys +export const LAST_WORSPACE_AND_TEAM = 'last-workspace-and-team'; +export const USER_SAW_OUTSTANDING_NOTIFICATION = 'user-saw-notif'; + // OAuth providers keys export const APPLE_CLIENT_ID = process.env.APPLE_CLIENT_ID;