@@ -2,25 +2,27 @@ import { Container } from "@/models/Container";
22
33type ContainerActions = "start" | "stop" | "restart" ;
44export const useContainerActions = ( container : Ref < Container > ) => {
5- const { showToast } = useToast ( ) ;
5+ const { showToast, removeToast } = useToast ( ) ;
6+ const { t } = useI18n ( ) ;
67
78 const actionStates = reactive ( {
89 stop : false ,
910 restart : false ,
1011 start : false ,
12+ update : false ,
1113 } ) ;
1214
1315 async function actionHandler ( action : ContainerActions ) {
1416 const actionUrl = `/api/hosts/${ container . value . host } /containers/${ container . value . id } /actions/${ action } ` ;
1517
1618 const errors = {
17- 404 : " container not found",
18- 500 : " unable to complete action",
19- 400 : " invalid action",
19+ 404 : t ( "error. container- not- found") ,
20+ 500 : t ( "error. unable-to- complete- action") ,
21+ 400 : t ( "error. invalid- action") ,
2022 } as Record < number , string > ;
2123
22- const defaultError = " something went wrong";
23- const toastTitle = "Action Failed" ;
24+ const defaultError = t ( "error. something- went- wrong") ;
25+ const toastTitle = t ( "error.action-failed" ) ;
2426
2527 actionStates [ action ] = true ;
2628
@@ -37,10 +39,103 @@ export const useContainerActions = (container: Ref<Container>) => {
3739 actionStates [ action ] = false ;
3840 }
3941
42+ async function update ( ) {
43+ const updateUrl = `/api/hosts/${ container . value . host } /containers/${ container . value . id } /actions/update` ;
44+ const toastId = "container-update" ;
45+ let reader : ReadableStreamDefaultReader < Uint8Array > | undefined ;
46+
47+ actionStates . update = true ;
48+
49+ showToast (
50+ {
51+ id : toastId ,
52+ title : t ( "toolbar.update" ) ,
53+ message : t ( "toolbar.update-pulling" ) ,
54+ type : "info" ,
55+ } ,
56+ { once : true } ,
57+ ) ;
58+
59+ try {
60+ const response = await fetch ( withBase ( updateUrl ) , { method : "POST" } ) ;
61+ if ( ! response . ok ) {
62+ removeToast ( toastId ) ;
63+ showToast ( { type : "error" , message : t ( "error.unable-to-update" ) , title : t ( "error.update-failed" ) } ) ;
64+ return ;
65+ }
66+
67+ reader = response . body ?. getReader ( ) ;
68+ if ( ! reader ) return ;
69+
70+ const decoder = new TextDecoder ( ) ;
71+ let buffer = "" ;
72+
73+ while ( true ) {
74+ const { done, value } = await reader . read ( ) ;
75+ if ( done ) break ;
76+
77+ buffer += decoder . decode ( value , { stream : true } ) ;
78+ const lines = buffer . split ( "\n\n" ) ;
79+ buffer = lines . pop ( ) ?? "" ;
80+
81+ for ( const chunk of lines ) {
82+ const dataLine = chunk . split ( "\n" ) . find ( ( l ) => l . startsWith ( "data: " ) ) ;
83+ if ( ! dataLine ) continue ;
84+
85+ const data = JSON . parse ( dataLine . slice ( 6 ) ) ;
86+
87+ switch ( data . status ) {
88+ case "pulling" :
89+ break ;
90+ case "recreating" :
91+ removeToast ( toastId ) ;
92+ showToast (
93+ {
94+ id : toastId ,
95+ title : t ( "toolbar.update" ) ,
96+ message : t ( "toolbar.update-recreating" ) ,
97+ type : "info" ,
98+ } ,
99+ { once : true } ,
100+ ) ;
101+ break ;
102+ case "done" :
103+ case "up-to-date" :
104+ removeToast ( toastId ) ;
105+ showToast (
106+ {
107+ title : t ( "toolbar.update" ) ,
108+ message : t ( `toolbar.update-${ data . status } ` ) ,
109+ type : "info" ,
110+ } ,
111+ { expire : 3000 } ,
112+ ) ;
113+ break ;
114+ case "error" :
115+ removeToast ( toastId ) ;
116+ showToast ( {
117+ type : "error" ,
118+ message : data . error || t ( "error.unknown-error" ) ,
119+ title : t ( "error.update-failed" ) ,
120+ } ) ;
121+ break ;
122+ }
123+ }
124+ }
125+ } catch ( error ) {
126+ removeToast ( toastId ) ;
127+ showToast ( { type : "error" , message : t ( "error.something-went-wrong" ) , title : t ( "error.update-failed" ) } ) ;
128+ } finally {
129+ reader ?. cancel ( ) ;
130+ actionStates . update = false ;
131+ }
132+ }
133+
40134 return {
41135 actionStates,
42136 start : ( ) => actionHandler ( "start" ) ,
43137 stop : ( ) => actionHandler ( "stop" ) ,
44138 restart : ( ) => actionHandler ( "restart" ) ,
139+ update,
45140 } ;
46141} ;
0 commit comments