Skip to content

Commit

Permalink
fix: admin/fs: in file dialog, changing base_url won't update the form
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Apr 15, 2024
1 parent e9fa319 commit 9de74e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 8 additions & 2 deletions admin/src/FileForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { BoolField, DisplayField, Field, FieldProps, Form, MultiSelectField, Sel
import { apiCall, UseApi } from './api'
import {
basename, defaultPerms, formatBytes, formatTimestamp, isEqualLax, isWhoObject, newDialog, objSameKeys,
onlyTruthy, prefix, VfsPerms, wantArray, Who, WhoObject, matches, HTTP_MESSAGES, xlate, md, Callback
onlyTruthy, prefix, VfsPerms, wantArray, Who, WhoObject, matches, HTTP_MESSAGES, xlate, md, Callback,
useRequestRender
} from './misc'
import { Btn, IconBtn, LinkBtn, modifiedProps, useBreakpoint, wikiLink } from './mui'
import { reloadVfs, VfsNode } from './VfsPage'
Expand Down Expand Up @@ -289,7 +290,12 @@ interface LinkFieldProps extends FieldProps<string> {
statusApi: UseApi<any> // receive status from parent, to avoid asking server at each click on a file
}
function LinkField({ value, statusApi }: LinkFieldProps) {
const { data, reload, error } = statusApi
const { reload, error } = statusApi
// workaround to get fresh data and be rerendered even when mounted inside imperative dialog
const requestRender = useRequestRender()
useEffect(() => statusApi.sub(requestRender), [])
const data = statusApi.getData()

const urls: string[] = data?.urls.https || data?.urls.http
const baseHost = data?.baseUrl && new URL(data.baseUrl).host
const root = useMemo(() => baseHost && _.find(data.roots, (root, host) => matches(baseHost, host)),
Expand Down
9 changes: 6 additions & 3 deletions shared/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <[email protected]> - License https://www.gnu.org/licenses/gpl-3.0.txt

import _ from 'lodash';
import { useCallback, useEffect, useRef } from 'react';
import { Dict, Falsy, getPrefixUrl, pendingPromise, useStateMounted, wait } from '.'
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { Dict, EventEmitter, Falsy, getPrefixUrl, pendingPromise, useStateMounted, wait } from '.'

export const API_URL = '/~/api/'

Expand Down Expand Up @@ -112,7 +112,10 @@ export function useApi<T=any>(cmd: string | Falsy, params?: object, options: Api
setForcer(v => v + 1)
reloadingRef.current = pendingPromise()
}, [setForcer])
return { data, setData, error, reload, loading: loadingRef.current || reloadingRef.current, getData: () => dataRef.current, }
const ee = useMemo(() => new EventEmitter, [])
const sub = useCallback((cb: EventListener) => ee.on('data', cb), [])
useEffect(() => ee.emit('data'), [data])
return { data, setData, error, reload, sub, loading: loadingRef.current || reloadingRef.current, getData: () => dataRef.current, }
}

type EventHandler = (type:string, data?:any) => void
Expand Down
8 changes: 8 additions & 0 deletions shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ export function createDurationFormatter({ locale=undefined, unitDisplay='narrow'
}
}

export class EventEmitter extends EventTarget {
emit(name: string) { this.dispatchEvent(new Event(name)) }
on(name: string, cb: EventListener) {
this.addEventListener(name, cb)
return () => this.removeEventListener(name, cb)
}
}

Element.prototype.replaceChildren ||= function(this:Element, addNodes) { // polyfill
while (this.lastChild) this.removeChild(this.lastChild);
if (addNodes !== undefined) this.append(addNodes);
Expand Down

0 comments on commit 9de74e0

Please sign in to comment.