1import * as Types from '../../../../../constants/types/chat2'
2import * as Constants from '../../../../../constants/chat2'
3import * as CryptoTypes from '../../../../../constants/types/crypto'
4import * as Tabs from '../../../../../constants/tabs'
5import * as FsGen from '../../../../../actions/fs-gen'
6import * as Chat2Gen from '../../../../../actions/chat2-gen'
7import * as RouteTreeGen from '../../../../../actions/route-tree-gen'
8import * as CryptoGen from '../../../../../actions/crypto-gen'
9import * as Container from '../../../../../util/container'
10import {globalColors} from '../../../../../styles'
11import {isPathSaltpack} from '../../../../../constants/crypto'
12import File from '.'
13
14type OwnProps = {
15  isHighlighted?: boolean
16  message: Types.MessageAttachment
17}
18
19export default Container.connect(
20  (state: Container.TypedState, ownProps: OwnProps) => {
21    const editInfo = Constants.getEditInfo(state, ownProps.message.conversationIDKey)
22    const isEditing = !!(editInfo && editInfo.ordinal === ownProps.message.ordinal)
23    return {isEditing}
24  },
25  dispatch => ({
26    _onDownload: (message: Types.MessageAttachment) => {
27      switch (message.transferState) {
28        case 'uploading':
29        case 'downloading':
30        case 'mobileSaving':
31          return
32      }
33      dispatch(
34        Chat2Gen.createAttachmentDownload({
35          message,
36        })
37      )
38    },
39    _onSaltpackFileOpen: (path: string, operation: CryptoTypes.Operations) => {
40      dispatch(RouteTreeGen.createSwitchTab({tab: Tabs.cryptoTab}))
41      dispatch(
42        CryptoGen.createOnSaltpackOpenFile({
43          operation,
44          path: new Container.HiddenString(path),
45        })
46      )
47    },
48    _onShare: (message: Types.MessageAttachment) => {
49      dispatch(Chat2Gen.createMessageAttachmentNativeShare({message}))
50    },
51    _onShowInFinder: (message: Types.MessageAttachment) => {
52      message.downloadPath &&
53        dispatch(FsGen.createOpenLocalPathInSystemFileManager({localPath: message.downloadPath}))
54    },
55  }),
56  (stateProps, dispatchProps, ownProps: OwnProps) => {
57    const message = ownProps.message
58    const {downloadPath, transferState} = message
59    const arrowColor = Container.isMobile
60      ? ''
61      : downloadPath
62      ? globalColors.green
63      : transferState === 'downloading'
64      ? globalColors.blue
65      : ''
66    const hasProgress =
67      !!transferState && transferState !== 'remoteUploading' && transferState !== 'mobileSaving'
68    return {
69      arrowColor,
70      errorMsg: message.transferErrMsg || '',
71      fileName: message.fileName,
72      hasProgress,
73      isEditing: stateProps.isEditing,
74      isHighlighted: ownProps.isHighlighted,
75      isSaltpackFile: isPathSaltpack(message.fileName),
76      message,
77      onDownload: () => {
78        if (Container.isMobile) {
79          dispatchProps._onShare(message)
80        } else {
81          if (!message.downloadPath) {
82            dispatchProps._onDownload(message)
83          }
84        }
85      },
86      onSaltpackFileOpen: dispatchProps._onSaltpackFileOpen,
87      onShowInFinder:
88        !Container.isMobile && message.downloadPath
89          ? () => dispatchProps._onShowInFinder(message)
90          : undefined,
91      progress: message.transferProgress,
92      title: message.decoratedText?.stringValue() || message.title || message.fileName,
93      transferState,
94    }
95  }
96)(File)
97