import './RecordsView.css' import { NSpin, NPageHeader, NFlex, NButton, NIcon, NGrid, NGi, NStatistic, NDataTable, NInput, NModalProvider, createDiscreteApi } from 'naive-ui' import type { DataTableColumns } from 'naive-ui' import { useRecordStore, type Record, type SOARecord, RecordTypes, type ARecord } from '@/stores/records' import { getErrorInfo } from '@/apis/api' import { PlusSquare, RedoAlt, CheckCircle, Clock, Cogs, Search } from '@vicons/fa' import router from '@/router'; import RecordOps from '@/components/records/RecordOps' import RecordEditModal from '@/components/records/RecordEditModal' import i18n from '@/locale/i18n' import { ref } from 'vue' const { t } = i18n.global type Props = { domain: string } const recordStore = useRecordStore() const { notification } = createDiscreteApi(['notification']) const editModalShow = ref(false) const editingRecord = ref({} as Record) const loading = ref(true); const records = ref([] as Record[]); const soa = ref({} as SOARecord) const reloadRecords = () => records.value = recordStore.records?.filter(e => e.record_type !== RecordTypes.RecordTypeSOA) async function refreshRecords(domain: string) { try { await recordStore.loadRecords(domain) reloadRecords() soa.value = recordStore.records?.find(e => e.record_type === RecordTypes.RecordTypeSOA)?.content as SOARecord } catch (err) { const msg = getErrorInfo(err) notification.error(msg) console.error(err) } finally { loading.value = false; } } function goBack() { router.push('/domains') } function searchRecord(value: string) { if (value.length > 0) { records.value = recordStore.records?. filter(e => e.record_type !== RecordTypes.RecordTypeSOA). filter(e => !!~e.name.indexOf(value)) } else { records.value = recordStore.records?. filter(e => e.record_type !== RecordTypes.RecordTypeSOA) } } async function deleteRecord(domain: string, record: Record) { try { await recordStore.removeRecord(domain, record) reloadRecords() } catch (err) { const msg = getErrorInfo(err) notification.error(msg) console.error(err) } } function showEditing(domain: string, record: Record) { editModalShow.value = true editingRecord.value = record } function newRecord(domain: string) { showEditing(domain, { zone: `${domain}.`, ttl: 500, record_type: RecordTypes.RecordTypeA, content: { ip: '' } as ARecord } as Record) } const generateColumns = (domain: string) => [ { key: 'no', title: '#', render(_, index) { return index + 1 } }, { key: 'name', title: t("records.name"), }, { key: 'record_type', title: t('records.recordType') }, { key: 'content', title: t('records.content'), render(row: Record) { return Object.entries(row.content).map(i => i[1]).join(" ") } }, { key: 'ttl', title: 'TTL (s)' }, { key: '', render(row: Record) { return } } ] as DataTableColumns const statRefresh = () => ( {{ suffix: () => 's', label: () => ( <> {t('records.refresh')} ) }} ) const statRetry = () => ( {{ suffix: () => 's', label: () => ( <> {t('records.retry')} ) }} ) const statExpire = () => ( {{ suffix: () => 's', label: () => ( <> {t('records.expire')} ) }} ) const statTTL = () => ( {{ suffix: () => 's', label: () => ( <> {t('records.ttl')} ) }} ) function recordsViewBodyHeaderExtra() { return ( ) } function recordsViewBody({ domain }: Props) { const columns = generateColumns(domain) return ( {{ extra: () => ( newRecord(domain)}> {{ icon: () => , default: () => t('common.add') }} {{ prefix: () => }} ), default: () => }}
) } function RecordsView({ domain }: Props) { try { refreshRecords(domain) } catch (err) { const msg = getErrorInfo(err) notification.error(msg) console.error(err) } return (
editModalShow.value = v} /> { loading.value ? : }
) } RecordsView.displayName = 'RecordsView' export default RecordsView