fix: set constrain for uploading packages only works in the Plugins tab

This commit is contained in:
Yi 2024-10-15 15:47:54 +08:00
parent 177e8cbf73
commit d83f94c55c
2 changed files with 37 additions and 28 deletions

View File

@ -36,23 +36,24 @@ const PluginPage = ({
const { setShowPluginSettingModal } = useModalContext() as any const { setShowPluginSettingModal } = useModalContext() as any
const [currentFile, setCurrentFile] = useState<File | null>(null) const [currentFile, setCurrentFile] = useState<File | null>(null)
const containerRef = usePluginPageContext(v => v.containerRef) const containerRef = usePluginPageContext(v => v.containerRef)
const { dragging, fileUploader, fileChangeHandle, removeFile } = useUploader({
onFileChange: setCurrentFile,
containerRef,
})
const options = useMemo(() => { const options = useMemo(() => {
return [ return [
{ value: 'plugins', text: t('common.menus.plugins') }, { value: 'plugins', text: t('common.menus.plugins') },
{ value: 'discover', text: 'Explore Marketplace' }, { value: 'discover', text: 'Explore Marketplace' },
] ]
}, [t]) }, [t])
const [activeTab, setActiveTab] = useTabSearchParams({ const [activeTab, setActiveTab] = useTabSearchParams({
defaultTab: options[0].value, defaultTab: options[0].value,
}) })
const uploaderProps = useUploader({
onFileChange: setCurrentFile,
containerRef,
enabled: activeTab === 'plugins',
})
const { dragging, fileUploader, fileChangeHandle, removeFile } = uploaderProps
return ( return (
<div <div
ref={containerRef} ref={containerRef}
@ -138,21 +139,21 @@ const PluginPage = ({
<span className="system-xs-regular">Drop plugin package here to install</span> <span className="system-xs-regular">Drop plugin package here to install</span>
</div> </div>
{currentFile && ( {currentFile && (
<InstallFromLocalPackage file={currentFile} onClose={removeFile} /> <InstallFromLocalPackage file={currentFile} onClose={removeFile ?? (() => {})} />
)} )}
<input
ref={fileUploader}
className="hidden"
type="file"
id="fileUploader"
accept='.difypkg'
onChange={fileChangeHandle ?? (() => {})}
/>
</> </>
)} )}
{ {
activeTab === 'discover' && marketplace activeTab === 'discover' && marketplace
} }
<input
ref={fileUploader}
className="hidden"
type="file"
id="fileUploader"
accept='.difypkg'
onChange={fileChangeHandle}
/>
</div> </div>
) )
} }

View File

@ -3,9 +3,10 @@ import { useEffect, useRef, useState } from 'react'
type UploaderHookProps = { type UploaderHookProps = {
onFileChange: (file: File | null) => void onFileChange: (file: File | null) => void
containerRef: React.RefObject<HTMLDivElement> containerRef: React.RefObject<HTMLDivElement>
enabled?: boolean
} }
export const useUploader = ({ onFileChange, containerRef }: UploaderHookProps) => { export const useUploader = ({ onFileChange, containerRef, enabled = true }: UploaderHookProps) => {
const [dragging, setDragging] = useState(false) const [dragging, setDragging] = useState(false)
const fileUploader = useRef<HTMLInputElement>(null) const fileUploader = useRef<HTMLInputElement>(null)
@ -39,19 +40,26 @@ export const useUploader = ({ onFileChange, containerRef }: UploaderHookProps) =
onFileChange(files[0]) onFileChange(files[0])
} }
const fileChangeHandle = (e: React.ChangeEvent<HTMLInputElement>) => { const fileChangeHandle = enabled
const file = e.target.files?.[0] || null ? (e: React.ChangeEvent<HTMLInputElement>) => {
onFileChange(file) const file = e.target.files?.[0] || null
} onFileChange(file)
}
: null
const removeFile = () => { const removeFile = enabled
if (fileUploader.current) ? () => {
fileUploader.current.value = '' if (fileUploader.current)
fileUploader.current.value = ''
onFileChange(null) onFileChange(null)
} }
: null
useEffect(() => { useEffect(() => {
if (!enabled)
return
const current = containerRef.current const current = containerRef.current
if (current) { if (current) {
current.addEventListener('dragenter', handleDragEnter) current.addEventListener('dragenter', handleDragEnter)
@ -67,10 +75,10 @@ export const useUploader = ({ onFileChange, containerRef }: UploaderHookProps) =
current.removeEventListener('drop', handleDrop) current.removeEventListener('drop', handleDrop)
} }
} }
}, [containerRef]) }, [containerRef, enabled])
return { return {
dragging, dragging: enabled ? dragging : false,
fileUploader, fileUploader,
fileChangeHandle, fileChangeHandle,
removeFile, removeFile,