|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogContent, |
| 5 | + DialogDescription, |
| 6 | + DialogFooter, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | +} from '../../ui/dialog'; |
| 10 | +import { Button } from '../../ui/button'; |
| 11 | +import { Input } from '../../ui/input'; |
| 12 | +import { Label } from '../../ui/label'; |
| 13 | +import { Alert, AlertDescription } from '../../ui/alert'; |
| 14 | +import { AlertCircle, Info } from 'lucide-react'; |
| 15 | + |
| 16 | +/** |
| 17 | + * HTTP Configuration Modal for managing resource entrypoints |
| 18 | + * Refactored from scratch using Shadcn UI components |
| 19 | + * |
| 20 | + * @param {Object} props |
| 21 | + * @param {string} props.entrypoints - Current entrypoints string |
| 22 | + * @param {Function} props.setEntrypoints - Function to update entrypoints in parent state (optional) |
| 23 | + * @param {Function} props.onSave - Save handler function (receives { entrypoints: string }) |
| 24 | + * @param {Function} props.onClose - Close modal handler |
| 25 | + * @param {boolean} props.isDisabled - Whether the resource is disabled |
| 26 | + */ |
| 27 | +const HTTPConfigModal = ({ |
| 28 | + entrypoints: initialEntrypoints, |
| 29 | + setEntrypoints: setParentEntrypoints, |
| 30 | + onSave, |
| 31 | + onClose, |
| 32 | + isDisabled |
| 33 | +}) => { |
| 34 | + const [localEntrypoints, setLocalEntrypoints] = useState(initialEntrypoints || 'websecure'); |
| 35 | + const [saving, setSaving] = useState(false); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + setLocalEntrypoints(initialEntrypoints || 'websecure'); |
| 39 | + }, [initialEntrypoints]); |
| 40 | + |
| 41 | + const handleInputChange = (e) => { |
| 42 | + setLocalEntrypoints(e.target.value); |
| 43 | + if (setParentEntrypoints) { |
| 44 | + setParentEntrypoints(e.target.value); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const handleSubmit = async (e) => { |
| 49 | + e.preventDefault(); |
| 50 | + if (isDisabled) return; |
| 51 | + |
| 52 | + setSaving(true); |
| 53 | + try { |
| 54 | + await onSave({ entrypoints: localEntrypoints || 'websecure' }); |
| 55 | + onClose(); |
| 56 | + } catch (err) { |
| 57 | + console.error('HTTP config update error:', err); |
| 58 | + } finally { |
| 59 | + setSaving(false); |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + return ( |
| 64 | + <Dialog open={true} onOpenChange={onClose}> |
| 65 | + <DialogContent className="sm:max-w-[500px]"> |
| 66 | + <DialogHeader> |
| 67 | + <DialogTitle>HTTP Router Configuration</DialogTitle> |
| 68 | + <DialogDescription> |
| 69 | + Configure HTTP entrypoints for this resource. |
| 70 | + </DialogDescription> |
| 71 | + </DialogHeader> |
| 72 | + <form onSubmit={handleSubmit}> |
| 73 | + <div className="space-y-4 py-4"> |
| 74 | + {isDisabled && ( |
| 75 | + <Alert variant="destructive"> |
| 76 | + <AlertCircle className="h-4 w-4" /> |
| 77 | + <AlertDescription> |
| 78 | + Configuration cannot be changed while the resource is disabled. |
| 79 | + </AlertDescription> |
| 80 | + </Alert> |
| 81 | + )} |
| 82 | + |
| 83 | + <div className="space-y-2"> |
| 84 | + <Label htmlFor="http-entrypoints"> |
| 85 | + HTTP Entry Points (comma-separated) |
| 86 | + </Label> |
| 87 | + <Input |
| 88 | + id="http-entrypoints" |
| 89 | + type="text" |
| 90 | + value={localEntrypoints} |
| 91 | + onChange={handleInputChange} |
| 92 | + placeholder="websecure,metrics" |
| 93 | + required |
| 94 | + disabled={saving || isDisabled} |
| 95 | + /> |
| 96 | + <div className="space-y-1"> |
| 97 | + <p className="text-xs text-muted-foreground"> |
| 98 | + Standard: websecure (HTTPS), web (HTTP). Default: websecure. |
| 99 | + </p> |
| 100 | + <Alert variant="info" className="py-2"> |
| 101 | + <Info className="h-3 w-3" /> |
| 102 | + <AlertDescription className="text-xs"> |
| 103 | + <strong>Note:</strong> Must match entrypoints defined in Traefik static configuration. |
| 104 | + </AlertDescription> |
| 105 | + </Alert> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + |
| 110 | + <DialogFooter> |
| 111 | + <Button |
| 112 | + type="button" |
| 113 | + variant="outline" |
| 114 | + onClick={onClose} |
| 115 | + disabled={saving} |
| 116 | + > |
| 117 | + Cancel |
| 118 | + </Button> |
| 119 | + <Button |
| 120 | + type="submit" |
| 121 | + disabled={saving || isDisabled} |
| 122 | + > |
| 123 | + {saving ? 'Saving...' : 'Save Configuration'} |
| 124 | + </Button> |
| 125 | + </DialogFooter> |
| 126 | + </form> |
| 127 | + </DialogContent> |
| 128 | + </Dialog> |
| 129 | + ); |
| 130 | +}; |
| 131 | + |
| 132 | +export default HTTPConfigModal; |
0 commit comments