|
| 1 | +import { useState, useCallback, useEffect, useRef, useMemo } from 'react' |
| 2 | +import { AlertTriangleIcon, Loader2Icon, RefreshCwIcon, DownloadIcon, ArrowDownIcon } from 'lucide-react' |
| 3 | +import { useTranslation } from 'react-i18next' |
| 4 | +import { useStore } from 'zustand' |
| 5 | +import { Alert, AlertDescription } from '@/components/ui/alert' |
| 6 | +import { Button } from '@/components/ui/button' |
| 7 | +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' |
| 8 | +import { isDevMode } from '@/constants/debugFeatures' |
| 9 | +import { fetchLog } from '@/lib/api/logs' |
| 10 | +import { cn, delayedPromise } from '@/lib/utils' |
| 11 | +import { authStore } from '@/store/authStore' |
| 12 | + |
| 13 | +const JMWALLETD_LOG_FILE_NAME = 'jmwalletd_stdout.log' |
| 14 | + |
| 15 | +interface SimpleAlert { |
| 16 | + variant: React.ComponentProps<typeof Alert>['variant'] |
| 17 | + message: string |
| 18 | +} |
| 19 | + |
| 20 | +interface LogViewerProps { |
| 21 | + value: string |
| 22 | + refresh: (signal: AbortSignal) => Promise<void> |
| 23 | +} |
| 24 | + |
| 25 | +function LogViewer({ value, refresh }: LogViewerProps) { |
| 26 | + const { t } = useTranslation() |
| 27 | + const logContentRef = useRef<HTMLPreElement>(null) |
| 28 | + const [isLoadingRefresh, setIsLoadingRefresh] = useState(false) |
| 29 | + const [logScrollProgress, setLogScrollProgress] = useState(0) |
| 30 | + const isScrolledToLogBottom = useMemo(() => logScrollProgress >= 0.995, [logScrollProgress]) |
| 31 | + |
| 32 | + const scrollToLogBottom = () => { |
| 33 | + logContentRef.current?.scrollTo({ |
| 34 | + top: logContentRef.current.scrollHeight, |
| 35 | + behavior: 'smooth', |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + const logScrollHandler = (event: React.UIEvent<HTMLPreElement>) => { |
| 40 | + const containerHeight = event.currentTarget.clientHeight |
| 41 | + const scrollHeight = event.currentTarget.scrollHeight |
| 42 | + |
| 43 | + const scrollTop = event.currentTarget.scrollTop |
| 44 | + setLogScrollProgress((scrollTop + containerHeight) / scrollHeight) |
| 45 | + } |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!value) return |
| 49 | + scrollToLogBottom() |
| 50 | + }, [value]) |
| 51 | + |
| 52 | + const handleRefresh = useCallback(async () => { |
| 53 | + if (isLoadingRefresh) return |
| 54 | + |
| 55 | + setIsLoadingRefresh(true) |
| 56 | + const abortCtrl = new AbortController() |
| 57 | + |
| 58 | + try { |
| 59 | + await refresh(abortCtrl.signal).then(() => delayedPromise(210)) |
| 60 | + } finally { |
| 61 | + setIsLoadingRefresh(false) |
| 62 | + } |
| 63 | + }, [isLoadingRefresh, refresh]) |
| 64 | + |
| 65 | + const handleDownload = useCallback(() => { |
| 66 | + const blob = new Blob([value], { type: 'text/plain' }) |
| 67 | + const url = URL.createObjectURL(blob) |
| 68 | + const a = document.createElement('a') |
| 69 | + a.href = url |
| 70 | + a.download = JMWALLETD_LOG_FILE_NAME |
| 71 | + document.body.appendChild(a) |
| 72 | + a.click() |
| 73 | + document.body.removeChild(a) |
| 74 | + setTimeout(() => { |
| 75 | + URL.revokeObjectURL(url) |
| 76 | + }, 0) |
| 77 | + }, [value]) |
| 78 | + |
| 79 | + return ( |
| 80 | + <Card className="flex flex-1 flex-col overflow-hidden pb-0"> |
| 81 | + <CardHeader className="flex flex-col justify-center gap-2 sm:flex-row sm:items-center sm:justify-between"> |
| 82 | + <CardTitle className="font-mono break-all">{JMWALLETD_LOG_FILE_NAME}</CardTitle> |
| 83 | + <div className="flex items-center justify-end gap-2"> |
| 84 | + <Button |
| 85 | + className="hover:[&>svg]:motion-safe:animate-bounce" |
| 86 | + variant="outline" |
| 87 | + onClick={handleDownload} |
| 88 | + disabled={!value || isLoadingRefresh} |
| 89 | + title={t('global.download')} |
| 90 | + > |
| 91 | + <DownloadIcon className="group/download" /> |
| 92 | + {t('global.download')} |
| 93 | + </Button> |
| 94 | + <Button variant="outline" onClick={handleRefresh} disabled={isLoadingRefresh} title={t('global.refresh')}> |
| 95 | + <RefreshCwIcon |
| 96 | + className={cn({ |
| 97 | + 'animate-spin': isLoadingRefresh, |
| 98 | + })} |
| 99 | + /> |
| 100 | + {t('global.refresh')} |
| 101 | + </Button> |
| 102 | + </div> |
| 103 | + </CardHeader> |
| 104 | + <CardContent className="relative flex-1 overflow-hidden rounded-b-xl p-0"> |
| 105 | + <pre |
| 106 | + onScrollEnd={logScrollHandler} |
| 107 | + ref={logContentRef} |
| 108 | + className="bg-muted/90 absolute inset-0 overflow-auto rounded-b-xl px-2 py-2 font-mono text-sm break-words whitespace-pre-wrap" |
| 109 | + > |
| 110 | + {value} |
| 111 | + </pre> |
| 112 | + <Button |
| 113 | + className={cn('absolute top-2 right-2 size-12', { |
| 114 | + 'opacity-25 hover:opacity-50': !isScrolledToLogBottom, |
| 115 | + hidden: isScrolledToLogBottom, |
| 116 | + })} |
| 117 | + variant={isScrolledToLogBottom ? 'ghost' : 'default'} |
| 118 | + disabled={isScrolledToLogBottom} |
| 119 | + size="icon" |
| 120 | + onClick={scrollToLogBottom} |
| 121 | + title={/* TODO: i18n */ 'Scroll to bottom'} |
| 122 | + > |
| 123 | + <ArrowDownIcon /> |
| 124 | + </Button> |
| 125 | + </CardContent> |
| 126 | + </Card> |
| 127 | + ) |
| 128 | +} |
| 129 | + |
| 130 | +interface LogsContentProps { |
| 131 | + className?: string |
| 132 | + enabled: boolean |
| 133 | +} |
| 134 | + |
| 135 | +export const LogsContent = ({ enabled, className }: LogsContentProps) => { |
| 136 | + const authState = useStore(authStore, (state) => state.state) |
| 137 | + const { t } = useTranslation() |
| 138 | + const [alert, setAlert] = useState<SimpleAlert>() |
| 139 | + const [isInitialized, setIsInitialized] = useState(false) |
| 140 | + const [logFileContent, setLogFileContent] = useState<string>() |
| 141 | + |
| 142 | + const refresh = useCallback( |
| 143 | + async (signal: AbortSignal) => { |
| 144 | + if (!authState?.auth?.token) { |
| 145 | + setAlert({ |
| 146 | + variant: 'destructive', |
| 147 | + message: 'No authentication token available. Please login again.', |
| 148 | + }) |
| 149 | + return Promise.reject(new Error('No authentication token')) |
| 150 | + } |
| 151 | + |
| 152 | + return fetchLog({ |
| 153 | + token: authState.auth.token, |
| 154 | + fileName: JMWALLETD_LOG_FILE_NAME, |
| 155 | + signal, |
| 156 | + }) |
| 157 | + .then((res) => (res.ok ? res.text() : Promise.reject(new Error(`HTTP ${res.status}`)))) |
| 158 | + .then((data) => { |
| 159 | + if (signal.aborted) return |
| 160 | + setAlert(undefined) |
| 161 | + setLogFileContent(data) |
| 162 | + }) |
| 163 | + .catch((e) => { |
| 164 | + if (signal.aborted) return |
| 165 | + |
| 166 | + const errorMessage = t('logs.error_loading_logs_failed', { |
| 167 | + reason: e.message || t('global.errors.reason_unknown'), |
| 168 | + }) |
| 169 | + |
| 170 | + if (isDevMode()) { |
| 171 | + setLogFileContent(`${errorMessage}\n`.repeat(1_000)) |
| 172 | + } |
| 173 | + |
| 174 | + setAlert({ |
| 175 | + variant: 'warning', |
| 176 | + message: errorMessage, |
| 177 | + }) |
| 178 | + }) |
| 179 | + }, |
| 180 | + [t, authState], |
| 181 | + ) |
| 182 | + |
| 183 | + if (enabled && !isInitialized) { |
| 184 | + const abortCtrl = new AbortController() |
| 185 | + refresh(abortCtrl.signal).finally(() => { |
| 186 | + if (abortCtrl.signal.aborted) return |
| 187 | + setIsInitialized(true) |
| 188 | + }) |
| 189 | + } |
| 190 | + |
| 191 | + if (!isInitialized) { |
| 192 | + return ( |
| 193 | + <div className={cn('flex items-center justify-center gap-2', className)}> |
| 194 | + <Loader2Icon className="h-4 w-4 animate-spin motion-reduce:hidden" /> |
| 195 | + {t('global.loading')} |
| 196 | + </div> |
| 197 | + ) |
| 198 | + } |
| 199 | + |
| 200 | + return ( |
| 201 | + <div className={cn('flex flex-col gap-3', className)}> |
| 202 | + {alert && ( |
| 203 | + <Alert variant={alert.variant}> |
| 204 | + <AlertTriangleIcon /> |
| 205 | + <AlertDescription>{alert.message}</AlertDescription> |
| 206 | + </Alert> |
| 207 | + )} |
| 208 | + |
| 209 | + {logFileContent && <LogViewer value={logFileContent} refresh={refresh} />} |
| 210 | + </div> |
| 211 | + ) |
| 212 | +} |
0 commit comments