summaryrefslogtreecommitdiff
path: root/src/components/ListView.js
blob: 409c8023a2ae578943c21b523f753edd149198d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React from "react";

import Pagination from "@mui/material/Pagination";

import ListItem from "./ListItem";
import SearchForm from "./SearchForm";

class ListView extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            scans: [],
            filter: {
                field: null,
                value: null
            },
            page: 1,
            totalPages: 1
        };

        this.filter = this.filter.bind(this);
        this.filterString = this.filterString.bind(this);
        this.getData = this.getData.bind(this);
        this.queryString = this.queryString.bind(this);
        this.setPage = this.setPage.bind(this);
    }

    componentDidMount() {
        this.getData();
    }

    //
    // Helpers
    //

    filterString() {
        return this.state.filter.field == null ||
            this.state.filter.value == null
            ? null
            : this.state.filter.field + "=" + this.state.filter.value;
    }

    queryString() {
        return [
            `limit=${window.injectedEnv.PER_PAGE}`,
            `skip=${(this.state.page - 1) * window.injectedEnv.PER_PAGE}`,
            this.filterString()
        ]
            .filter(x => x !== null)
            .join("&");
    }

    // Fetch data from external source, update state
    getData() {
        fetch(
            window.injectedEnv.COLLECTOR_URL +
                "/sc/v0/get?" +
                this.queryString(),
            {
                headers: {
                    Authorization: "Bearer " + this.props.token
                }
            }
        )
            // TODO: Look at `status` or return code or both?
            .then(resp => {
                if (resp.status !== 200)
                    throw new Error(
                        `Unexpected HTTP response code from soc_collector: ${resp.status} ${resp.statusText}`
                    );
                this.setState({
                    totalPages: parseInt(resp.headers.get("X-Total-Count"))
                });
                return resp.json();
            })
            .then(json => {
                if (json.status != "success")
                    throw new Error(
                        `Unexpected status from soc_collector: ${json.status}`
                    );
                this.setState({
                    scans: json.docs.map(d => ({
                        ...d,
                        timestamp_in_utc: new Date(
                            d.timestamp_in_utc.replace(/ UTC$/, "Z")
                        )
                    }))
                });
            })
            .catch(e => this.props.setError(e));
    }

    //
    // Event handlers
    //

    filter(field, value) {
        this.setState(
            {
                filter: {
                    field: field,
                    value: value
                },
                page: 1
            },
            this.getData
        );
    }

    setPage(event, value) {
        this.setState({ page: value }, () => {
            this.getData();
            window.scrollTo(0, 0);
        });
    }

    render() {
        return (
            <div id="list-container">
                <div id="controls">
                    <div id="action"></div>
                    <div id="search">
                        <SearchForm filter={this.filter} />
                    </div>
                </div>
                <table id="main">
                    <tbody>
                        {this.state.scans
                            .sort((a, b) =>
                                a.timestamp_in_utc > b.timestamp_in_utc ? 1 : -1
                            )
                            .map(scan =>
                                Object.entries(scan.result)
                                    .filter(
                                        ([_, res]) =>
                                            res.vulnerable ||
                                            res.investigation_needed
                                    )
                                    .map(([id, res]) => (
                                        <ListItem
                                            summary={true}
                                            {...scan}
                                            {...res}
                                            key={scan._id + id}
                                        />
                                    ))
                            )
                            .flat()}
                    </tbody>
                </table>
                <div id="pagination">
                    <Pagination
                        page={this.state.page}
                        count={this.state.totalPages}
                        onChange={this.setPage}
                        variant="outlined"
                        shape="rounded"
                        showFirstButton
                        showLastButton
                    />
                </div>
            </div>
        );
    }
}

export default ListView;