|
| 1 | +import React, {FC, useState, useCallback, useEffect} from 'react' |
| 2 | +import { |
| 3 | + Button, |
| 4 | + ComponentColor, |
| 5 | + ComponentSize, |
| 6 | + ComponentStatus, |
| 7 | + IconFont, |
| 8 | + Input, |
| 9 | + List, |
| 10 | + TechnoSpinner, |
| 11 | + Overlay, |
| 12 | + EmptyState, |
| 13 | +} from '@influxdata/clockface' |
| 14 | +import {RemoteDataState} from 'src/types' |
| 15 | +import './SaveAsScript.scss' |
| 16 | +import {CLOUD} from 'src/shared/constants' |
| 17 | +import {useHistory} from 'react-router-dom' |
| 18 | +import {useSelector} from 'react-redux' |
| 19 | +import {getOrg} from 'src/organizations/selectors' |
| 20 | + |
| 21 | +let getScripts |
| 22 | + |
| 23 | +if (CLOUD) { |
| 24 | + getScripts = require('src/client/scriptsRoutes').getScripts |
| 25 | +} |
| 26 | + |
| 27 | +interface Props { |
| 28 | + onClose: () => void |
| 29 | +} |
| 30 | + |
| 31 | +const OpenScript: FC<Props> = ({onClose}) => { |
| 32 | + const [scripts, setScripts] = useState([]) |
| 33 | + const [loading, setLoading] = useState(RemoteDataState.NotStarted) |
| 34 | + const [searchTerm, setSearchTerm] = useState('') |
| 35 | + const [selectedScript, setSelectedScript] = useState<any>({}) |
| 36 | + const history = useHistory() |
| 37 | + const org = useSelector(getOrg) |
| 38 | + |
| 39 | + const handleGetScripts = useCallback(async () => { |
| 40 | + try { |
| 41 | + if (getScripts) { |
| 42 | + setLoading(RemoteDataState.Loading) |
| 43 | + const resp = await getScripts({}) |
| 44 | + |
| 45 | + if (resp.status !== 200) { |
| 46 | + throw new Error(resp.data.message) |
| 47 | + } |
| 48 | + |
| 49 | + setScripts(resp.data.scripts) |
| 50 | + setLoading(RemoteDataState.Done) |
| 51 | + } else { |
| 52 | + alert('you are in an supported environment') |
| 53 | + } |
| 54 | + } catch (error) { |
| 55 | + setLoading(RemoteDataState.Error) |
| 56 | + console.error({error}) |
| 57 | + } |
| 58 | + }, [getScripts]) |
| 59 | + |
| 60 | + const handleOpenScript = () => { |
| 61 | + history.replace( |
| 62 | + `/orgs/${org.id}/data-explorer/from/script/${selectedScript.id}` |
| 63 | + ) |
| 64 | + onClose() |
| 65 | + } |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + handleGetScripts() |
| 69 | + }, []) |
| 70 | + |
| 71 | + if ( |
| 72 | + loading === RemoteDataState.NotStarted || |
| 73 | + loading === RemoteDataState.Loading |
| 74 | + ) { |
| 75 | + return ( |
| 76 | + <div className="data-source--list__empty"> |
| 77 | + <TechnoSpinner strokeWidth={ComponentSize.Small} diameterPixels={32} /> |
| 78 | + </div> |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + if (loading === RemoteDataState.Error) { |
| 83 | + return ( |
| 84 | + <div className="data-source--list__empty"> |
| 85 | + <p>Could not get scripts</p> |
| 86 | + </div> |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + if (loading === RemoteDataState.Done) { |
| 91 | + const filteredScripts = scripts.filter(script => |
| 92 | + script.name.includes(searchTerm) |
| 93 | + ) |
| 94 | + |
| 95 | + let list = ( |
| 96 | + <List> |
| 97 | + {filteredScripts.map(script => ( |
| 98 | + <List.Item |
| 99 | + key={script.id} |
| 100 | + value={script.name} |
| 101 | + onClick={() => setSelectedScript(script)} |
| 102 | + selected={script.name === selectedScript?.name} |
| 103 | + title={script} |
| 104 | + wrapText={true} |
| 105 | + > |
| 106 | + {script.name} |
| 107 | + </List.Item> |
| 108 | + ))} |
| 109 | + </List> |
| 110 | + ) |
| 111 | + if (filteredScripts.length === 0 && searchTerm) { |
| 112 | + list = ( |
| 113 | + <EmptyState className="data-source--list__no-results"> |
| 114 | + <p>{`No Scripts match "${searchTerm}"`}</p> |
| 115 | + </EmptyState> |
| 116 | + ) |
| 117 | + } else if (filteredScripts.length === 0 && !searchTerm) { |
| 118 | + list = ( |
| 119 | + <EmptyState className="data-source--list__no-results"> |
| 120 | + <p>No Scripts found</p> |
| 121 | + </EmptyState> |
| 122 | + ) |
| 123 | + } |
| 124 | + return ( |
| 125 | + <Overlay.Container maxWidth={500}> |
| 126 | + <Overlay.Header title="Open Script" onDismiss={onClose} /> |
| 127 | + <Overlay.Body> |
| 128 | + <Input |
| 129 | + className="data-source--search" |
| 130 | + icon={IconFont.Search_New} |
| 131 | + size={ComponentSize.Medium} |
| 132 | + value={searchTerm} |
| 133 | + placeholder="Search Scripts" |
| 134 | + onChange={evt => setSearchTerm(evt.target.value)} |
| 135 | + /> |
| 136 | + <List>{list}</List> |
| 137 | + </Overlay.Body> |
| 138 | + <Overlay.Footer> |
| 139 | + <Button |
| 140 | + color={ComponentColor.Tertiary} |
| 141 | + onClick={onClose} |
| 142 | + text="Cancel" |
| 143 | + /> |
| 144 | + {CLOUD && ( |
| 145 | + <Button |
| 146 | + color={ComponentColor.Primary} |
| 147 | + status={ |
| 148 | + scripts.length === 0 |
| 149 | + ? ComponentStatus.Disabled |
| 150 | + : ComponentStatus.Default |
| 151 | + } |
| 152 | + onClick={handleOpenScript} |
| 153 | + text="Open" |
| 154 | + /> |
| 155 | + )} |
| 156 | + </Overlay.Footer> |
| 157 | + </Overlay.Container> |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + return null |
| 162 | +} |
| 163 | + |
| 164 | +export default OpenScript |
0 commit comments