<commit_before>// @flow

import invariant from 'invariant';
import * as React from 'react';
import { Button, View, Image } from 'react-native';
import filesystem from 'react-native-fs';

import type { PhotoPaste } from 'lib/types/media-types';
import sleep from 'lib/utils/sleep';

import Modal from '../components/modal.react';
import { InputStateContext } from '../input/input-state';
import type { RootNavigationProp } from '../navigation/root-navigator.react';
import type { NavigationRoute } from '../navigation/route-names';
import { useStyles } from '../themes/colors';

export type ImagePasteModalParams = {|
  +imagePasteStagingInfo: PhotoPaste,
  +threadID: string,
|};

type Props = {|
  +navigation: RootNavigationProp<'ImagePasteModal'>,
  +route: NavigationRoute<'ImagePasteModal'>,
|};
function ImagePasteModal(props: Props) {
  const styles = useStyles(unboundStyles);
  const inputState = React.useContext(InputStateContext);
  const {
    navigation,
    route: {
      params: { imagePasteStagingInfo, threadID },
    },
  } = props;

  const sendImage = React.useCallback(async () => {
    navigation.goBackOnce();
    const selection: $ReadOnlyArray<PhotoPaste> = [imagePasteStagingInfo];
    invariant(inputState, 'inputState should be set in ImagePasteModal');
    await inputState.sendMultimediaMessage(threadID, selection);
    invariant(
      imagePasteStagingInfo,
      'imagePasteStagingInfo should be set in ImagePasteModal',
    );
  }, [imagePasteStagingInfo, inputState, navigation, threadID]);

  const cancel = React.useCallback(async () => {
    navigation.goBackOnce();
    await sleep(5000);
    filesystem.unlink(imagePasteStagingInfo.uri);
  }, [imagePasteStagingInfo.uri, navigation]);

  return (
    <Modal
      modalStyle={styles.modal}
      navigation={navigation}
      safeAreaEdges={['top']}
    >
      <Image style={styles.image} source={{ uri: imagePasteStagingInfo.uri }} />
      <View style={styles.linebreak}></View>
      <View style={styles.spacer}></View>
      <Button title="Send" onPress={sendImage} />
      <View style={styles.linebreak}></View>
      <View style={styles.spacer}></View>
      <Button title="Cancel" onPress={cancel} />
      <View style={styles.spacer}></View>
    </Modal>
  );
}

const unboundStyles = {
  modal: {
    backgroundColor: 'modalBackground',
    flex: 1,
    justifyContent: 'center',
    marginHorizontal: 0,
    marginTop: 300,
    marginBottom: 0,
    padding: 12,
    borderTopLeftRadius: 10,
    borderTopRightRadius: 10,
    borderRadius: 0,
  },
  modalBackground: {
    backgroundColor: 'modalBackground',
    padding: 12,
    justifyContent: 'center',
    alignItems: 'center',
    borderRadius: 5,
  },
  image: {
    resizeMode: 'contain',
    flex: 1,
    justifyContent: 'center',
    marginBottom: 15,
    marginHorizontal: 15,
    marginTop: 20,<commit_msg>Image paste modal cleanup<commit_after>// @flow

import invariant from 'invariant';
import * as React from 'react';
import { Button, View, Image } from 'react-native';
import filesystem from 'react-native-fs';

import type { PhotoPaste } from 'lib/types/media-types';
import sleep from 'lib/utils/sleep';

import Modal from '../components/modal.react';
import { InputStateContext } from '../input/input-state';
import type { RootNavigationProp } from '../navigation/root-navigator.react';
import type { NavigationRoute } from '../navigation/route-names';
import { useStyles } from '../themes/colors';

export type ImagePasteModalParams = {|
  +imagePasteStagingInfo: PhotoPaste,
  +threadID: string,
|};

type Props = {|
  +navigation: RootNavigationProp<'ImagePasteModal'>,
  +route: NavigationRoute<'ImagePasteModal'>,
|};
function ImagePasteModal(props: Props) {
  const styles = useStyles(unboundStyles);
  const inputState = React.useContext(InputStateContext);
  const {
    navigation,
    route: {
      params: { imagePasteStagingInfo, threadID },
    },
  } = props;

  const sendImage = React.useCallback(async () => {
    navigation.goBackOnce();
    const selection: $ReadOnlyArray<PhotoPaste> = [imagePasteStagingInfo];
    invariant(inputState, 'inputState should be set in ImagePasteModal');
    await inputState.sendMultimediaMessage(threadID, selection);
    invariant(
      imagePasteStagingInfo,
      'imagePasteStagingInfo should be set in ImagePasteModal',
    );
  }, [imagePasteStagingInfo, inputState, navigation, threadID]);

  const cancel = React.useCallback(async () => {
    navigation.goBackOnce();
    await sleep(5000);
    filesystem.unlink(imagePasteStagingInfo.uri);
  }, [imagePasteStagingInfo.uri, navigation]);

  return (
    <Modal
      modalStyle={styles.modal}
      navigation={navigation}
      safeAreaEdges={['top']}
    >
      <Image style={styles.image} source={{ uri: imagePasteStagingInfo.uri }} />
      <View style={styles.linebreak} />
      <View style={styles.spacer} />
      <Button title="Send" onPress={sendImage} />
      <View style={styles.linebreak} />
      <View style={styles.spacer} />
      <Button title="Cancel" onPress={cancel} />
      <View style={styles.spacer} />
    </Modal>
  );
}

const unboundStyles = {
  modal: {
    marginHorizontal: 0,
    marginTop: 300,
    marginBottom: 0,
    borderTopLeftRadius: 10,
    borderTopRightRadius: 10,
    borderRadius: 0,
  },
  image: {
    resizeMode: 'contain',
    flex: 1,
    justifyContent: 'center',
    marginBottom: 15,
    marginHorizontal: 15,
    marginTop: 20,
{'fraction_non_alphanumeric': 0.13457670506491132, 'fraction_numerical': 0.00877674163466813, 'mean_word_length': 3.892665474060823, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4213804', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1667, 'n_words': 371}