Skip to content

Commit

Permalink
feat: store and remember the latest selected workspace and team (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
GloireMutaliko21 authored Jul 5, 2024
1 parent 4f6c3c2 commit 88332dd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
12 changes: 11 additions & 1 deletion apps/web/app/[locale]/auth/passcode/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand All @@ -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) {
Expand Down
12 changes: 11 additions & 1 deletion apps/web/app/[locale]/auth/password/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand All @@ -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) {
Expand Down
30 changes: 29 additions & 1 deletion apps/web/app/[locale]/auth/workspace/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -57,14 +58,41 @@ 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();

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(() => {
Expand Down
4 changes: 4 additions & 0 deletions apps/web/app/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 88332dd

Please sign in to comment.