fix: #LOGIN was not working #651
Massimo Melina committed
Jun 24, 2024 at 10:47 UTC
119ddaa5e8f0336871a7ca77f4f1082fb64a7757
3 files changed
+12
-8
frontend/src/login.ts
+2
-4
@@ -141,12 +141,10 @@ export async function loginDialog(closable=true, reloadAfter=true) {
141
142
export function useAuthorized() {
143
const { loginRequired } = useSnapState()
144
- if (location.hash === '#LOGIN')
145
- state.loginRequired = true
144
useEffect(() => {
145
if (!loginRequired)
148
- return closeLoginDialog?.()
149
- if (!closeLoginDialog)
146
+ closeLoginDialog?.()
147
+ else if (!closeLoginDialog)
148
void loginDialog(false)
149
}, [loginRequired])
150
return loginRequired ? null : true
frontend/src/useFetchList.ts
+7
-2
@@ -10,6 +10,7 @@ import { alertDialog } from './dialog'
10
import { hfsEvent, HTTP_MESSAGES, HTTP_METHOD_NOT_ALLOWED, HTTP_UNAUTHORIZED, LIST, urlParams, waitFor, xlate } from './misc'
11
import { t } from './i18n'
12
import { useLocation, useNavigate } from 'react-router-dom'
13
+import { closeLoginDialog } from './login'
14
15
export function usePath() {
16
return useLocation().pathname
@@ -30,6 +31,7 @@ export default function useFetchList() {
31
const lastReloader = useRef(snap.listReloader)
32
const isMounted = useIsMounted()
33
const navigate = useNavigate()
34
+ const { loginRequired=false } = snap // undefined=false
35
useEffect(()=>{
36
const previous = lastUri.current
37
lastUri.current = uri
@@ -77,7 +79,10 @@ export default function useFetchList() {
79
state.loading = false
80
return
81
case 'msg':
80
- state.loginRequired = false
82
+ const showLogin = location.hash === '#LOGIN'
83
+ if (closeLoginDialog)
84
+ location.hash = ''
85
+ state.loginRequired = showLogin
86
for (const entry of data) {
87
if (!Array.isArray(entry)) continue // unexpected
88
const [op, par] = entry
@@ -122,7 +127,7 @@ export default function useFetchList() {
127
clearInterval(timer)
128
src.close()
129
}
125
- }, [uri, search, snap.username, snap.listReloader, snap.loginRequired])
130
+ }, [uri, search, snap.username, snap.listReloader, loginRequired])
131
}
132
133
export function reloadList() {
shared/dialogs.ts
+3
-2
@@ -91,6 +91,7 @@ export function Dialogs(props: HTMLAttributes<HTMLDivElement>) {
91
useEffect(() => domOn('popstate', () => {
92
if (ignorePopState)
93
return ignorePopState = false
94
+ if (!history.state) return
95
const { $dialog } = history.state
96
if ($dialog && !dialogs.find(x => x.$id === $dialog)) // it happens if the user, after closing a dialog, goes forward in the history
97
return back()
@@ -189,7 +190,7 @@ export function newDialog(options: DialogOptions) {
190
dialogs.push(options)
191
options = dialogs[dialogs.length - 1] // replace with proxy object, to stay in sync with its changes
192
if (options.closable !== false)
192
- history.pushState({ $dialog: $id, ts, idx: history.state.idx + 1 }, '')
193
+ history.pushState({ $dialog: $id, ts, idx: 1 + (history.state?.idx || 0) }, '')
194
}, 10) // 10 for firefox, chrome125 seems to be ok with 1
195
return { close }
196
@@ -197,7 +198,7 @@ export function newDialog(options: DialogOptions) {
198
clearTimeout(options.$opening) // in case it was not open yet
199
const i = dialogs.findIndex(x => (x as any).$id === $id)
200
if (i < 0) return
200
- if (history.state.$dialog === $id)
201
+ if (history.state?.$dialog === $id)
202
options.closed = back()
203
closeDialogAt(i, v)
204
return options