plugins: moved cantOpen in Entry object

Massimo Melina committed Apr 13, 2023 at 10:44 UTC 0a56dc6383301117dbb6766fbe69a4730b2ad938
3 files changed +20 -17
dev-plugins.md
+4 -2
@@ -189,10 +189,12 @@ This is a list of available frontend-events, with respective object parameter an
189 - `t?: Date` generic timestamp, combination of creation-time and modified-time.
190 - `c?: Date` creation-time.
191 - `m?: Date` modified-time.
192 + - `p?: string` permissions missing
193 + - `cantOpen: boolean` true if current user has no permission to open this entry
194 - output `Html`
195 - `afterEntryName`
196 - you receive each entry of the list, and optionally produce HTML code that will be added after the name of the entry.
195 - - parameter `{ entry: Entry, cantOpen: boolean }` (refer above for Entry object)
197 + - parameter `{ entry: Entry }` (refer above for Entry object)
198 - output `Html`
199 - `beforeHeader` & `afterHeader`
200 - use this to produce content that should go right before/after the `header` part
@@ -236,7 +238,7 @@ HFS will scan through them in inverted alphabetical order searching for a compat
238 ## API version history
239
240 - 8.1 (v0.44.0)
239 - - afterEntryname.cantOpen
241 + - entry.cantOpen
242 - HFS.apiCall, reloadList, logout
243 - 8 (v0.43.0)
244 - entry.name & .uri
frontend/src/BrowseFiles.ts
+15 -15
@@ -25,7 +25,7 @@ import { t, useI18N } from './i18n'
25 import { deleteFiles } from './menu'
26
27 export interface DirEntry { n:string, s?:number, m?:string, c?:string, p?:string,
28 - name: string, uri: string, ext:string, isFolder:boolean, t?:Date } // we memoize these value for speed
28 + name: string, uri: string, ext:string, isFolder:boolean, t?:Date, cantOpen: boolean } // we memoize these value for speed
29 export type DirList = DirEntry[]
30
31 const MISSING_PERM = "Missing permission"
@@ -109,7 +109,7 @@ function FilesList() {
109 key: entry.n,
110 midnight,
111 separator: idx > 0 && !(idx % pageSize) ? String(offset + idx) : undefined,
112 - ...entry
112 + entry,
113 })),
114 loading && h(Spinner),
115 ),
@@ -179,15 +179,15 @@ function useMidnight() {
179
180 const PAGE_SEPARATOR_CLASS = 'page-separator'
181
182 -const Entry = memo((entry: DirEntry & { midnight: Date, separator?: string }) => {
183 - const { name, uri, isFolder, separator } = entry
182 +interface EntryProps { entry: DirEntry, midnight: Date, separator?: string }
183 +const Entry = memo(({ entry, midnight, separator }: EntryProps) => {
184 + const { uri, isFolder } = entry
185 const base = usePath()
186 const [menuBtn, setMenuBtn] = useState(false)
187 const { showFilter, selected, can_delete } = useSnapState()
187 - const cantOpen = entry.p?.includes(isFolder ? 'l' : 'r') // to open we need list for folders and read for files
188 const containerDir = isFolder ? '' : uri.substring(0, uri.lastIndexOf('/')+1)
189 let className = isFolder ? 'folder' : 'file'
190 - if (cantOpen)
190 + if (entry.cantOpen)
191 className += ' cant-open'
192 if (separator)
193 className += ' ' + PAGE_SEPARATOR_CLASS
@@ -215,11 +215,11 @@ const Entry = memo((entry: DirEntry & { midnight: Date, separator?: string }) =>
215 )
216 : h(Fragment, {},
217 containerDir && h(Link, { to: base + containerDir, className:'container-folder' }, ico, pathDecode(containerDir) ),
218 - h('a', { href: uri, onClick }, !containerDir && ico, name)
218 + h('a', { href: uri, onClick }, !containerDir && ico, entry.name)
219 ),
220 - h(CustomCode, { name: 'afterEntryName', props: { entry, cantOpen } }),
220 + h(CustomCode, { name: 'afterEntryName', props: { entry } }),
221 h('div', { className: 'entry-panel' },
222 - h(EntryProps, entry),
222 + h(EntryProps, { entry, midnight }),
223 (!menuOnLink || isFolder && mobile) && h('button', { className: 'file-menu-button', onClick: openFileMenu }, hIcon('menu')),
224 ),
225 h('div'),
@@ -228,18 +228,18 @@ const Entry = memo((entry: DirEntry & { midnight: Date, separator?: string }) =>
228 function openFileMenu(ev: MouseEvent) {
229 if (ev.altKey || ev.ctrlKey || ev.metaKey) return
230 ev.preventDefault()
231 - const cantDownload = cantOpen || isFolder && entry.p?.includes('r') // folders needs both list and read
231 + const cantDownload = entry.cantOpen || isFolder && entry.p?.includes('r') // folders needs both list and read
232 const OPEN_ICON = 'play'
233 const OPEN_LABEL = t('file_open', "Open")
234 const menu = [
235 - menuOnLink && !cantOpen && (
235 + menuOnLink && !entry.cantOpen && (
236 isFolder ? h(Link, { to: base + uri, onClick: () => close() }, hIcon(OPEN_ICON), OPEN_LABEL)
237 : { label: OPEN_LABEL, href: uri, target: isFolder ? undefined : '_blank', icon: OPEN_ICON }),
238 !cantDownload && { label: t`Download`, href: uri + (isFolder ? '?get=zip' : '?dl'), icon: 'download' },
239 can_delete && { label: t`Delete`, icon: 'trash', onClick: () => deleteFiles([uri], base) }
240 ]
241 const props = [
242 - [t(`Name`), name]
242 + [t(`Name`), entry.name]
243 ]
244 const res = hfsEvent('fileMenu', { entry, menu, props })
245 if (res)
@@ -259,7 +259,7 @@ const Entry = memo((entry: DirEntry & { midnight: Date, separator?: string }) =>
259 : null
260 ))
261 ),
262 - !cantOpen && h(Fragment, {}, hIcon('password', { style: { marginRight: '.5em' } }), t(MISSING_PERM)),
262 + entry.cantOpen && h(Fragment, {}, hIcon('password', { style: { marginRight: '.5em' } }), t(MISSING_PERM)),
263 h('div', { className: 'file-menu' },
264 dontBotherWithKeys(menu.map((e: any, i) =>
265 isValidElement(e) ? e
@@ -281,9 +281,9 @@ const Entry = memo((entry: DirEntry & { midnight: Date, separator?: string }) =>
281 }
282 })
283
284 -const EntryProps = memo((entry: DirEntry & { midnight: Date }) => {
284 +const EntryProps = memo(({ entry, midnight }: { entry: DirEntry, midnight: Date }) => {
285 const { t: time, s } = entry
286 - const today = time && time > entry.midnight
286 + const today = time && time > midnight
287 const shortTs = isMobile()
288 const {t} = useI18N()
289 const dd = '2-digit'
frontend/src/useFetchList.ts
+1
@@ -123,6 +123,7 @@ export default function useFetchList() {
123 if (t)
124 add.t = new Date(t)
125 add.name = add.n.slice(add.n.lastIndexOf('/', -1) +1, add.isFolder ? -1 : Infinity)
126 + add.cantOpen = add.p?.includes(add.isFolder ? 'l' : 'r') // to open we need list for folders and read for files
127
128 buffer.push(add)
129 }