'use client'; import {useEffect, useState} from 'react'; import {Input} from '@/components/ui/input'; import {Label} from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import {Switch} from '@/components/ui/switch'; import {Textarea} from '@/components/ui/textarea'; import type {ProxyRoutePoWConfig} from '@/lib/services/openflare'; import {listToText, parseTextareaList} from './helpers'; interface PowConfigPanelProps { enabled: boolean; config: ProxyRoutePoWConfig; onChange: (enabled: boolean, config: ProxyRoutePoWConfig) => void; } type PowListDraft = { whitelist: Record; blacklist: Record; }; function buildDraft(config: ProxyRoutePoWConfig): PowListDraft { return { whitelist: { ips: listToText(config.whitelist?.ips), ip_cidrs: listToText(config.whitelist?.ip_cidrs), paths: listToText(config.whitelist?.paths), path_regexes: listToText(config.whitelist?.path_regexes), user_agents: listToText(config.whitelist?.user_agents), }, blacklist: { ips: listToText(config.blacklist?.ips), ip_cidrs: listToText(config.blacklist?.ip_cidrs), paths: listToText(config.blacklist?.paths), path_regexes: listToText(config.blacklist?.path_regexes), user_agents: listToText(config.blacklist?.user_agents), }, }; } function applyDraft( config: ProxyRoutePoWConfig, draft: PowListDraft, ): ProxyRoutePoWConfig { return { ...config, whitelist: { ips: parseTextareaList(draft.whitelist.ips), ip_cidrs: parseTextareaList(draft.whitelist.ip_cidrs), paths: parseTextareaList(draft.whitelist.paths), path_regexes: parseTextareaList(draft.whitelist.path_regexes), user_agents: parseTextareaList(draft.whitelist.user_agents), }, blacklist: { ips: parseTextareaList(draft.blacklist.ips), ip_cidrs: parseTextareaList(draft.blacklist.ip_cidrs), paths: parseTextareaList(draft.blacklist.paths), path_regexes: parseTextareaList(draft.blacklist.path_regexes), user_agents: parseTextareaList(draft.blacklist.user_agents), }, }; } function PowListCard({ title, scope, draft, onUpdate, }: { title: string; scope: keyof PowListDraft; draft: PowListDraft; onUpdate: ( scope: keyof PowListDraft, key: keyof ProxyRoutePoWConfig['whitelist'], value: string, ) => void; }) { const fields: Array<{ key: keyof ProxyRoutePoWConfig['whitelist']; label: string; placeholder: string; }> = [ {key: 'ips', label: 'IP', placeholder: '每行一个 IP'}, {key: 'ip_cidrs', label: 'IP CIDR', placeholder: '每行一个网段,如 10.0.0.0/8'}, {key: 'paths', label: '路径', placeholder: '每行一个路径前缀'}, {key: 'path_regexes', label: '路径正则', placeholder: '每行一个正则表达式'}, {key: 'user_agents', label: 'User-Agent', placeholder: '每行一个 UA 关键词或正则'}, ]; return (

{title}

{fields.map((field) => (