@@ -15,19 +15,18 @@ import { stackedThreadToast, toastManager } from "./ui/toast";
1515import { Tooltip , TooltipPopup , TooltipTrigger } from "./ui/tooltip" ;
1616
1717/**
18- * Updates every outdated provider on every connected machine at once, then
19- * reports the results in one toast. Each server queues updates that share an
20- * installer, so sending them all together is safe. The server still checks
21- * permissions: a session that cannot operate a machine gets a failure line for
22- * it. Renders nothing when no machine has a one-click update.
18+ * Every outdated provider with a one-click update on every connected machine,
19+ * plus a function that updates them all and reports the results in one toast.
20+ * Each server queues updates that share an installer, so sending them all
21+ * together is safe. The server still checks permissions: a session that
22+ * cannot operate a machine gets a failure line for it. The returned promise
23+ * settles when every update has finished.
2324 */
24- export function ProviderUpdatesAction ( ) {
25+ export function useProviderUpdates ( ) {
2526 const { environments } = useEnvironments ( ) ;
2627 const updateProvider = useAtomCommand ( serverEnvironment . updateProvider , {
2728 reportFailure : false ,
2829 } ) ;
29- const pending = useRef ( false ) ;
30- const [ isPending , setIsPending ] = useState ( false ) ;
3130 const machines = useMemo (
3231 ( ) =>
3332 environments . flatMap ( ( environment ) => {
@@ -44,6 +43,41 @@ export function ProviderUpdatesAction() {
4443 } ) ,
4544 [ environments ] ,
4645 ) ;
46+
47+ const updateAll = async ( ) => {
48+ const runs = await Promise . all (
49+ machines . flatMap ( ( { environmentId, label, candidates } ) =>
50+ candidates . map ( async ( candidate ) : Promise < ProviderUpdateRun > => ( {
51+ machineLabel : label ,
52+ driver : candidate . driver ,
53+ instanceId : candidate . instanceId ,
54+ result : await updateProvider ( {
55+ environmentId,
56+ input : { provider : candidate . driver , instanceId : candidate . instanceId } ,
57+ } ) ,
58+ } ) ) ,
59+ ) ,
60+ ) ;
61+ const view = getProviderUpdateRunToastView ( runs ) ;
62+ if ( view ) {
63+ toastManager . add (
64+ stackedThreadToast ( {
65+ ...view ,
66+ description : < span className = "whitespace-pre-line" > { view . description } </ span > ,
67+ } ) ,
68+ ) ;
69+ }
70+ } ;
71+
72+ return { machines, updateAll } ;
73+ }
74+
75+ /** Provider settings button for {@link useProviderUpdates}. Renders nothing
76+ when no machine has a one-click update. */
77+ export function ProviderUpdatesAction ( ) {
78+ const { machines, updateAll } = useProviderUpdates ( ) ;
79+ const pending = useRef ( false ) ;
80+ const [ isPending , setIsPending ] = useState ( false ) ;
4781 // Candidates leave the list as soon as their servers report them queued, so
4882 // keep the button while the run is in flight.
4983 if ( machines . length === 0 && ! isPending ) {
@@ -55,28 +89,7 @@ export function ProviderUpdatesAction() {
5589 pending . current = true ;
5690 setIsPending ( true ) ;
5791 try {
58- const runs = await Promise . all (
59- machines . flatMap ( ( { environmentId, label, candidates } ) =>
60- candidates . map ( async ( candidate ) : Promise < ProviderUpdateRun > => ( {
61- machineLabel : label ,
62- driver : candidate . driver ,
63- instanceId : candidate . instanceId ,
64- result : await updateProvider ( {
65- environmentId,
66- input : { provider : candidate . driver , instanceId : candidate . instanceId } ,
67- } ) ,
68- } ) ) ,
69- ) ,
70- ) ;
71- const view = getProviderUpdateRunToastView ( runs ) ;
72- if ( view ) {
73- toastManager . add (
74- stackedThreadToast ( {
75- ...view ,
76- description : < span className = "whitespace-pre-line" > { view . description } </ span > ,
77- } ) ,
78- ) ;
79- }
92+ await updateAll ( ) ;
8093 } finally {
8194 pending . current = false ;
8295 setIsPending ( false ) ;
0 commit comments