import * as React from 'react'; import { useState, useEffect } from 'react'; import ButtonBar from '../ConfigScreen/ButtonBar'; import { _ } from '@joplin/lib/locale'; const { connect } = require('react-redux'); import Setting from '@joplin/lib/models/Setting'; import { themeStyle } from '@joplin/lib/theme'; import ReportService, { ReportItem, ReportSection, RetryAllHandler } from '@joplin/lib/services/ReportService'; import Button, { ButtonLevel } from '../Button/Button'; import bridge from '../../services/bridge'; import styled from 'styled-components'; import { AppState } from '../../app.reducer'; import { writeFileSync } from 'fs'; interface Props { themeId: number; style: any; // eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied dispatch: Function; } const StyledAdvancedToolItem = styled.div` margin-bottom: 1em; `; async function exportDebugReportClick() { const filename = `syncReport-${new Date().getTime()}.csv`; const filePath = await bridge().showSaveDialog({ title: _('Please select where the sync status should be exported to'), defaultPath: filename, }); if (!filePath) return; const service = new ReportService(); const csv = (await service.basicItemList({ format: 'csv' })) as string; await writeFileSync(filePath, csv); } function StatusScreen(props: Props) { const [report, setReport] = useState([]); async function refreshScreen() { const service = new ReportService(); const r = await service.status(Setting.value('sync.target')); setReport(r); } useEffect(() => { void refreshScreen(); }, []); const theme = themeStyle(props.themeId); const style = { ...props.style, display: 'flex', flexDirection: 'column', }; const retryStyle = { ...theme.urlStyle, marginLeft: 5 }; const retryAllStyle = { ...theme.urlStyle, marginTop: 5, display: 'inline-block' }; const containerPadding = theme.configScreenPadding; const containerStyle = { ...theme.containerStyle, padding: containerPadding, flex: 1 }; function renderSectionTitle(key: string, title: string) { return (

{title}

); } function renderSectionRetryAll(key: string, retryAllHandler: RetryAllHandler) { return ( {_('Retry All')} ); } const renderRetryAll = (key: string, section: ReportSection) => { const items: React.JSX.Element[] = []; if (section.canRetryAll) { items.push(renderSectionRetryAll(`${key}_${section.title}`, async () => { await section.retryAllHandler(); void refreshScreen(); })); } return items; }; const renderSection = (key: string, section: ReportSection) => { let items = []; items.push(renderSectionTitle(section.title, section.title)); items = items.concat(renderRetryAll('top', section)); let currentListKey = ''; let listItems: React.JSX.Element[] = []; for (const n in section.body) { if (!section.body.hasOwnProperty(n)) continue; const item = section.body[n]; let text = ''; let retryLink = null; let itemType = null; if (typeof item === 'object') { if (item.canRetry) { const onClick = async () => { await item.retryHandler(); void refreshScreen(); }; retryLink = ( {_('Retry')} ); } text = item.text; itemType = item.type; } else { text = item; } if (itemType === 'openList') { currentListKey = (item as ReportItem).key; continue; } if (itemType === 'closeList') { items.push(); currentListKey = ''; listItems = []; continue; } if (!text) text = '\xa0'; if (currentListKey) { listItems.push(
  • {text} {retryLink}
  • , ); } else { items.push(
    {text} {retryLink}
    , ); } } items = items.concat(renderRetryAll('bottom', section)); return
    {items}
    ; }; function renderBody(report: ReportSection[]) { const sections = []; for (let i = 0; i < report.length; i++) { const section = report[i]; if (!section.body.length) continue; sections.push(renderSection(`${i}`, section)); } return
    {sections}
    ; } function renderTools() { return (

    {_('Advanced tools')}

    ); } const body = renderBody(report); return (
    {renderTools()} {body}
    props.dispatch({ type: 'NAV_BACK' })} />
    ); } const mapStateToProps = (state: AppState) => { return { themeId: state.settings.theme, settings: state.settings, locale: state.settings.locale, }; }; export default connect(mapStateToProps)(StatusScreen);