|
| 1 | +import type { SubscriptionFormData } from '@/features/subscriptions/components/subscription-settings-schema' |
| 2 | +import { Button } from '@/components/ui/button' |
| 3 | +import { Input } from '@/components/ui/input' |
| 4 | +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' |
| 5 | +import { Textarea } from '@/components/ui/textarea' |
| 6 | +import { VariablesList } from '@/components/ui/variables-popover' |
| 7 | +import useDirDetection from '@/hooks/use-dir-detection' |
| 8 | +import { useIsMobile } from '@/hooks/use-mobile' |
| 9 | +import { Info, Plus, Trash2 } from 'lucide-react' |
| 10 | +import { UseFormReturn } from 'react-hook-form' |
| 11 | +import { useTranslation } from 'react-i18next' |
| 12 | + |
| 13 | +export interface SubscriptionResponseHeadersSectionProps { |
| 14 | + form: UseFormReturn<SubscriptionFormData> |
| 15 | +} |
| 16 | + |
| 17 | +export function SubscriptionResponseHeadersSection({ form }: SubscriptionResponseHeadersSectionProps) { |
| 18 | + const { t } = useTranslation() |
| 19 | + const dir = useDirDetection() |
| 20 | + const isMobile = useIsMobile() |
| 21 | + const infoPopoverSide = isMobile ? 'bottom' : dir === 'rtl' ? 'left' : 'right' |
| 22 | + const infoPopoverAlign = isMobile ? 'center' : 'start' |
| 23 | + |
| 24 | + const responseHeaders = (form.watch('response_headers') || {}) as Record<string, string> |
| 25 | + const responseHeaderEntries = Object.entries(responseHeaders) |
| 26 | + const responseHeaderCount = responseHeaderEntries.length |
| 27 | + |
| 28 | + const addResponseHeader = () => { |
| 29 | + const nextKey = `x-header-${Object.keys(responseHeaders).length + 1}` |
| 30 | + form.setValue( |
| 31 | + 'response_headers', |
| 32 | + { |
| 33 | + ...responseHeaders, |
| 34 | + [nextKey]: '', |
| 35 | + }, |
| 36 | + { shouldDirty: true }, |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + const updateResponseHeaderName = (currentKey: string, nextKey: string) => { |
| 41 | + const updatedHeaders = Object.fromEntries( |
| 42 | + responseHeaderEntries.map(([headerKey, headerValue]) => [headerKey === currentKey ? nextKey : headerKey, headerValue]), |
| 43 | + ) |
| 44 | + form.setValue('response_headers', updatedHeaders, { shouldDirty: true }) |
| 45 | + } |
| 46 | + |
| 47 | + const updateResponseHeaderValue = (headerKey: string, value: string) => { |
| 48 | + form.setValue( |
| 49 | + 'response_headers', |
| 50 | + { |
| 51 | + ...responseHeaders, |
| 52 | + [headerKey]: value, |
| 53 | + }, |
| 54 | + { shouldDirty: true }, |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + const removeResponseHeader = (headerKey: string) => { |
| 59 | + const updatedHeaders = { ...responseHeaders } |
| 60 | + delete updatedHeaders[headerKey] |
| 61 | + form.setValue('response_headers', updatedHeaders, { shouldDirty: true }) |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <div className="space-y-3"> |
| 66 | + <div className="flex items-start justify-between gap-2"> |
| 67 | + <div className="min-w-0 flex-1 space-y-2"> |
| 68 | + <h3 className="text-base font-semibold sm:text-lg">{t('settings.subscriptions.responseHeaders.title')}</h3> |
| 69 | + <p className="text-xs text-muted-foreground sm:text-sm">{t('settings.subscriptions.responseHeaders.description')}</p> |
| 70 | + </div> |
| 71 | + <Popover> |
| 72 | + <PopoverTrigger asChild> |
| 73 | + <Button type="button" variant="ghost" size="icon" className="h-8 w-8 shrink-0"> |
| 74 | + <Info className="h-4 w-4 text-muted-foreground" /> |
| 75 | + </Button> |
| 76 | + </PopoverTrigger> |
| 77 | + <PopoverContent className="w-[min(90vw,20rem)] p-3 sm:w-80" side={infoPopoverSide} align={infoPopoverAlign} sideOffset={5}> |
| 78 | + <div className="space-y-1.5"> |
| 79 | + <h4 className="mb-2 text-[11px] font-medium">{t('hostsDialog.variables.title')}</h4> |
| 80 | + <div className="max-h-[60vh] space-y-1 overflow-y-auto pr-1"> |
| 81 | + <VariablesList includeProfileTitle={true} includeFormat={true} /> |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + </PopoverContent> |
| 85 | + </Popover> |
| 86 | + </div> |
| 87 | + |
| 88 | + <div className="flex justify-end"> |
| 89 | + <Button type="button" variant="outline" size="sm" onClick={addResponseHeader}> |
| 90 | + <Plus className="mr-1.5 h-3.5 w-3.5" /> |
| 91 | + {t('settings.subscriptions.responseHeaders.addHeader')} |
| 92 | + </Button> |
| 93 | + </div> |
| 94 | + |
| 95 | + <div className="space-y-3"> |
| 96 | + {responseHeaderCount > 0 ? ( |
| 97 | + responseHeaderEntries.map(([headerKey, headerValue], index) => ( |
| 98 | + <div key={`sub-header-${index}`} className="space-y-2 rounded-lg border bg-card/50 p-3"> |
| 99 | + <div className="flex items-start gap-2"> |
| 100 | + <Input |
| 101 | + value={headerKey} |
| 102 | + onChange={e => updateResponseHeaderName(headerKey, e.target.value)} |
| 103 | + placeholder={t('settings.subscriptions.responseHeaders.headerName')} |
| 104 | + className="font-mono text-xs" |
| 105 | + /> |
| 106 | + <Button |
| 107 | + type="button" |
| 108 | + variant="ghost" |
| 109 | + size="icon" |
| 110 | + className="h-8 w-8 shrink-0 text-destructive hover:bg-destructive/10" |
| 111 | + onClick={() => removeResponseHeader(headerKey)} |
| 112 | + > |
| 113 | + <Trash2 className="h-4 w-4" /> |
| 114 | + </Button> |
| 115 | + </div> |
| 116 | + <Textarea |
| 117 | + value={headerValue} |
| 118 | + onChange={e => updateResponseHeaderValue(headerKey, e.target.value)} |
| 119 | + placeholder={t('settings.subscriptions.responseHeaders.headerValue')} |
| 120 | + className="min-h-[60px] resize-none font-mono text-xs" |
| 121 | + rows={2} |
| 122 | + /> |
| 123 | + </div> |
| 124 | + )) |
| 125 | + ) : ( |
| 126 | + <div className="rounded-lg border border-dashed border-border/70 px-4 py-8 text-center"> |
| 127 | + <p className="text-sm text-muted-foreground">{t('settings.subscriptions.responseHeaders.noHeaders')}</p> |
| 128 | + </div> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + ) |
| 133 | +} |
0 commit comments