summaryrefslogtreecommitdiff
path: root/client/components/results.component.js
blob: ac028d2ac2ecbd057b7abb5d5bc1bbb8eae6cb43 (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
import React, { Component } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'

import * as actions from '../actions'
import * as types from '../types'

function Results({ actions, loading, success, error, match, results, added }) {
  if (!success && !error) {
    return (
      <div className='results'>
      </div>
    )
  }
  if (loading) {
    return (
      <div className='results'>
        <i>Loading...</i>
      </div>
    )
  }

  if (error) {
    console.log(error)
    return (
      <div className='results'>
        <b>Error: {typeof error == 'string' ? error.replace(/_/g, ' ') : 'Server error'}</b>
      </div>
    )
  }

  if (!match) {
    return (
      <div className='results'>
        {added
          ? "New image! Added URL to database"
          : "No match found"
        }
      </div>
    )
  }

  return (
    <div className='results'>
      {results.map(({ phash, score, sha256, url }) => (
        <div className='result' key={sha256}>
          <div className='img'>
            <img
              src={url}
              onClick={() => {
                let searchURL = url
                if (searchURL.indexOf('http') !== 0) {
                  searchURL = window.location.origin + searchURL
                }
                actions.similar(searchURL, types.SIMILAR_THRESHOLD)
                actions.updateQuery({
                  image: url,
                  blob: null,
                  searchType: 'url',
                  thresholdChanged: false,
                  saveIfNotFound: false,
                  threshold: types.SIMILAR_THRESHOLD,
                })
              }}
            />
          </div>
          <br />
          {score == 0
            ? <span className='score'><b>Exact match</b></span>
            : <span className='score'>Score: {score}</span>
          }<br />
          <span className='sha256'>{sha256}</span>
          Phash: {phash.toString(16)}
        </div>
      ))}
    </div>
  )
}

const mapStateToProps = state => (state.api.similar || {})
const mapDispatchToProps = dispatch => ({
  actions: bindActionCreators({ ...actions }, dispatch),
})

export default connect(mapStateToProps, mapDispatchToProps)(Results)