import { useEffect, useRef, useState } from "react"; import styled from "styled-components"; import { Button, Col, Collapse, Icon, Menu, Modal, PopConfirm, Row, ScrollIndicator, Slider, SplitButton, Transfer } from "tiny-ui"; import WLED from "./WLED.js"; const { Panel } = Collapse; const to255 = (v) => Math.ceil((v / 100) * 255); const from255 = (v) => Math.ceil((v / 255) * 100); const download = (filename, data, type = "application/json") => { const blob = new Blob([data], { type: type }); const href = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = href; link.download = filename; document.body.appendChild(link); link.click(); document.body.removeChild(link); }; const StyledRow = styled((props) => )` margin: 0.25rem 0.5rem; & .action { text-align: right; & .ty-icon { margin-right: 1rem; } } `; const StyledMenu = styled((props) => )` & .ty-icon { margin-right: 1rem; } `; const ScrollingDiv = (props) => { const targetRef = useRef(null); return ( <> targetRef?.current} />
); }; const UploadModal = ({ data, client, onConfirm, ...rest }) => { const [payload, setPayload] = useState({}); useEffect(() => { if (data) setPayload({ ...data }); }, [data]); const handleConfirm = (_) => { onConfirm(payload); }; const handleChange = (e) => { const fileReader = new FileReader(); fileReader.readAsText(e?.target?.files?.[0], "UTF-8"); fileReader.onload = (e) => { if (e?.target?.result) { setPayload({ ...payload, value: JSON.parse(e?.target?.result) }); } }; }; if (!data) return null; return (
Are you sure you want to upload and override presets?
{payload?.value ? (
{JSON.stringify(payload?.value, null, 2)}
) : null}
); }; const PurgeModal = ({ data, client, onConfirm, ...rest }) => { const [payload, setPayload] = useState({}); useEffect(() => { if (data) setPayload({ ...data }); }, [data]); const handleConfirm = (_) => { onConfirm(payload); }; if (!data) return null; return (
Are you sure you want to delete these presets?
{JSON.stringify(data?.value, null, 2)}
); }; const CopyModal = ({ data, client, clients, onConfirm, ...rest }) => { const [transfer, setTransfer] = useState({}); const [payload, setPayload] = useState({}); useEffect(() => { setPayload({ ...payload, value: data }); }, [data]); useEffect(() => { if (clients) setTransfer( clients ?.filter((o) => o !== client) ?.map((o, i) => ({ key: o, label: o })) ); }, [clients]); const handleConfirm = (_) => { onConfirm(payload); }; if (!data) return null; return (
{JSON.stringify(data, null, 2)}
{ setPayload({ ...payload, clients: value }); }} />
); }; const InstanceManagement = ({ client, presets, onCopy, onPreset, onDelete }) => { if (!presets) return null; return Object.keys(presets)?.map((o, i) => ( {presets[o]?.name || ""} { download( `preset_${o}.json`, JSON.stringify(presets[o], null, 2) ); }} > Save { onCopy({ [o]: presets[o] }); }} > Copy onDelete({ clients: [client], value: [o] })} > Delete } onClick={(e) => { onPreset({ clients: [client], value: o }); }} > Apply )); }; const StateMangement = ({ client, onPower, onSync, onBrightness, current }) => { if (!current) return null; return ( onPower({ clients: [client], value: "reboot" })} > onSync({ clients: [client], value: "enable" })} > onSync({ clients: [client], value: "disable" })} > { onBrightness({ clients: [client], value: to255(v) }); }} /> ); }; const Management = ({ key, itemKey, clientName, onCopy, onDelete, onSync, ...props }) => { const [copy, setCopy] = useState(null); const [purge, setPurge] = useState(null); const [upload, setUpload] = useState(null); return ( {clientName} ({props?.client}) } extra={ { e.preventDefault(); e.stopPropagation(); }} overlay={ { e.preventDefault(); e.stopPropagation(); window.open(`http://${props?.client}`); }} > Open { e.preventDefault(); e.stopPropagation(); download( `presets.json`, JSON.stringify(props?.presets, null, 2) ); }} > Save all { e.preventDefault(); e.stopPropagation(); setUpload({ clients: [props?.client] }); }} > Restore all { e.preventDefault(); e.stopPropagation(); setCopy(props?.presets); }} > Copy all { e.preventDefault(); e.stopPropagation(); setPurge({ clients: [props?.client], value: Object.keys(props?.presets) }); }} > Delete all } > } > { setPurge(e); }} onCopy={(e) => { setCopy(e); }} /> { onCopy(e); setCopy(null); }} onCancel={(e) => setCopy(null)} {...props} /> { onDelete(e); setPurge(null); }} onCancel={(e) => setPurge(null)} {...props} /> { onCopy(e); setUpload(null); }} onCancel={(e) => setUpload(null)} {...props} /> ); }; export default WLED(Management);