Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpeterkort committed Oct 25, 2024
1 parent 41a982f commit 31d0f32
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const Gen3Provider = ({
<MantineProvider theme={theme}>
<ModalsProvider>
<Notifications />
<SessionProvider sessionConfig={sessionConfig}>
<SessionProvider {...sessionConfig}>
<Gen3ModalsProvider config={modalsConfig}>
{children}
</Gen3ModalsProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ const ExplorerTable = ({ index, tableConfig }: ExplorerTableProps) => {
<React.Fragment>
{Object.keys(rowSelection).length > 0 ? (
<DetailsComponent
title={`${getFieldValue(
title={`${String(tableConfig?.detailsConfig?.nodeType).charAt(0).toUpperCase() + String(tableConfig?.detailsConfig?.nodeType).slice(1)} / ${getFieldValue(
tableConfig,
rowSelection,
data?.data?.[index] ?? [],
Expand Down
61 changes: 18 additions & 43 deletions packages/frontend/src/lib/session/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ import React, {
useContext,
useRef,
useState,
useMemo,
} from 'react';
import { useRouter, NextRouter } from 'next/router';
import {
Session,
SessionProviderProps,
SessionConfiguration,
SessionConfig,
} from './types';
import { useRouter } from 'next/router';
import { Session, SessionProviderProps } from './types';
import { isUserOnPage } from './utils';
import { defaultComposer } from 'default-composer';

import {
useCoreDispatch,
useCoreSelector,
Expand All @@ -34,11 +26,6 @@ import { showNotification } from '@mantine/notifications';
const SecondsToMilliseconds = (seconds: number) => seconds * 1000;
const MinutesToMilliseconds = (minutes: number) => minutes * 60 * 1000;

interface Gen3StandardSessionProviderProps {
sessionConfig: SessionConfig;
children: React.ReactNode;
}

export const logoutSession = async () => {
await fetch(`${GEN3_FENCE_API}/user/logout?next=${GEN3_REDIRECT_URL}/`, {
cache: 'no-store',
Expand Down Expand Up @@ -120,15 +107,14 @@ export const useIsAuthenticated = () => {
};

const refreshSession = (
updateSessionTime: number,
getUserDetails: () => void,
mostRecentSessionRefreshTimestamp: number,
updateSessionRefreshTimestamp: (arg0: number) => void,
): void => {
const timeSinceLastSessionUpdate =
Date.now() - mostRecentSessionRefreshTimestamp;
// don't hit Fence to refresh tokens too frequently
if (timeSinceLastSessionUpdate < updateSessionTime) {
if (timeSinceLastSessionUpdate < UPDATE_SESSION_LIMIT) {
return;
}

Expand Down Expand Up @@ -159,6 +145,8 @@ const useInterval = (callback: IntervalFunction, delay: number | null) => {
}, [delay]);
};

const UPDATE_SESSION_LIMIT = MinutesToMilliseconds(5);

/**
* SessionProvider creates a React context which keeps track of wether the user is authenticated
* and if their session is stale and logs them out if they do not preform an action in an alotted amount of time
Expand All @@ -171,26 +159,16 @@ const useInterval = (callback: IntervalFunction, delay: number | null) => {
* @returns a Session context that can be used to keep track of user session activity
*/
export const SessionProvider = ({
sessionConfig,
children,
}: Gen3StandardSessionProviderProps) => {
session,
updateSessionTime = 5,
inactiveTimeLimit = 20,
workspaceInactivityTimeLimit = 0,
logoutInactiveUsers = true,
}: SessionProviderProps) => {
const router = useRouter();
const coreDispatch = useCoreDispatch();

const defaultConfig: SessionConfig = {
sessionConfig: {
updateSessionTime: 5,
inactiveTimeLimit: 20,
logoutInactiveUsers: true,
refetchOnWindowFocus: true,
workspaceInactivityTimeLimit: 50,
},
};
const sesConfig = useMemo(
() => defaultComposer(defaultConfig, sessionConfig),
[],
);

const [getUserDetails] = useLazyFetchUserDetailsQuery(); // Fetch user details
const userStatus = useCoreSelector((state: CoreState) =>
selectUserAuthStatus(state),
Expand All @@ -204,16 +182,14 @@ export const SessionProvider = ({
setMostRecentSessionRefreshTimestamp,
] = useState(Date.now());

const inactiveTimeLimitMilliseconds = MinutesToMilliseconds(
sesConfig.sessionConfig.inactiveTimeLimit ?? 20,
);
const inactiveTimeLimitMilliseconds =
MinutesToMilliseconds(inactiveTimeLimit);

const workspaceInactivityTimeLimitMilliseconds = MinutesToMilliseconds(
sesConfig.sessionConfig.workspaceInactivityTimeLimit ?? 20,
);
const updateSessionIntervalMilliseconds = MinutesToMilliseconds(
sesConfig.sessionConfig.updateSessionTime ?? 5,
workspaceInactivityTimeLimit,
);
const updateSessionIntervalMilliseconds =
MinutesToMilliseconds(updateSessionTime);

// Update session status using the session token api
const updateSessionWithSessionApi = async () => {
Expand Down Expand Up @@ -248,7 +224,7 @@ export const SessionProvider = ({
});
})
.finally(() => {
router.push('/'); // TODO replace with config option
router.push(`${GEN3_REDIRECT_URL}`); // TODO replace with config option
});
};
/**
Expand Down Expand Up @@ -279,7 +255,7 @@ export const SessionProvider = ({

const timeSinceLastActivity = Date.now() - mostRecentActivityTimestamp;

if (sesConfig.sessionConfig.logoutInactiveUsers) {
if (logoutInactiveUsers) {
if (
timeSinceLastActivity >= inactiveTimeLimitMilliseconds &&
!isUserOnPage('workspace')
Expand All @@ -300,7 +276,6 @@ export const SessionProvider = ({
}
// fetching a userState will renew the session
refreshSession(
updateSessionIntervalMilliseconds,
getUserDetails,
mostRecentSessionRefreshTimestamp,
(ts: number) => setMostRecentSessionRefreshTimestamp(ts),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export const UniqueAssociatedValsForSpecimen = ({
}) => {
const { resData, isLoading, isError } = useFilesQuery(identifiers);
if (isError) {
return <Text> Error occurred while fetching data </Text>;
return <Text> Error occurred while fetching file metadata </Text>;
}
if (!isLoading) {
const ResourceList = [...new Set(resData.map((val) => val[asoc_val]))].join(
Expand Down Expand Up @@ -134,7 +134,7 @@ export const AssociatedAssaysTable = ({
<Table.Tr>
<Table.Th>File Name</Table.Th>
<Table.Th>Assay</Table.Th>
<Table.Th>IndexdDays</Table.Th>
<Table.Th>Indexd Days</Table.Th>
<Table.Th> Sample Family Id </Table.Th>
</Table.Tr>
</Table.Thead>
Expand All @@ -161,62 +161,6 @@ export const AssociatedAssaysTable = ({
);
};

export const AssociatedFilesTable = ({
identifiers,
}: {
identifiers: string;
}) => {
const { resData, isLoading, isError } = useFilesQuery([identifiers]);

if (isError) {
return <ErrorCard message={'Error occurred while fetching data'} />;
}

return (
<Stack>
<LoadingOverlay visible={isLoading} />
{resData.length > 0 && (
<div className="text-primary">
<Title className="text-lg p-5 text-center">
Specimen Identifier {identifiers} files
</Title>
<Table>
<Table.Thead>
<Table.Tr>
<Table.Th>Title</Table.Th>
<Table.Th>Download</Table.Th>
</Table.Tr>
</Table.Thead>
<Table.Tbody>
{resData.map((element) => (
<Table.Tr key={element.title}>
<Table.Td>{element.title}</Table.Td>
<Table.Td>
<div className="flex">
<div className="px-2">
<FiDownload title="download" size={16} />
</div>
<Anchor
c="accent.1"
href={`${GEN3_FENCE_API}/user/data/download/${
element.id ? (element.id as string) : ''
}?redirect=true`}
target="_blank"
>
{element.id ? (element.id as string) : ''}
</Anchor>
</div>
</Table.Td>
</Table.Tr>
))}
</Table.Tbody>
</Table>
</div>
)}
</Stack>
);
};

export const AssayCheckboxChart = ({
data,
}: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const ResearchSubjectDetailPanel = ({
<CopyButton value={JSON.stringify(queryData)} timeout={2000}>
{({ copied, copy }) => (
<Tooltip
label={copied ? 'Copied' : 'Copy'}
label={copied ? 'Copied' : 'Copy raw JSON data”'}
withArrow
position="right"
>
Expand Down

0 comments on commit 31d0f32

Please sign in to comment.