Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions web/api/auth/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export const removeUser = async () => {
return await userManager.removeUser()
}

export const invalidateRestoreSessionCache = () => {
lastRestoreSessionResult = null
lastRestoreSessionTime = 0
}

export const restoreSession = async (): Promise<User | undefined> => {
if (typeof window === 'undefined') return

Expand Down
160 changes: 77 additions & 83 deletions web/components/layout/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,89 +468,83 @@ export const Sidebar = ({ isOpen, onClose, ...props }: SidebarProps) => {
<span className="flex grow">{translation('patients')}</span>
{context?.totalPatientsCount !== undefined && (<span className="text-description">{context.totalPatientsCount}</span>)}
</SidebarLink>
{(context?.teams ?? []).length > 0 && (
<ExpandableRoot
className="shadow-none"
isExpanded={context?.sidebar?.isShowingTeams ?? false}
onExpandedChange={isExpanded => context?.update(prevState => ({
...prevState,
sidebar: {
...prevState.sidebar,
isShowingTeams: isExpanded,
}
}))}
>
<ExpandableHeader className="px-2.5 py-1.5">
<div className="flex-row-2">
<Users className="size-5" />
{translation('teams')}
</div>
</ExpandableHeader>
<ExpandableContent className="!max-h-none !h-auto !overflow-visible gap-y-0 pl-4 p-0">
{(context?.teams ?? []).map(team => (
<SidebarLink key={team.id} href={`${locationRoute}/${team.id}`} onClick={onClose}>
{team.title}
</SidebarLink>
))}
</ExpandableContent>
</ExpandableRoot>
)}

{(context?.wards ?? []).length > 0 && (
<ExpandableRoot
className="shadow-none"
isExpanded={context?.sidebar?.isShowingWards ?? false}
onExpandedChange={isExpanded => context?.update(prevState => ({
...prevState,
sidebar: {
...prevState.sidebar,
isShowingWards: isExpanded,
}
}))}
>
<ExpandableHeader className="px-2.5 py-1.5">
<div className="flex-row-2">
<Building2 className="size-5" />
{translation('wards')}
</div>
</ExpandableHeader>
<ExpandableContent className="!max-h-none !h-auto !overflow-visible gap-y-0 pl-4 p-0">
{(context?.wards ?? []).map(ward => (
<SidebarLink key={ward.id} href={`${locationRoute}/${ward.id}`} onClick={onClose}>
{ward.title}
</SidebarLink>
))}
</ExpandableContent>
</ExpandableRoot>
)}

{(context?.clinics ?? []).length > 0 && (
<ExpandableRoot
className="shadow-none"
isExpanded={context?.sidebar?.isShowingClinics ?? false}
onExpandedChange={isExpanded => context?.update(prevState => ({
...prevState,
sidebar: {
...prevState.sidebar,
isShowingClinics: isExpanded,
}
}))}
>
<ExpandableHeader className="px-2.5 py-1.5">
<div className="flex-row-2">
<Hospital className="size-5" />
{translation('clinics')}
</div>
</ExpandableHeader>
<ExpandableContent className="!max-h-none !h-auto !overflow-visible gap-y-0 pl-4 p-0">
{(context?.clinics ?? []).map(clinic => (
<SidebarLink key={clinic.id} href={`${locationRoute}/${clinic.id}`} onClick={onClose}>
{clinic.title}
</SidebarLink>
))}
</ExpandableContent>
</ExpandableRoot>
)}
<ExpandableRoot
className="shadow-none"
isExpanded={context?.sidebar?.isShowingTeams ?? false}
onExpandedChange={isExpanded => context?.update(prevState => ({
...prevState,
sidebar: {
...prevState.sidebar,
isShowingTeams: isExpanded,
}
}))}
>
<ExpandableHeader className="px-2.5 py-1.5">
<div className="flex-row-2">
<Users className="size-5" />
{translation('teams')}
</div>
</ExpandableHeader>
<ExpandableContent className="!max-h-none !h-auto !overflow-visible gap-y-0 pl-4 p-0">
{(context?.teams ?? []).map(team => (
<SidebarLink key={team.id} href={`${locationRoute}/${team.id}`} onClick={onClose}>
{team.title}
</SidebarLink>
))}
</ExpandableContent>
</ExpandableRoot>

<ExpandableRoot
className="shadow-none"
isExpanded={context?.sidebar?.isShowingWards ?? false}
onExpandedChange={isExpanded => context?.update(prevState => ({
...prevState,
sidebar: {
...prevState.sidebar,
isShowingWards: isExpanded,
}
}))}
>
<ExpandableHeader className="px-2.5 py-1.5">
<div className="flex-row-2">
<Building2 className="size-5" />
{translation('wards')}
</div>
</ExpandableHeader>
<ExpandableContent className="!max-h-none !h-auto !overflow-visible gap-y-0 pl-4 p-0">
{(context?.wards ?? []).map(ward => (
<SidebarLink key={ward.id} href={`${locationRoute}/${ward.id}`} onClick={onClose}>
{ward.title}
</SidebarLink>
))}
</ExpandableContent>
</ExpandableRoot>

<ExpandableRoot
className="shadow-none"
isExpanded={context?.sidebar?.isShowingClinics ?? false}
onExpandedChange={isExpanded => context?.update(prevState => ({
...prevState,
sidebar: {
...prevState.sidebar,
isShowingClinics: isExpanded,
}
}))}
>
<ExpandableHeader className="px-2.5 py-1.5">
<div className="flex-row-2">
<Hospital className="size-5" />
{translation('clinics')}
</div>
</ExpandableHeader>
<ExpandableContent className="!max-h-none !h-auto !overflow-visible gap-y-0 pl-4 p-0">
{(context?.clinics ?? []).map(clinic => (
<SidebarLink key={clinic.id} href={`${locationRoute}/${clinic.id}`} onClick={onClose}>
{clinic.title}
</SidebarLink>
))}
</ExpandableContent>
</ExpandableRoot>
</nav>
<div className="mt-auto pt-4 border-t border-on-surface/20 sm:hidden">
<RootLocationSelector onSelect={onClose} />
Expand Down
20 changes: 0 additions & 20 deletions web/components/pages/login.tsx

This file was deleted.

11 changes: 10 additions & 1 deletion web/data/cache/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function buildCacheConfig(): InMemoryCacheConfig {
patient: { keyArgs: ['id'] },
patients: {
keyArgs: ['locationId', 'rootLocationIds', 'states', 'filtering', 'sorting', 'search', 'pagination'],
merge: (_existing, incoming) => incoming,
},
locationNode: { keyArgs: ['id'] },
locationNodes: {
Expand All @@ -30,7 +31,15 @@ export function buildCacheConfig(): InMemoryCacheConfig {
Task: { keyFields: ['id'] },
Patient: { keyFields: ['id'] },
User: { keyFields: ['id'] },
UserType: { keyFields: ['id'] },
UserType: {
keyFields: ['id'],
fields: {
tasks: {
keyArgs: ['rootLocationIds'],
merge: (_existing, incoming) => incoming,
},
},
},
LocationNode: { keyFields: ['id'] },
LocationNodeType: { keyFields: ['id'] },
PropertyValue: { keyFields: propertyValueKeyFields },
Expand Down
17 changes: 3 additions & 14 deletions web/data/subscriptions/useApolloGlobalSubscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ import {
removeRefreshingPatient
} from './refreshingEntities'
import { getConnectionStatus, setConnectionStatus } from '@/data/connectionStatus'
import {
GetGlobalDataDocument,
GetTasksDocument,
GetPatientsDocument
} from '@/api/gql/generated'

const QUERIES_TO_REFETCH_AFTER_MERGE = [
GetGlobalDataDocument,
GetTasksDocument,
GetPatientsDocument,
]

const TASK_UPDATED = `
subscription TaskUpdated($taskId: ID, $rootLocationIds: [ID!]) {
Expand Down Expand Up @@ -112,7 +101,7 @@ export function useApolloGlobalSubscriptions(
await mergeTaskUpdatedIntoCache(client, taskId, payloadObj, optionsRef.current).catch(
() => {}
)
client.refetchQueries({ include: QUERIES_TO_REFETCH_AFTER_MERGE })
client.refetchQueries({ include: 'active' })
} finally {
removeRefreshingTask(taskId)
}
Expand Down Expand Up @@ -143,7 +132,7 @@ export function useApolloGlobalSubscriptions(
await mergePatientUpdatedIntoCache(client, patientId, payloadObj, optionsRef.current).catch(
() => {}
)
client.refetchQueries({ include: QUERIES_TO_REFETCH_AFTER_MERGE })
client.refetchQueries({ include: 'active' })
} finally {
removeRefreshingPatient(patientId)
}
Expand Down Expand Up @@ -174,7 +163,7 @@ export function useApolloGlobalSubscriptions(
await mergePatientUpdatedIntoCache(client, patientId, payloadObj, optionsRef.current).catch(
() => {}
)
client.refetchQueries({ include: QUERIES_TO_REFETCH_AFTER_MERGE })
client.refetchQueries({ include: 'active' })
} finally {
removeRefreshingPatient(patientId)
}
Expand Down
1 change: 1 addition & 0 deletions web/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default [
'api/gql/*',
'i18n/*',
'next-env.d.ts',
'build/*',
],
},
{
Expand Down
46 changes: 29 additions & 17 deletions web/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use client'

import type { ComponentType, PropsWithChildren, ReactNode } from 'react'
import { createContext, useCallback, useContext, useEffect, useState } from 'react'
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react'
import { HelpwaveLogo } from '@helpwave/hightide'
import { login, logout, onTokenExpiringCallback, removeUser, renewToken, restoreSession } from '@/api/auth/authService'
import type { User } from 'oidc-client-ts'
import { getConfig } from '@/utils/config'
import { usePathname } from 'next/navigation'

const config = getConfig()
const LOGIN_REDIRECT_COOLDOWN_MS = 5000

type AuthState = {
identity?: User,
Expand Down Expand Up @@ -42,6 +43,18 @@ export const AuthProvider = ({
pathname.startsWith(pattern))
const isIgnored = !!pathname && ignoredURLs.some(pattern =>
pathname.startsWith(pattern))
const loginTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)

const scheduleLoginRedirect = useCallback(() => {
if (loginTimeoutRef.current) return
loginTimeoutRef.current = setTimeout(() => {
loginTimeoutRef.current = null
login(
config.auth.redirect_uri +
`?redirect_uri=${encodeURIComponent(window.location.href)}`
).catch(() => {})
}, LOGIN_REDIRECT_COOLDOWN_MS)
}, [])

useEffect(() => {
if(isIgnored) {
Expand All @@ -68,11 +81,7 @@ export const AuthProvider = ({
})
} else {
if (!isUnprotected) {
login(
config.auth.redirect_uri +
`?redirect_uri=${encodeURIComponent(window.location.href)}`
).catch(() => {
})
scheduleLoginRedirect()
} else {
removeUser()
.then(() => {
Expand All @@ -95,11 +104,7 @@ export const AuthProvider = ({
if (isAuthenticationServerUnavailable) {
setAuthState({ isLoading: false })
} else {
login(
config.auth.redirect_uri +
`?redirect_uri=${encodeURIComponent(window.location.href)}`
).catch(() => {
})
scheduleLoginRedirect()
}
} else {
removeUser()
Expand All @@ -114,8 +119,12 @@ export const AuthProvider = ({

return () => {
isMounted = false
if (loginTimeoutRef.current) {
clearTimeout(loginTimeoutRef.current)
loginTimeoutRef.current = null
}
}
}, [isIgnored, isUnprotected])
}, [isIgnored, isUnprotected, scheduleLoginRedirect])

const logoutAndReset = useCallback(() => {
logout()
Expand All @@ -125,11 +134,14 @@ export const AuthProvider = ({

useEffect(() => {
if (isIgnored || isUnprotected || identity || isLoading) return
login(
config.auth.redirect_uri +
`?redirect_uri=${encodeURIComponent(window.location.href)}`
).catch(() => {})
}, [identity, isLoading, isIgnored, isUnprotected])
scheduleLoginRedirect()
return () => {
if (loginTimeoutRef.current) {
clearTimeout(loginTimeoutRef.current)
loginTimeoutRef.current = null
}
}
}, [identity, isLoading, isIgnored, isUnprotected, scheduleLoginRedirect])

const authLoadingContent = (
<div className="flex flex-col items-center justify-center w-screen h-screen bg-surface">
Expand Down
Loading
Loading