|
| 1 | +/** |
| 2 | + * Downloader (Signature) |
| 3 | + * |
| 4 | + * The lazy-loaded version with the property definition. |
| 5 | + * |
| 6 | + * Author: Yuchen Jin (cainmagi) |
| 7 | + * GitHub: https://github.com/cainmagi/dash-file-cache |
| 8 | + * License: MIT |
| 9 | + */ |
| 10 | + |
| 11 | +import React from "react"; |
| 12 | +import PropTypes, {InferProps} from "prop-types"; |
| 13 | +import {Downloader as RealComponent} from "../LazyLoader"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Default values of Downloader. |
| 17 | + */ |
| 18 | +const defaultProps = { |
| 19 | + url: "", |
| 20 | + allow_cross_origin: false, |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Property types of Downloader. |
| 25 | + */ |
| 26 | +const propTypes = { |
| 27 | + /** |
| 28 | + * The ID used to identify this component in Dash callbacks. |
| 29 | + */ |
| 30 | + id: PropTypes.string, |
| 31 | + |
| 32 | + /** |
| 33 | + * The URL used to access the data to be downloaded. |
| 34 | + * |
| 35 | + * Each time when this value is set, a download event will be triggered. After |
| 36 | + * triggering the download event, this value will be reset by a blank string. |
| 37 | + */ |
| 38 | + url: PropTypes.oneOfType([ |
| 39 | + PropTypes.string, |
| 40 | + PropTypes.exact({ |
| 41 | + /** |
| 42 | + * The URL used to access the data to be downloaded. |
| 43 | + */ |
| 44 | + url: PropTypes.string.isRequired, |
| 45 | + |
| 46 | + /** |
| 47 | + * A maunally configured file name. If this file name is configured, it will |
| 48 | + * be used when the file name cannot be parsed in the headers. This configuration |
| 49 | + * is useful when the URL is from a cross-origin site. |
| 50 | + */ |
| 51 | + file_name_fallback: PropTypes.string, |
| 52 | + }), |
| 53 | + ]), |
| 54 | + |
| 55 | + /** |
| 56 | + * The extra headers to be used when submitting the request of the downloading |
| 57 | + * event. |
| 58 | + * |
| 59 | + * This property may need to be configured when the downloading event needs to |
| 60 | + * add authentication information. |
| 61 | + */ |
| 62 | + headers: PropTypes.object, |
| 63 | + |
| 64 | + /** |
| 65 | + * A flag determineing whether the cross-origin downloading link can be used. |
| 66 | + * |
| 67 | + * If the data to be downloaded is from a cross-domain site, need to configure this |
| 68 | + * value as `True` while the remote site needs to configure the headers |
| 69 | + * Access-Control-Allow-Origin |
| 70 | + */ |
| 71 | + allow_cross_origin: PropTypes.bool, |
| 72 | + |
| 73 | + /** |
| 74 | + * The status code when a downloading event is finalized. |
| 75 | + * |
| 76 | + * If multiple downloading events are triggered by the same downloader, the later |
| 77 | + * event will overwrite the status from the former events. |
| 78 | + */ |
| 79 | + status: PropTypes.exact({ |
| 80 | + /** |
| 81 | + * The status code of the event. If the event is successful, this value should |
| 82 | + * be "success" once the downloading event is finalized. |
| 83 | + */ |
| 84 | + code: PropTypes.oneOf([ |
| 85 | + "success", |
| 86 | + "error-connect", |
| 87 | + "error-config", |
| 88 | + "error-io", |
| 89 | + "error-unknown", |
| 90 | + ]), |
| 91 | + |
| 92 | + /** |
| 93 | + * The HTTP code from the response. If the event is successful, this value should |
| 94 | + * be in the range of 200-299. |
| 95 | + */ |
| 96 | + http_code: PropTypes.number, |
| 97 | + }), |
| 98 | + |
| 99 | + /** |
| 100 | + * Dash-assigned callback that should be called to report property changes |
| 101 | + * to Dash, to make them available for callbacks. |
| 102 | + */ |
| 103 | + setProps: PropTypes.func, |
| 104 | + |
| 105 | + /** |
| 106 | + * Object that holds the loading state object coming from dash-renderer |
| 107 | + */ |
| 108 | + loading_state: PropTypes.shape({ |
| 109 | + /** |
| 110 | + * Determines if the component is loading or not |
| 111 | + */ |
| 112 | + is_loading: PropTypes.bool, |
| 113 | + /** |
| 114 | + * Holds which property is loading |
| 115 | + */ |
| 116 | + prop_name: PropTypes.string, |
| 117 | + /** |
| 118 | + * Holds the name of the component that is loading |
| 119 | + */ |
| 120 | + component_name: PropTypes.string, |
| 121 | + }), |
| 122 | +}; |
| 123 | + |
| 124 | +export type ComponentTypes = InferProps<typeof propTypes>; |
| 125 | + |
| 126 | +/** |
| 127 | + * Downloader is a React component based on StreamSaver. |
| 128 | + * |
| 129 | + * The StreamSaver.js project provides a customizable way to access and download |
| 130 | + * an online stream. This is the recommended downloader for practical uses. It has |
| 131 | + * the optimized performance for triggering multiple downloading events. |
| 132 | + */ |
| 133 | +const Downloader = (props: ComponentTypes = defaultProps): JSX.Element => { |
| 134 | + return ( |
| 135 | + <React.Suspense fallback={null}> |
| 136 | + <RealComponent {...props} /> |
| 137 | + </React.Suspense> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +Downloader.propTypes = propTypes; |
| 142 | +Downloader.defaultProps = defaultProps; |
| 143 | + |
| 144 | +export default Downloader; |
0 commit comments