Correctly disable dropzone when host is offline

This commit is contained in:
Danielv123 committed 2024-05-18 10:27:40 +01:00
1 parent 05b697fd24
commit c6abe29e89
4 files changed
+22 -13

No files matched your search

+14 -6
View File
@@ -3,11 +3,19 @@ import { DraggingContext } from "../model/is_dragging";
import { useContext } from "react"; import { useContext } from "react";
// This component is used to display a dropzone when a file is being dragged over the parent component // This component is used to display a dropzone when a file is being dragged over the parent component
export function Dropzone() { export function Dropzone({ disabled = false }: { disabled?: boolean }) {
const isDroppingFile = useContext(DraggingContext); const isDroppingFile = useContext(DraggingContext);
const textStyle: { color?: string } = {};
let text = "Drop to upload";
if (disabled) {
textStyle.color = "red";
text = "Target is offline";
}
const borderColor = disabled ? "gray" : "rgb(22, 119, 255)";
return <div return <div
className="dropzone" // Don't remove this class, linked to SiteLayout.tsx className={`dropzone ${disabled ? "disabled" : "enabled"}`} // Don't remove this class, linked to SiteLayout
style={{ style={{
position: "absolute", position: "absolute",
top: "0", top: "0",
@@ -17,7 +25,7 @@ export function Dropzone() {
zIndex: "90", zIndex: "90",
backgroundColor: "#88888844", backgroundColor: "#88888844",
borderRadius: "20px", borderRadius: "20px",
border: "dashed 2px rgb(22, 119, 255)", border: `dashed 2px ${borderColor}`,
display: isDroppingFile ? "block" : "none", display: isDroppingFile ? "block" : "none",
}} }}
> >
@@ -34,9 +42,9 @@ export function Dropzone() {
height: "100%", height: "100%",
}} }}
> >
<InboxOutlined /> <InboxOutlined style={textStyle} />
<p style={{ fontSize: "24px", display: "block", textAlign: "center", marginTop: "8px" }}> <p style={{ ...textStyle, fontSize: "24px", display: "block", textAlign: "center", marginTop: "8px" }}>
Drop to upload {text}
</p> </p>
</div> </div>
</div>; </div>;
+1 -1
View File
@@ -363,7 +363,7 @@ export default function SavesList(props: { instance: lib.InstanceDetails }) {
zIndex: "1010", zIndex: "1010",
}} }}
> >
<Dropzone /> <Dropzone disabled={hostOffline} />
{saveTable} {saveTable}
</div> </div>
</Upload.Dragger> </Upload.Dragger>
@@ -13,7 +13,7 @@ import { DraggingContext } from "../model/is_dragging";
const { Header, Sider } = Layout; const { Header, Sider } = Layout;
function isDropzone(element: HTMLElement | null): boolean { function isActiveDropzone(element: HTMLElement | null): boolean {
if (!element) { if (!element) {
return false; return false;
} }
@@ -21,7 +21,7 @@ function isDropzone(element: HTMLElement | null): boolean {
let depth = 0; let depth = 0;
while (element && depth < checkDepth) { while (element && depth < checkDepth) {
if (element.classList.contains("dropzone")) { if (element.classList.contains("dropzone")) {
return true; return element.classList.contains("enabled");
} }
element = element.parentElement; element = element.parentElement;
depth += 1; depth += 1;
@@ -105,7 +105,7 @@ export default function SiteLayout() {
onDrop={(e) => { onDrop={(e) => {
setDragging(0); setDragging(0);
// Prevent dropping outside of dropzone // Prevent dropping outside of dropzone
if (!isDropzone(e.target as HTMLElement)) { if (!isActiveDropzone(e.target as HTMLElement)) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
} }
@@ -114,7 +114,7 @@ export default function SiteLayout() {
onDragOver={(e) => { onDragOver={(e) => {
e.preventDefault(); e.preventDefault();
// Change cursor // Change cursor
if (isDropzone(e.target as HTMLElement)) { if (isActiveDropzone(e.target as HTMLElement)) {
e.dataTransfer.dropEffect = "copy"; e.dataTransfer.dropEffect = "copy";
} else { } else {
e.dataTransfer.dropEffect = "none"; e.dataTransfer.dropEffect = "none";
@@ -6,13 +6,14 @@ import { DraggingContext } from "../model/is_dragging";
export default function UploadButton(props: React.ComponentProps<typeof Button>) { export default function UploadButton(props: React.ComponentProps<typeof Button>) {
const isDroppingFile = useContext(DraggingContext); const isDroppingFile = useContext(DraggingContext);
const borderColor = props.disabled ? "gray" : "rgb(22, 119, 255)";
return <Button return <Button
className="dropzone" className={`dropzone ${props.disabled ? "disabled" : "enabled"}`}
{...props} {...props}
style={{ style={{
...props.style, ...props.style,
// Distinct border when dropping file // Distinct border when dropping file
border: isDroppingFile ? "dashed 2px rgb(22, 119, 255)" : undefined, border: isDroppingFile ? `dashed 2px ${borderColor}` : undefined,
}} }}
icon={isDroppingFile ? <InboxOutlined /> : props.icon || <UploadOutlined />} icon={isDroppingFile ? <InboxOutlined /> : props.icon || <UploadOutlined />}
> >