zhongrj
2025-11-24 276323dce9613867abb3f58a4cc2abbfb2fd0dea
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
import React from 'react';
import PropTypes from 'prop-types';
import Storage from 'webodm/classes/Storage';
import ErrorMessage from 'webodm/components/ErrorMessage';
import $ from 'jquery';
 
const STATE_IDLE = 0;
const STATE_RUNNING = 1;
const STATE_ERROR = 2;
const STATE_DONE = 3;
 
const ICON_CLASS_MAPPER = [
    // Idle
    'ddb-icon fa-fw', 
    // Running
    'fa fa-circle-notch fa-spin fa-fw', 
    // Error
    'fa fa-exclamation-triangle', 
    // Done
    'fas fa-external-link-alt'
];
 
const BUTTON_TEXT_MAPPER = [
    // Idle
    'Share to DroneDB',
    // Running
    'Sharing',
    // Error retry
    'Error, retry',
    // Done
    'View on DroneDB'
];
 
export default class ShareButton extends React.Component{
    static defaultProps = {
        task: null
    };
 
    static propTypes = {
        task: PropTypes.object.isRequired,
    };
 
    constructor(props){
        super(props);
 
        this.state = {            
            taskInfo: null,
            error: '',
            monitorTimeout: null
        };
 
    }
    
    componentDidMount(){
        this.updateTaskInfo(false);
    }
 
    updateTaskInfo = (showErrors) => {
        const { task } = this.props;
        return $.ajax({
                type: 'GET',
                url: `/api/plugins/dronedb/tasks/${task.id}/status`,
                contentType: 'application/json'
            }).done(taskInfo => {            
                this.setState({taskInfo});
                if (taskInfo.error && showErrors) this.setState({error: taskInfo.error});
            }).fail(error => {
                this.setState({error: error.statusText});
            });
    }
 
    componentWillUnmount(){
        if (this.monitorTimeout) clearTimeout(this.monitorTimeout);
    }
 
    shareToDdb = (formData) => {
        const { task } = this.props;
 
        return $.ajax({
            url: `/api/plugins/dronedb/tasks/${task.id}/share`,
            contentType: 'application/json',
            dataType: 'json',
            type: 'POST'
          }).done(taskInfo => {
 
            this.setState({taskInfo});
            this.monitorProgress();
 
          });
    }
 
    monitorProgress = () => {
        if (this.state.taskInfo.status == STATE_RUNNING){
            // Monitor progress
            this.monitorTimeout = setTimeout(() => {
                this.updateTaskInfo(true).always(this.monitorProgress);
            }, 3000);
        }
    }
 
    handleClick = e => {
 
        if (this.state.taskInfo.status == STATE_IDLE || this.state.taskInfo.status == STATE_ERROR) {
            this.shareToDdb();
        }
 
        if (this.state.taskInfo.status == STATE_DONE){
            window.open(this.state.taskInfo.shareUrl, '_blank');
        }
 
    }
 
 
    render(){
        const { taskInfo, error } = this.state;
 
        const getButtonIcon = () => {
 
            if (taskInfo == null) return "fa fa-circle-notch fa-spin fa-fw";            
            if (taskInfo.error) return "fa fa-exclamation-triangle";
            
            return ICON_CLASS_MAPPER[taskInfo.status];
        };
 
        const getButtonLabel = () => {
 
            if (taskInfo == null) return "Share to DroneDB";
            if (taskInfo.error) return "DroneDB plugin error";
 
            var text = BUTTON_TEXT_MAPPER[taskInfo.status];
 
            if (taskInfo.status == STATE_RUNNING && taskInfo.uploadedSize > 0 && taskInfo.totalSize > 0) {
                var progress = (taskInfo.uploadedSize / taskInfo.totalSize) * 100;
                text += ` (${progress.toFixed(2)}%)`;
            }
 
            return text;
        };
 
        return (
            <div className="share-button">
                <button className="btn btn-primary btn-sm" onClick={this.handleClick} disabled={this.state.taskInfo == null || this.state.taskInfo.status == STATE_RUNNING }>
                    <i className={getButtonIcon()}></i>&nbsp;
                    {getButtonLabel()}
                </button>
                {this.state.error && <div style={{ marginTop: '10px' }}><ErrorMessage bind={[this, 'error']} /></div> }
            </div>
        );
    }
}