import { useState } from 'react' import useSWR from 'swr' import { ActionIcon, Avatar, Badge, Code, Group, Loader, Pagination, Paper, ScrollArea, Stack, Table, Text, TextInput, Title, Tooltip, } from '@mantine/core' import { useMediaQuery } from '@mantine/hooks' import { createFileRoute, useParams } from '@tanstack/react-router' import { TbAlertCircle, TbHistory, TbHome2, TbSearch, TbX, } from 'react-icons/tb' import { API_URLS } from '../config/api' export const Route = createFileRoute('/apps/$appId/logs')({ component: AppLogsPage, }) interface LogEntry { id: string createdAt: string action: string desc: string username: string village: string } const fetcher = (url: string) => fetch(url).then((res) => res.json()) const ACTION_COLOR: Record = { LOGIN: 'teal', LOGOUT: 'gray', CREATE: 'blue', UPDATE: 'yellow', DELETE: 'red', } function getActionColor(action: string) { return ACTION_COLOR[action.toUpperCase()] ?? 'brand-blue' } function AppLogsPage() { const { appId } = useParams({ from: '/apps/$appId/logs' }) const [page, setPage] = useState(1) const [search, setSearch] = useState('') const [searchQuery, setSearchQuery] = useState('') const isDesaPlus = appId === 'desa-plus' const isMobile = useMediaQuery('(max-width: 768px)') const apiUrl = isDesaPlus ? API_URLS.getLogsAllVillages(page, searchQuery) : null const { data: response, error, isLoading } = useSWR(apiUrl, fetcher) const logs: LogEntry[] = response?.data?.log || [] const handleSearchChange = (val: string) => { setSearch(val) if (val.length >= 3 || val.length === 0) { setSearchQuery(val) setPage(1) } } const handleClearSearch = () => { setSearch('') setSearchQuery('') setPage(1) } if (!isDesaPlus) { return ( Activity Logs — Coming Soon This feature is currently available for Desa+. Other apps coming soon. ) } return ( Activity Logs {isLoading ? 'Loading logs...' : `${(response?.data?.total ?? 0).toLocaleString()} events across all villages`} } size="sm" rightSection={ search ? ( ) : null } value={search} onChange={(e) => handleSearchChange(e.currentTarget.value)} radius="md" /> {isLoading ? ( ) : error ? ( Failed to load logs from the API. ) : logs.length === 0 ? ( {searchQuery ? 'No activity found for this search.' : 'No activity logs yet.'} ) : ( Timestamp User & Village Action Description {logs.map((log) => ( {log.createdAt.endsWith('lalu') ? ( {log.createdAt} ) : ( {log.createdAt.split(' ').slice(1).join(' ')} {log.createdAt.split(' ')[0]} )} {log.username.charAt(0)} {log.username} {log.village} {log.action} {log.desc} ))}
)} {!isLoading && !error && response?.data?.totalPage > 0 && ( )}
) }