@@ -81,62 +81,134 @@ function set_up_copy_button_clicks() {
8181
8282set_up_copy_button_clicks ( ) ;
8383
84- function set_ubuntu ( owner , repo ) {
85- const ubuntu = document . querySelector ( ".command.ubuntu" ) ;
84+ const platformCommandMap = {
85+ ubuntu : '.command.ubuntu' ,
86+ debian : '.command.debian' ,
87+ fedora : '.command.fedora' ,
88+ opensuse : '.command.suse' ,
89+ } ;
8690
87- console . log ( "Setting Ubuntu install command for:" , ubuntu ) ;
91+ let currentEventSource = null ;
8892
89- ubuntu . textContent = `wget -qO- https://packhub.dev/sh/ubuntu/github/${ owner } /${ repo } | sh`
93+ function extractGithubInfo ( value ) {
94+ const githubRegex = / h t t p s ? : \/ \/ g i t h u b \. c o m \/ ( [ ^ \/ ] + ) \/ ( [ ^ \/ ] + ) / ;
95+ return value . match ( githubRegex ) ;
9096}
9197
92- function set_debian ( owner , repo ) {
93- const ubuntu = document . querySelector ( ".command.debian" ) ;
98+ function resetDetection ( ) {
99+ // Hide all platform sections
100+ document . querySelectorAll ( '.platform-section' ) . forEach ( el => {
101+ el . style . display = 'none' ;
102+ } ) ;
94103
95- console . log ( "Setting Ubuntu install command for:" , ubuntu ) ;
104+ // Clear package list
105+ const packageList = document . getElementById ( 'package-list' ) ;
106+ packageList . innerHTML = '' ;
107+ document . getElementById ( 'detected-packages' ) . style . display = 'none' ;
96108
97- ubuntu . textContent = `wget -qO- https://packhub.dev/sh/debian/github/${ owner } /${ repo } | sh`
109+ // Clear status
110+ document . getElementById ( 'sse-status' ) . textContent = '' ;
111+ document . getElementById ( 'sse-status' ) . className = '' ;
98112}
99113
100- function set_fedora ( owner , repo ) {
101- const rpm = document . querySelector ( ".command.fedora" ) ;
114+ function startDetection ( owner , repo ) {
115+ if ( currentEventSource ) {
116+ currentEventSource . close ( ) ;
117+ currentEventSource = null ;
118+ }
102119
103- console . log ( "Setting Fedora install command for:" , rpm ) ;
120+ resetDetection ( ) ;
104121
105- rpm . textContent = `wget -qO- https://packhub.dev/sh/yum/github/${ owner } /${ repo } | sh`
106- }
122+ const status = document . getElementById ( 'sse-status' ) ;
123+ status . textContent = 'Detecting packages...' ;
124+ status . className = 'sse-status detecting' ;
107125
108- function set_suse ( owner , repo ) {
109- const rpm = document . querySelector ( ".command.suse" ) ;
126+ const packageList = document . getElementById ( 'package-list' ) ;
127+ document . getElementById ( 'detected-packages' ) . style . display = 'block' ;
110128
111- console . log ( "Setting Suse install command for:" , rpm ) ;
129+ const es = new EventSource ( `/sse/detect?owner=${ encodeURIComponent ( owner ) } &repo=${ encodeURIComponent ( repo ) } ` ) ;
130+ currentEventSource = es ;
131+
132+ es . addEventListener ( 'package' , ( e ) => {
133+ const data = JSON . parse ( e . data ) ;
134+ const li = document . createElement ( 'li' ) ;
135+ let text = data . file_name ;
136+ if ( data . distro ) {
137+ text += ` (${ data . distro } )` ;
138+ }
139+ text += ` [${ data . architecture } ]` ;
140+ li . textContent = text ;
141+ packageList . appendChild ( li ) ;
142+ } ) ;
143+
144+ es . addEventListener ( 'link' , ( e ) => {
145+ const data = JSON . parse ( e . data ) ;
146+ const section = document . querySelector ( `.platform-section[data-platform="${ data . platform } "]` ) ;
147+ if ( section ) {
148+ section . style . display = 'block' ;
149+ const commandSelector = platformCommandMap [ data . platform ] ;
150+ if ( commandSelector ) {
151+ const command = document . querySelector ( commandSelector ) ;
152+ if ( command ) {
153+ command . textContent = `wget -qO- ${ data . url } | sh` ;
154+ }
155+ }
156+ }
157+ } ) ;
158+
159+ es . addEventListener ( 'done' , ( e ) => {
160+ status . textContent = 'Detection complete.' ;
161+ status . className = 'sse-status complete' ;
162+ es . close ( ) ;
163+ currentEventSource = null ;
164+ } ) ;
165+
166+ es . addEventListener ( 'error' , ( e ) => {
167+ if ( e . data ) {
168+ const data = JSON . parse ( e . data ) ;
169+ status . textContent = 'Error: ' + data . message ;
170+ } else {
171+ status . textContent = 'Connection error.' ;
172+ }
173+ status . className = 'sse-status error' ;
174+ es . close ( ) ;
175+ currentEventSource = null ;
176+ } ) ;
177+
178+ es . onerror = ( ) => {
179+ if ( es . readyState === EventSource . CLOSED ) return ;
180+ status . textContent = 'Connection lost.' ;
181+ status . className = 'sse-status error' ;
182+ es . close ( ) ;
183+ currentEventSource = null ;
184+ } ;
185+ }
112186
113- rpm . textContent = `wget -qO- https://packhub.dev/sh/zypp/github/${ owner } /${ repo } | sh`
187+ function triggerDetection ( ) {
188+ const input = document . querySelector ( '.github-link' ) ;
189+ const value = input . value || input . placeholder ;
190+ const match = extractGithubInfo ( value ) ;
191+ if ( match ) {
192+ startDetection ( match [ 1 ] , match [ 2 ] ) ;
193+ } else {
194+ const status = document . getElementById ( 'sse-status' ) ;
195+ status . textContent = 'Please enter a valid GitHub repository URL.' ;
196+ status . className = 'sse-status error' ;
197+ }
114198}
115199
116200document . addEventListener ( "DOMContentLoaded" , ( ) => {
117- const inputElement = document . querySelector ( ".github-link" ) ;
118-
119- function extractGithubInfo ( value ) {
120- const githubRegex = / h t t p s ? : \/ \/ g i t h u b \. c o m \/ ( [ ^ \/ ] + ) \/ ( [ ^ \/ ] + ) / ;
121- const match = value . match ( githubRegex ) ;
122-
123- if ( match ) {
124- set_ubuntu ( match [ 1 ] , match [ 2 ] ) ;
125- set_debian ( match [ 1 ] , match [ 2 ] ) ;
126- set_fedora ( match [ 1 ] , match [ 2 ] ) ;
127- set_suse ( match [ 1 ] , match [ 2 ] ) ;
128- } else {
129- console . log ( "Invalid or missing GitHub URL" ) ;
130- }
201+ const detectBtn = document . getElementById ( 'detect-btn' ) ;
202+ if ( detectBtn ) {
203+ detectBtn . addEventListener ( 'click' , triggerDetection ) ;
131204 }
132205
206+ const inputElement = document . querySelector ( '.github-link' ) ;
133207 if ( inputElement ) {
134- // Extract initial value (if present)
135- extractGithubInfo ( inputElement . value || inputElement . placeholder ) ;
136-
137- // Listen for changes in the input field
138- inputElement . addEventListener ( "input" , ( event ) => {
139- extractGithubInfo ( inputElement . value || inputElement . placeholder ) ;
208+ inputElement . addEventListener ( 'keydown' , ( e ) => {
209+ if ( e . key === 'Enter' ) {
210+ triggerDetection ( ) ;
211+ }
140212 } ) ;
141213 }
142214} ) ;
0 commit comments