plugins: HFS.navigate

Massimo Melina committed Dec 17, 2023 at 16:31 UTC 922e92c9f7f09137fed54b6459334a1781d13a1c
6 files changed +12 -21
frontend/src/App.ts
+3 -3
@@ -9,6 +9,7 @@ import { useSnapState } from './state'
9 import { I18Nprovider } from './i18n'
10 import { proxy, useSnapshot } from "valtio"
11 import { Spinner } from "./components"
12 +import { getHFS, getPrefixUrl } from '@hfs/shared'
13
14 function App() {
15 useTheme()
@@ -30,10 +31,9 @@ function App() {
31 )
32 }
33
33 -// expose navigate function for programmatic usage
34 -export let navigate: ReturnType<typeof useNavigate>
34 function NavigationExtractor(props: any) {
36 - navigate = useNavigate()
35 + const go = useNavigate() // expose navigate function for programmatic usage
36 + getHFS().navigate = (uri: string) => go((uri.startsWith('/') ? getPrefixUrl() : '') + uri)
37 return h(Fragment, props)
38 }
39
frontend/src/BrowseFiles.ts
+1 -2
@@ -21,7 +21,6 @@ export const MISSING_PERM = "Missing permission"
21 export function BrowseFiles() {
22 useFetchList()
23 const { error } = useSnapState()
24 - const navigate = useNavigate()
24 const { props, tile_size=0 } = useSnapState()
25 const propsDropFiles = useMemo(() => acceptDropFiles(files =>
26 props?.can_upload ? enqueue(files.map(file => ({ file })))
@@ -31,7 +30,7 @@ export function BrowseFiles() {
30 return h(CustomCode, { name: 'unauthorized',
31 ifEmpty: () => h('h1', {
32 className: 'unauthorized',
34 - onClick: () => loginDialog(navigate)
33 + onClick: () => loginDialog()
34 }, t`Unauthorized`)
35 })
36 return h('div', propsDropFiles, // element dedicated to drop-files to cover full screen
frontend/src/fileMenu.ts
+2 -3
@@ -1,5 +1,5 @@
1 import { t, useI18N } from './i18n'
2 -import { dontBotherWithKeys, formatBytes, hfsEvent, hIcon, newDialog, prefix, with_, working } from './misc'
2 +import { dontBotherWithKeys, formatBytes, getHFS, hfsEvent, hIcon, newDialog, prefix, with_, working } from './misc'
3 import { createElement as h, Fragment, isValidElement, MouseEvent, ReactNode } from 'react'
4 import _ from 'lodash'
5 import { getEntryIcon, MISSING_PERM } from './BrowseFiles'
@@ -9,7 +9,6 @@ import { Link } from 'react-router-dom'
9 import { fileShow, getShowType } from './show'
10 import { alertDialog, promptDialog } from './dialog'
11 import { apiCall, useApi } from '@hfs/shared/api'
12 -import { navigate } from './App'
12 import { inputComment } from './upload'
13 import { cut } from './clip'
14
@@ -129,7 +128,7 @@ async function rename(entry: DirEntry) {
128 }
129 alertDialog(t`Operation successful`).then(() => {
130 if (isCurrentFolder)
132 - navigate(uri + '../' + pathEncode(dest) + '/')
131 + getHFS().navigate(uri + '../' + pathEncode(dest) + '/')
132 })
133 }
134 catch(e: any) {
frontend/src/login.ts
+4 -10
@@ -42,7 +42,7 @@ export function logout(){
42
43 export let closeLoginDialog: undefined | (() => void)
44 let lastPromise: Promise<any>
45 -export async function loginDialog(navigate: ReturnType<typeof useNavigate>) {
45 +export async function loginDialog() {
46 return lastPromise = new Promise(resolve => {
47 if (closeLoginDialog)
48 return lastPromise
@@ -112,7 +112,7 @@ export async function loginDialog(navigate: ReturnType<typeof useNavigate>) {
112 const res = await login(usr, pwd)
113 close(true)
114 if (res?.redirect)
115 - navigate(getPrefixUrl() + res.redirect)
115 + getHFS().navigate(res.redirect)
116 } catch (err: any) {
117 await alertDialog(err)
118 usrRef.current?.focus()
@@ -131,17 +131,11 @@ export function useAuthorized() {
131 const { loginRequired } = useSnapState()
132 if (location.hash === '#LOGIN')
133 state.loginRequired = true
134 - const navigate = useNavigate()
134 useEffect(() => {
135 if (!loginRequired)
136 return closeLoginDialog?.()
137 if (!closeLoginDialog)
139 - loginDialog(navigate).then()
140 - }, [loginRequired, navigate])
138 + loginDialog().then()
139 + }, [loginRequired])
140 return loginRequired ? null : true
141 }
143 -
144 -function tElement(s: string) {
145 - const {t} = useI18N()
146 - return t(s)
147 -}
frontend/src/menu.ts
+1 -2
@@ -186,7 +186,6 @@ export function MenuLink({ href, target, confirm, confirmOptions, ...rest }: Men
186
187 function LoginButton() {
188 const snap = useSnapState()
189 - const navigate = useNavigate()
189 const {t} = useI18N()
190 return MenuButton(snap.username ? {
191 id: 'user-button',
@@ -199,7 +198,7 @@ function LoginButton() {
198 icon: 'login',
199 label: t`Login`,
200 onClickAnimation: false,
202 - onClick: () => loginDialog(navigate),
201 + onClick: () => loginDialog(),
202 })
203 }
204
src/const.ts
+1 -1
@@ -7,7 +7,7 @@ import { mkdirSync } from 'fs'
7 import { basename, dirname, join } from 'path'
8 export * from './cross-const'
9
10 -export const API_VERSION = 8.61
10 +export const API_VERSION = 8.62
11 export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
12 export const HFS_REPO = 'rejetto/hfs'
13