Skip to content

Commit

Permalink
storage: Optional spinners in StorageButtons
Browse files Browse the repository at this point in the history
  • Loading branch information
mvollmer authored and jelly committed Aug 9, 2023
1 parent 13fa26d commit c0089cf
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions pkg/storaged/storage-controls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class StorageControl extends React.Component {
}
}

function checked(callback) {
function checked(callback, setSpinning) {
return function (event) {
if (!event)
return;
Expand All @@ -82,31 +82,41 @@ function checked(callback) {
return;

const promise = client.run(callback);
if (promise)
if (promise) {
if (setSpinning)
setSpinning(true);
promise.finally(() => {
if (setSpinning)
setSpinning(false);
});
promise.catch(function (error) {
dialog_open({
Title: _("Error"),
Body: error.toString()
});
});
}
event.stopPropagation();
};
}

export const StorageButton = ({ id, kind, excuse, onClick, children, ariaLabel, onlyWide }) => (
<StorageControl excuse={excuse}
content={excuse => (
<Button id={id}
aria-label={ariaLabel}
onClick={checked(onClick)}
variant={kind || "secondary"}
isDisabled={!!excuse}
className={onlyWide ? "show-only-when-wide" : null}
style={excuse ? { pointerEvents: 'none' } : null}>
{children}
</Button>
)} />
);
export const StorageButton = ({ id, kind, excuse, onClick, children, ariaLabel, onlyWide, spinner }) => {
const [spinning, setSpinning] = useState(false);

return <StorageControl excuse={excuse}
content={excuse => (
<Button id={id}
aria-label={ariaLabel}
onClick={checked(onClick, setSpinning)}
variant={kind || "secondary"}
isDisabled={!!excuse || (spinner && spinning)}
className={onlyWide ? "show-only-when-wide" : null}
style={excuse ? { pointerEvents: 'none' } : null}
isLoading={spinner ? spinning : undefined}>
{children}
</Button>
)} />;
};

export const StorageLink = ({ id, excuse, onClick, children }) => (
<StorageControl excuse={excuse}
Expand Down

0 comments on commit c0089cf

Please sign in to comment.