1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-26 21:25:18 +01:00

Make Attach URL prettier

This commit is contained in:
Philipp Heckel 2022-03-31 12:03:36 -04:00
parent 9814a9f792
commit 2bdae49425

View file

@ -37,6 +37,7 @@ const SendDialog = (props) => {
const [attachUrl, setAttachUrl] = useState(""); const [attachUrl, setAttachUrl] = useState("");
const [attachFile, setAttachFile] = useState(null); const [attachFile, setAttachFile] = useState(null);
const [filename, setFilename] = useState(""); const [filename, setFilename] = useState("");
const [filenameEdited, setFilenameEdited] = useState(false);
const [email, setEmail] = useState(""); const [email, setEmail] = useState("");
const [delay, setDelay] = useState(""); const [delay, setDelay] = useState("");
@ -219,16 +220,46 @@ const SendDialog = (props) => {
{showAttachUrl && {showAttachUrl &&
<ClosableRow onClose={() => { <ClosableRow onClose={() => {
setAttachUrl(""); setAttachUrl("");
setFilename("");
setFilenameEdited(false);
setShowAttachUrl(false); setShowAttachUrl(false);
}}> }}>
<TextField <TextField
margin="dense" margin="dense"
label="Attachment URL" label="Attachment URL"
placeholder="Attach file by URL, e.g. https://f-droid.org/F-Droid.apk"
value={attachUrl} value={attachUrl}
onChange={ev => setAttachUrl(ev.target.value)} onChange={ev => {
const url = ev.target.value;
setAttachUrl(url);
if (!filenameEdited) {
try {
const u = new URL(url);
const parts = u.pathname.split("/");
if (parts.length > 0) {
setFilename(parts[parts.length-1]);
}
} catch (e) {
// Do nothing
}
}
}}
type="url" type="url"
variant="standard" variant="standard"
fullWidth sx={{flexGrow: 5, marginRight: 1}}
/>
<TextField
margin="dense"
label="Filename"
placeholder="Attachment filename"
value={filename}
onChange={ev => {
setFilename(ev.target.value);
setFilenameEdited(true);
}}
type="text"
variant="standard"
sx={{flexGrow: 1}}
/> />
</ClosableRow> </ClosableRow>
} }
@ -242,16 +273,10 @@ const SendDialog = (props) => {
file={attachFile} file={attachFile}
filename={filename} filename={filename}
onChangeFilename={(f) => setFilename(f)} onChangeFilename={(f) => setFilename(f)}
onClose={() => setAttachFile(null)} onClose={() => {
/>} setAttachFile(null);
{showAttachUrl && <TextField setFilename("");
margin="dense" }}
label="Attachment Filename"
value={filename}
onChange={ev => setFilename(ev.target.value)}
type="text"
variant="standard"
fullWidth
/>} />}
{showDelay && {showDelay &&
<ClosableRow onClose={() => { <ClosableRow onClose={() => {
@ -328,32 +353,8 @@ const DialogIconButton = (props) => {
const AttachmentBox = (props) => { const AttachmentBox = (props) => {
const file = props.file; const file = props.file;
const invisibleFilenameRef = useRef();
const minFilenameWidth = 140;
const [filenameWidth, setFilenameWidth] = useState(minFilenameWidth);
const handleFilenameChange = (ev) => {
props.onChangeFilename(ev.target.value);
};
const determineFilenameWidth = () => {
const boundingRect = invisibleFilenameRef?.current?.getBoundingClientRect();
if (!boundingRect) {
return minFilenameWidth;
}
return (boundingRect.width >= minFilenameWidth) ? Math.round(boundingRect.width) : minFilenameWidth;
};
useEffect(() => {
setFilenameWidth(determineFilenameWidth() + 5);
}, [props.filename]);
return ( return (
<> <>
<Typography
ref={invisibleFilenameRef}
component="span"
variant="body2" // Same as text field below!
sx={{position: "absolute", left: "-100%"}}
>
{props.filename}
</Typography>
<Typography variant="body1" sx={{marginTop: 2}}> <Typography variant="body1" sx={{marginTop: 2}}>
Attached file: Attached file:
</Typography> </Typography>
@ -365,16 +366,11 @@ const AttachmentBox = (props) => {
}}> }}>
<Icon type={file.type}/> <Icon type={file.type}/>
<Typography variant="body2" sx={{ marginLeft: 1, textAlign: 'left', color: 'text.primary' }}> <Typography variant="body2" sx={{ marginLeft: 1, textAlign: 'left', color: 'text.primary' }}>
<TextField <ExpandingTextField
margin="dense" minWidth={140}
placeholder="Attachment filename" variant="body2"
value={props.filename} value={props.filename}
onChange={handleFilenameChange} onChange={(ev) => props.onChangeFilename(ev.target.value)}
type="text"
variant="standard"
sx={{ width: `${filenameWidth}px`, borderBottom: "none" }}
InputProps={{ style: { fontSize: theme.typography.body2.fontSize } }}
inputProps={{ style: { paddingBottom: 0, paddingTop: 0 } }}
/> />
<br/> <br/>
{formatBytes(file.size)} {formatBytes(file.size)}
@ -385,6 +381,44 @@ const AttachmentBox = (props) => {
); );
}; };
const ExpandingTextField = (props) => {
const invisibleFieldRef = useRef();
const [textWidth, setTextWidth] = useState(props.minWidth);
const determineTextWidth = () => {
const boundingRect = invisibleFieldRef?.current?.getBoundingClientRect();
if (!boundingRect) {
return props.minWidth;
}
return (boundingRect.width >= props.minWidth) ? Math.round(boundingRect.width) : props.minWidth;
};
useEffect(() => {
setTextWidth(determineTextWidth() + 5);
}, [props.value]);
return (
<>
<Typography
ref={invisibleFieldRef}
component="span"
variant={props.variant}
sx={{position: "absolute", left: "-100%"}}
>
{props.value}
</Typography>
<TextField
margin="dense"
placeholder="Attachment filename"
value={props.value}
onChange={props.onChange}
type="text"
variant="standard"
sx={{ width: `${textWidth}px`, borderBottom: "none" }}
InputProps={{ style: { fontSize: theme.typography[props.variant].fontSize } }}
inputProps={{ style: { paddingBottom: 0, paddingTop: 0 } }}
/>
</>
)
};
const priorities = { const priorities = {
1: { label: "Minimum priority", file: priority1 }, 1: { label: "Minimum priority", file: priority1 },
2: { label: "Low priority", file: priority2 }, 2: { label: "Low priority", file: priority2 },