hosts.tsx (14424B)
1 import m from 'mithril'; 2 import { Modal } from '@component/modal'; 3 import { PageHeader } from '@component/page-header'; 4 import { Breadcrumb } from '../component/breadcrumb'; 5 import { RealmSelector } from '../component/realm-selector'; 6 import { realmNavigation } from '../util/navigation'; 7 import { 8 createAdminRealmHost, 9 deleteAdminRealmHost, 10 loadAdminRealmHosts, 11 } from '../util/auth'; 12 import { type AdminRealm, type AdminRealmHost } from '../util/api'; 13 import { formatDate } from '../util/format'; 14 import { hostFormatError, normalizeHostValue } from '../util/hosts'; 15 16 interface HostsAttrs { 17 realmSlug: string | null; 18 realms: AdminRealm[] | null; 19 realmsError: Error | null; 20 canCreateRealm: boolean; 21 onAuthExpired: () => void; 22 } 23 24 function hostRequestError(e: any): boolean { 25 return e?.status === 401 || (e?.status === 400 && e?.code !== 'invalid_request'); 26 } 27 28 export const HostsPage: m.Component<HostsAttrs> = { 29 attrs: null as HostsAttrs | null, 30 loadedRealm: null as string | null, 31 loading: false, 32 redirecting: false, 33 hosts: null as AdminRealmHost[] | null, 34 error: null as Error | null, 35 notice: null as string | null, 36 search: '', 37 38 addOpen: false, 39 addValue: '', 40 addError: null as Error | string | null, 41 addInvalid: false, 42 creating: false, 43 44 removeTarget: null as string | null, 45 removeConfirm: '', 46 removeError: null as Error | null, 47 removing: false, 48 49 oninit(this: any, vnode: m.Vnode<HostsAttrs>) { 50 this.attrs = vnode.attrs; 51 this.ensure(); 52 }, 53 54 onupdate(this: any, vnode: m.Vnode<HostsAttrs>) { 55 this.attrs = vnode.attrs; 56 this.ensure(); 57 }, 58 59 ensure(this: any) { 60 const { realms, realmSlug } = this.attrs; 61 if (!realms) return; 62 63 if (realms.length === 0) { 64 this.loadedRealm = null; 65 this.hosts = null; 66 this.loading = false; 67 this.error = null; 68 if (!this.redirecting) { 69 this.redirecting = true; 70 m.route.set('/realms', null, { replace: true }); 71 } 72 return; 73 } 74 75 const selected = realmSlug && realms.some((realm) => realm.slug === realmSlug); 76 if (!selected) { 77 if (!this.redirecting) { 78 this.redirecting = true; 79 m.route.set(`/hosts/${encodeURIComponent(realms[0].slug)}`, null, { replace: true }); 80 } 81 return; 82 } 83 84 this.redirecting = false; 85 if (this.loadedRealm !== realmSlug) { 86 this.load(realmSlug!); 87 } 88 }, 89 90 async load(this: any, realmSlug: string) { 91 this.loadedRealm = realmSlug; 92 this.loading = true; 93 this.error = null; 94 this.notice = null; 95 this.hosts = null; 96 this.search = ''; 97 m.redraw(); 98 99 try { 100 const hosts = await loadAdminRealmHosts(realmSlug); 101 if (this.loadedRealm !== realmSlug) return; 102 this.hosts = hosts; 103 } catch (e: any) { 104 if (this.loadedRealm !== realmSlug) return; 105 if (e?.status === 401 || e?.status === 400) { 106 this.attrs.onAuthExpired(); 107 return; 108 } 109 this.error = e as Error; 110 } finally { 111 if (this.loadedRealm === realmSlug) this.loading = false; 112 m.redraw(); 113 } 114 }, 115 116 resetAdd(this: any) { 117 this.addOpen = false; 118 this.addValue = ''; 119 this.addError = null; 120 this.addInvalid = false; 121 this.creating = false; 122 }, 123 124 openAdd(this: any) { 125 this.resetAdd(); 126 this.addOpen = true; 127 m.redraw(); 128 }, 129 130 async submitAdd(this: any) { 131 const { realmSlug } = this.attrs; 132 if (!realmSlug) return; 133 134 const formatError = hostFormatError(this.addValue); 135 if (formatError) { 136 this.addError = formatError; 137 this.addInvalid = true; 138 m.redraw(); 139 return; 140 } 141 142 const host = normalizeHostValue(this.addValue); 143 this.creating = true; 144 this.addError = null; 145 this.addInvalid = false; 146 m.redraw(); 147 148 try { 149 const response = await createAdminRealmHost(realmSlug, host); 150 if (this.loadedRealm !== realmSlug) { 151 this.resetAdd(); 152 return; 153 } 154 this.hosts = [response.host, ...(this.hosts || [])]; 155 this.notice = `Host ${response.host.host} added.`; 156 this.resetAdd(); 157 } catch (e: any) { 158 if (hostRequestError(e)) { 159 this.attrs.onAuthExpired(); 160 return; 161 } 162 this.addError = (e as Error).message || 'Unable to add host.'; 163 this.addInvalid = true; 164 } finally { 165 this.creating = false; 166 m.redraw(); 167 } 168 }, 169 170 resetRemove(this: any) { 171 this.removeTarget = null; 172 this.removeConfirm = ''; 173 this.removeError = null; 174 this.removing = false; 175 }, 176 177 openRemove(this: any, host: string) { 178 this.resetRemove(); 179 this.removeTarget = host; 180 m.redraw(); 181 }, 182 183 async submitRemove(this: any) { 184 const { realmSlug } = this.attrs; 185 const target = this.removeTarget; 186 if (!realmSlug || !target || this.removing) return; 187 if (normalizeHostValue(this.removeConfirm) !== target) return; 188 189 this.removing = true; 190 this.removeError = null; 191 m.redraw(); 192 193 try { 194 const result = await deleteAdminRealmHost(realmSlug, target); 195 if (this.loadedRealm === realmSlug) { 196 this.hosts = (this.hosts || []).filter((entry) => entry.host !== result.host); 197 this.notice = `Host ${result.host} removed.`; 198 } 199 this.resetRemove(); 200 } catch (e: any) { 201 if (hostRequestError(e)) { 202 this.attrs.onAuthExpired(); 203 return; 204 } 205 this.removeError = e as Error; 206 } finally { 207 this.removing = false; 208 m.redraw(); 209 } 210 }, 211 212 view(this: any) { 213 const { realms, realmsError, realmSlug, canCreateRealm } = this.attrs; 214 const selected = 215 realms?.find((realm) => realm.slug === realmSlug) || realms?.[0] || null; 216 const canManage = Boolean(selected?.can_edit); 217 const nav = realmNavigation(realms, Boolean(canCreateRealm)); 218 const breadcrumbRealm = selected?.display_name || selected?.slug || realmSlug || ''; 219 const settingsHref = `/settings/${encodeURIComponent(selected?.slug || realmSlug || '')}`; 220 221 const breadcrumbItems = [ 222 { 223 label: nav.label, 224 ...(nav.isPlural ? { path: '/realms' } : {}), 225 }, 226 { label: breadcrumbRealm, path: settingsHref }, 227 { label: 'Hosts' }, 228 ]; 229 230 const hosts = this.hosts as AdminRealmHost[] | null; 231 const term = this.search.trim().toLowerCase(); 232 const visible = hosts ? hosts.filter((entry) => !term || entry.host.includes(term)) : []; 233 234 return ( 235 <div> 236 <Breadcrumb items={breadcrumbItems} /> 237 <PageHeader row title="Hosts" subtitle="Hostnames that resolve to this realm."> 238 <RealmSelector 239 realms={realms} 240 selected={selected} 241 onchange={(slug: string) => { 242 m.route.set(`/hosts/${encodeURIComponent(slug)}`); 243 }} 244 /> 245 </PageHeader> 246 247 {realmsError ? ( 248 <div class="alert" role="alert"> 249 {realmsError.message} 250 </div> 251 ) : null} 252 253 {this.error ? ( 254 <div class="alert" role="alert"> 255 <p>{this.error.message}</p> 256 <button 257 class="button button-secondary" 258 type="button" 259 onclick={() => { 260 if (realmSlug) { 261 this.load(realmSlug); 262 } 263 }} 264 > 265 Try again 266 </button> 267 </div> 268 ) : null} 269 270 {this.notice ? ( 271 <div class="notice" role="status"> 272 {this.notice} 273 </div> 274 ) : null} 275 276 {!realms ? ( 277 <p class="note">Loading realms</p> 278 ) : realms.length === 0 ? ( 279 <div class="empty"> 280 <p>No realms are available for this account.</p> 281 </div> 282 ) : this.loading || this.redirecting || !hosts ? ( 283 <div class="panel" aria-busy="true"> 284 <div class="skeleton skeleton-title"></div> 285 <div class="skeleton"></div> 286 <div class="skeleton skeleton-short"></div> 287 </div> 288 ) : ( 289 <section class="card" aria-label="Realm hosts"> 290 <div class="toolbar toolbar-panel"> 291 <div class="toolbar-search"> 292 <input 293 type="search" 294 value={this.search} 295 aria-label="Search hosts" 296 placeholder="Search hosts" 297 oninput={(event: Event) => { 298 this.search = (event.target as HTMLInputElement).value; 299 }} 300 /> 301 </div> 302 {canManage ? ( 303 <div class="toolbar-actions"> 304 <button class="button button-primary" type="button" onclick={() => this.openAdd()}> 305 Add host 306 </button> 307 </div> 308 ) : null} 309 </div> 310 311 <table class="table"> 312 <thead> 313 <tr> 314 <th scope="col">Hostname</th> 315 <th scope="col">Added</th> 316 {canManage ? <th scope="col">Actions</th> : null} 317 </tr> 318 </thead> 319 <tbody> 320 {visible.map((entry: AdminRealmHost) => ( 321 <tr> 322 <td data-label="Hostname"> 323 <span class="table-mono">{entry.host}</span> 324 </td> 325 <td data-label="Added" class="table-meta u-nowrap">{formatDate(entry.created_at)}</td> 326 {canManage ? ( 327 <td data-label="Actions"> 328 <button 329 class="button-inline button-danger" 330 type="button" 331 onclick={() => this.openRemove(entry.host)} 332 > 333 Remove 334 </button> 335 </td> 336 ) : null} 337 </tr> 338 ))} 339 </tbody> 340 </table> 341 {hosts.length > 0 && visible.length === 0 ? ( 342 <div class="empty"> 343 <p>No hosts match this search.</p> 344 </div> 345 ) : hosts.length === 0 ? ( 346 <div class="empty"> 347 <p>No hosts are assigned to this realm.</p> 348 </div> 349 ) : null} 350 </section> 351 )} 352 353 {this.addOpen ? ( 354 <Modal title="Add host" onClose={() => this.resetAdd()}> 355 <form 356 onsubmit={(event: Event) => { 357 event.preventDefault(); 358 this.submitAdd(); 359 }} 360 > 361 {this.addError ? ( 362 <div class="alert" role="alert"> 363 {typeof this.addError === 'string' ? this.addError : this.addError.message} 364 </div> 365 ) : null} 366 367 <div class="field"> 368 <span class="field-label">Hostname</span> 369 <input 370 class="field-control" 371 type="text" 372 value={this.addValue} 373 maxlength="253" 374 placeholder="login.acme.test" 375 autocomplete="off" 376 spellcheck="false" 377 data-modal-autofocus 378 disabled={this.creating} 379 aria-invalid={this.addInvalid ? 'true' : null} 380 oninput={(event: Event) => { 381 this.addValue = (event.target as HTMLInputElement).value; 382 this.addError = null; 383 this.addInvalid = false; 384 }} 385 /> 386 </div> 387 <p class="modal-note"> 388 Lowercase hostname. Do not include a scheme, port, path or wildcard. 389 </p> 390 391 <div class="modal-actions"> 392 <button 393 class="button button-secondary" 394 type="button" 395 disabled={this.creating} 396 onclick={() => this.resetAdd()} 397 > 398 Cancel 399 </button> 400 <button class="button button-primary" type="submit" disabled={this.creating}> 401 {this.creating ? 'Adding' : 'Add host'} 402 </button> 403 </div> 404 </form> 405 </Modal> 406 ) : null} 407 408 {this.removeTarget ? ( 409 <Modal title="Remove host" onClose={() => this.resetRemove()}> 410 <p class="modal-note"> 411 Remove {this.removeTarget}? This hostname will stop resolving to{' '} 412 {breadcrumbRealm}. Sessions for this realm on that host will end. Realm settings, 413 clients and users are unaffected. 414 </p> 415 {this.removeError ? ( 416 <div class="alert" role="alert"> 417 {this.removeError.message} 418 </div> 419 ) : null} 420 421 <div class="field"> 422 <span class="field-label">Confirm hostname</span> 423 <input 424 class="field-control" 425 type="text" 426 value={this.removeConfirm} 427 maxlength="253" 428 placeholder={this.removeTarget} 429 autocomplete="off" 430 spellcheck="false" 431 data-modal-autofocus 432 disabled={this.removing} 433 oninput={(event: Event) => { 434 this.removeConfirm = (event.target as HTMLInputElement).value; 435 this.removeError = null; 436 }} 437 /> 438 </div> 439 <p class="modal-note">Type the hostname exactly to confirm.</p> 440 441 <div class="modal-actions"> 442 <button 443 class="button button-secondary" 444 type="button" 445 disabled={this.removing} 446 onclick={() => this.resetRemove()} 447 > 448 Cancel 449 </button> 450 <button 451 class="button button-danger" 452 type="button" 453 disabled={ 454 this.removing || 455 normalizeHostValue(this.removeConfirm) !== this.removeTarget 456 } 457 onclick={() => this.submitRemove()} 458 > 459 {this.removing ? 'Removing' : 'Remove host'} 460 </button> 461 </div> 462 </Modal> 463 ) : null} 464 </div> 465 ); 466 }, 467 };