Skip to content

Commit

Permalink
fix: flickering sidenav, clear swr cache on logout
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Apr 6, 2023
1 parent 8510fc8 commit 242937b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-terms-wonder.md
@@ -0,0 +1,5 @@
---
'@siafoundation/renterd': minor
---

The renterd sidenav no longer flickers when changing tabs.
1 change: 1 addition & 0 deletions apps/renterd/components/Files/index.tsx
Expand Up @@ -12,6 +12,7 @@ export function Files() {
return (
<RenterdAuthedLayout
title="Files"
navTitle={null}
routes={routes}
sidenav={<RenterSidenav />}
nav={<FilesBreadcrumbMenu />}
Expand Down
81 changes: 0 additions & 81 deletions apps/renterd/contexts/hosts/utils.ts

This file was deleted.

34 changes: 19 additions & 15 deletions apps/renterd/hooks/useAutopilot.tsx
@@ -1,5 +1,5 @@
import { useAutopilotStatus } from '@siafoundation/react-core'
import { useEffect, useState } from 'react'
import useSWR from 'swr'

export function useAutopilot() {
const aps = useAutopilotStatus({
Expand All @@ -8,27 +8,31 @@ export function useAutopilot() {
dedupingInterval: 60_000,
revalidateOnFocus: false,
refreshInterval: 60_000,
keepPreviousData: true,
},
},
})

const [autopilotMode, setAutopilotMode] = useState<'on' | 'off' | 'init'>(
'init'
)

useEffect(() => {
if (autopilotMode === 'init' && (aps.data || aps.error)) {
if (aps.error) {
setAutopilotMode('off')
const apm = useSWR<'on' | 'off' | 'init'>(
[aps.data, aps.error],
() => {
if (aps.data || aps.error) {
if (aps.error) {
return 'off'
}
// This check is required because the API currently returns html when the endpoint does not exist
const validResponse = typeof aps.data === 'object'
return validResponse ? 'on' : 'off'
}
// This check is required because the API currently returns html when the endpoint does not exist
const validResponse = typeof aps.data === 'object'
setAutopilotMode(validResponse ? 'on' : 'off')
return 'init'
},
{
keepPreviousData: true,
fallbackData: 'init',
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [aps])
)

return {
autopilotMode,
autopilotMode: apm.data,
}
}
8 changes: 7 additions & 1 deletion libs/design-system/src/app/AppAuthedLayout/index.tsx
Expand Up @@ -10,6 +10,7 @@ type Props = {
appName: string
title?: string
size?: React.ComponentProps<typeof Container>['size']
navTitle?: string
nav?: React.ReactNode
actions?: React.ReactNode
sidenav?: React.ReactNode
Expand All @@ -31,6 +32,7 @@ type Props = {
export function AppAuthedLayout({
appName,
title,
navTitle,
size = '4',
nav,
actions,
Expand All @@ -47,7 +49,11 @@ export function AppAuthedLayout({
{sidenav}
</Sidenav>
<div className="flex flex-col flex-1 overflow-hidden">
<AppNavbar title={title} nav={nav} actions={actions} />
<AppNavbar
title={navTitle === undefined ? title : navTitle}
nav={nav}
actions={actions}
/>
<ScrollArea className="z-0">
<Container size={size} pad={false}>
<div className="flex flex-col gap-5">{children}</div>
Expand Down
6 changes: 6 additions & 0 deletions libs/design-system/src/hooks/useMonitorConnAndLock.ts
@@ -1,6 +1,7 @@
import { useAppSettings } from '@siafoundation/react-core'
import { NextRouter, useRouter } from 'next/router'
import { useEffect } from 'react'
import { mutate } from 'swr'
import { useConnectivity } from './useConnectivity'

type Routes = {
Expand Down Expand Up @@ -33,6 +34,7 @@ export function useMonitorConnAndLock(routes: Routes) {
const noPasswordOrDisconnected = !settings.password || !isConnected
if (isProtectedPath && noPasswordOrDisconnected) {
setSettings({ password: '' })
clearAllSwrKeys()
router.push({
pathname: routes.lockscreen,
query: {
Expand All @@ -55,3 +57,7 @@ export function useMonitorConnAndLock(routes: Routes) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router, isConnected, isSynced])
}

function clearAllSwrKeys() {
mutate(() => true, undefined, { revalidate: false })
}

0 comments on commit 242937b

Please sign in to comment.