|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import styled from 'styled-components'; |
| 3 | +import { Button } from '@trezor/components'; |
| 4 | + |
| 5 | +import { TextColumn, ActionColumn } from 'src/components/suite'; |
| 6 | +import { useDevice, usePasswords } from 'src/hooks/suite'; |
| 7 | +import { getNextId } from 'src/utils/suite/passwords'; |
| 8 | + |
| 9 | +import { EntryForm } from './EntryForm'; |
| 10 | +import { PasswordsList } from './PasswordsList'; |
| 11 | +import { TagsList } from './TagsList'; |
| 12 | + |
| 13 | +const PasswordManagerBody = styled.div` |
| 14 | + display: flex; |
| 15 | + flex: 1; |
| 16 | + flex-direction: column; |
| 17 | +`; |
| 18 | + |
| 19 | +const Section = styled.div` |
| 20 | + display: flex; |
| 21 | +`; |
| 22 | + |
| 23 | +export const PasswordManager = () => { |
| 24 | + const { |
| 25 | + entries, |
| 26 | + tags, |
| 27 | + entriesByTag, |
| 28 | + isSomeTagSelected, |
| 29 | + config, |
| 30 | + fileName, |
| 31 | + selectedTags, |
| 32 | + setSelectedTags, |
| 33 | + connect, |
| 34 | + disconnect, |
| 35 | + selectedProvider, |
| 36 | + providerConnecting, |
| 37 | + savePasswords, |
| 38 | + device, |
| 39 | + } = usePasswords(); |
| 40 | + |
| 41 | + const { isLocked } = useDevice(); |
| 42 | + const isDeviceLocked = isLocked(); |
| 43 | + |
| 44 | + const [formActive, setFormActive] = useState<null | number>(null); |
| 45 | + |
| 46 | + if (!device?.state) { |
| 47 | + return <Section>Connect and authorize device</Section>; |
| 48 | + } |
| 49 | + if (providerConnecting) { |
| 50 | + return <Section>Connecting...</Section>; |
| 51 | + } |
| 52 | + |
| 53 | + if (!selectedProvider || !fileName) { |
| 54 | + return ( |
| 55 | + <Section> |
| 56 | + <TextColumn |
| 57 | + title="Trezor password manager" |
| 58 | + description="Re-implementation of former Trezor Password Manager webextension" |
| 59 | + /> |
| 60 | + <ActionColumn> |
| 61 | + <Button onClick={connect} isDisabled={isDeviceLocked}> |
| 62 | + Connect to Dropbox |
| 63 | + </Button> |
| 64 | + {/* TODO: connect to drive */} |
| 65 | + </ActionColumn> |
| 66 | + </Section> |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return ( |
| 71 | + <> |
| 72 | + <Section> |
| 73 | + <TextColumn |
| 74 | + title="Provider details" |
| 75 | + description={`type: ${selectedProvider.type}, clientId: ${selectedProvider.clientId}, connected user: ${selectedProvider.user}`} |
| 76 | + /> |
| 77 | + <ActionColumn> |
| 78 | + <Button onClick={disconnect}>Disconnect</Button> |
| 79 | + </ActionColumn> |
| 80 | + </Section> |
| 81 | + <Section> |
| 82 | + {config ? ( |
| 83 | + <PasswordManagerBody> |
| 84 | + <TagsList |
| 85 | + tags={tags} |
| 86 | + selectedTags={selectedTags} |
| 87 | + setSelectedTags={setSelectedTags} |
| 88 | + /> |
| 89 | + |
| 90 | + <PasswordsList |
| 91 | + isSomeTagSelected={isSomeTagSelected} |
| 92 | + formActive={formActive} |
| 93 | + entriesByTag={entriesByTag} |
| 94 | + entries={entries} |
| 95 | + savePasswords={savePasswords} |
| 96 | + setFormActive={setFormActive} |
| 97 | + fileName={fileName} |
| 98 | + nextId={getNextId(entries)} |
| 99 | + /> |
| 100 | + </PasswordManagerBody> |
| 101 | + ) : ( |
| 102 | + <div style={{ display: 'flex', justifyContent: 'center', flex: 1 }}> |
| 103 | + {formActive !== 0 && ( |
| 104 | + <> |
| 105 | + <div>There are no passwords yet </div> |
| 106 | + <Button |
| 107 | + size="tiny" |
| 108 | + onClick={() => setFormActive(0)} |
| 109 | + type="button" |
| 110 | + variant="tertiary" |
| 111 | + icon="PENCIL" |
| 112 | + > |
| 113 | + Add the first one! |
| 114 | + </Button> |
| 115 | + </> |
| 116 | + )} |
| 117 | + {formActive === 0 && ( |
| 118 | + <EntryForm |
| 119 | + cancel={() => setFormActive(null)} |
| 120 | + onEncrypted={entry => { |
| 121 | + savePasswords(getNextId(entries), entry); |
| 122 | + setFormActive(null); |
| 123 | + }} |
| 124 | + /> |
| 125 | + )} |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </Section> |
| 129 | + </> |
| 130 | + ); |
| 131 | +}; |
0 commit comments