import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button } from '@heroui/react' import React, { useEffect, useState, useMemo } from 'react' import { getFileStr, setFileStr, convertMrsRuleset, getRuntimeConfig } from '@renderer/utils/ipc' import yaml from 'js-yaml' import { useTranslation } from 'react-i18next' import { BaseEditor } from '../base/base-editor' type Language = 'yaml' | 'javascript' | 'css' | 'json' | 'text' interface Props { onClose: () => void path: string type: string title: string privderType: string format?: string behavior?: string } const Viewer: React.FC = (props) => { const { t } = useTranslation() const { type, path, title, format, privderType, behavior, onClose } = props const [currData, setCurrData] = useState('') const [isLoading, setIsLoading] = useState(true) const language: Language = useMemo(() => { if (format === 'MrsRule') return 'text' if (type === 'Inline') return 'yaml' if (!format || format === 'YamlRule') return 'yaml' return 'text' }, [format, type]) useEffect(() => { const loadContent = async (): Promise => { setIsLoading(true) try { let fileContent: React.SetStateAction if (format === 'MrsRule') { let ruleBehavior: string = behavior || 'domain' if (!behavior) { try { const runtimeConfig = await getRuntimeConfig() const provider = runtimeConfig['rule-providers']?.[title] ruleBehavior = provider?.behavior || 'domain' } catch { ruleBehavior = 'domain' } } fileContent = await convertMrsRuleset(path, ruleBehavior) setCurrData(fileContent) return } if (type === 'Inline') { fileContent = await getFileStr('config.yaml') } else { fileContent = await getFileStr(path) } try { const parsedYaml = yaml.load(fileContent) if (privderType === 'proxy-providers') { setCurrData( yaml.dump({ proxies: parsedYaml[privderType][title].payload }) ) } else { setCurrData( yaml.dump({ rules: parsedYaml[privderType][title].payload }) ) } } catch { setCurrData(fileContent) } } finally { setIsLoading(false) } } loadContent() }, [path, type, title, format, privderType, behavior]) return ( {title} {isLoading ? (
{t('common.loading')}
) : ( setCurrData(value)} /> )}
{type === 'File' && format !== 'MrsRule' && ( )}
) } export default Viewer