|
| 1 | +import styled from 'styled-components'; |
| 2 | +import { useCallback, useMemo, useState } from 'react'; |
| 3 | +import { Button, SettingsLabel, Space, Spinner, TextInput } from '../common'; |
| 4 | +import { WalltakerService } from './WalltakerService'; |
| 5 | +import { WalltakerCredentials } from './WalltakerProvider'; |
| 6 | + |
| 7 | +export interface WalltakerCredentialsInputProps { |
| 8 | + service: WalltakerService; |
| 9 | + initialValue?: Partial<WalltakerCredentials>; |
| 10 | + onSaved?: (credentials: WalltakerCredentials) => void; |
| 11 | + disabled?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +const StyledE621CredentialsInput = styled.div` |
| 15 | + grid-column: 1 / -1; |
| 16 | + display: grid; |
| 17 | + grid-template-columns: auto 1fr auto; |
| 18 | +`; |
| 19 | + |
| 20 | +const StyledE621SaveCredentials = styled.div` |
| 21 | + grid-column: 1 / -1; |
| 22 | + display: flex; |
| 23 | + justify-content: flex-end; |
| 24 | + align-items: center; |
| 25 | +`; |
| 26 | + |
| 27 | +export const WalltakerCredentialsInput = ({ |
| 28 | + service, |
| 29 | + initialValue, |
| 30 | + onSaved, |
| 31 | + disabled, |
| 32 | +}: WalltakerCredentialsInputProps) => { |
| 33 | + const [input, setInput] = useState<Partial<WalltakerCredentials>>({ |
| 34 | + ...initialValue, |
| 35 | + username: '', |
| 36 | + apiKey: '', |
| 37 | + }); |
| 38 | + const [loading, setLoading] = useState(false); |
| 39 | + |
| 40 | + const onSave = useCallback(async () => { |
| 41 | + const credentials = input as WalltakerCredentials; |
| 42 | + setLoading(true); |
| 43 | + const valid = await service.testCredentials(credentials); |
| 44 | + if (valid) { |
| 45 | + onSaved?.(credentials); |
| 46 | + } |
| 47 | + }, [input, onSaved, service]); |
| 48 | + |
| 49 | + const hasData = useMemo( |
| 50 | + () => input?.username && input?.apiKey, |
| 51 | + [input?.username, input?.apiKey] |
| 52 | + ); |
| 53 | + |
| 54 | + const locked = useMemo(() => loading || disabled, [loading, disabled]); |
| 55 | + |
| 56 | + return ( |
| 57 | + <StyledE621CredentialsInput> |
| 58 | + <Space size='small' /> |
| 59 | + <SettingsLabel>Username</SettingsLabel> |
| 60 | + <TextInput |
| 61 | + name='walltaker-username' |
| 62 | + style={{ gridColumn: '2 / -1' }} |
| 63 | + value={input?.username} |
| 64 | + onChange={username => |
| 65 | + setInput({ |
| 66 | + ...input, |
| 67 | + username, |
| 68 | + }) |
| 69 | + } |
| 70 | + placeholder='Username' |
| 71 | + autoComplete='username' |
| 72 | + disabled={locked} |
| 73 | + /> |
| 74 | + <Space size='small' /> |
| 75 | + <SettingsLabel>API Key</SettingsLabel> |
| 76 | + <TextInput |
| 77 | + name='walltaker-api-key' |
| 78 | + style={{ gridColumn: '2 / -1' }} |
| 79 | + value={input?.apiKey} |
| 80 | + onChange={apiKey => |
| 81 | + setInput({ |
| 82 | + ...input, |
| 83 | + apiKey, |
| 84 | + }) |
| 85 | + } |
| 86 | + placeholder='API Key' |
| 87 | + type='password' |
| 88 | + autoComplete='current-password' |
| 89 | + disabled={locked} |
| 90 | + /> |
| 91 | + <Space size='small' /> |
| 92 | + <StyledE621SaveCredentials> |
| 93 | + {loading && <Spinner />} |
| 94 | + <Space size='small' /> |
| 95 | + <Button |
| 96 | + onClick={!loading ? onSave : undefined} |
| 97 | + disabled={!hasData || locked} |
| 98 | + > |
| 99 | + Save |
| 100 | + </Button> |
| 101 | + </StyledE621SaveCredentials> |
| 102 | + </StyledE621CredentialsInput> |
| 103 | + ); |
| 104 | +}; |
0 commit comments