Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: share button sometimes not working after first share #847

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 37 additions & 31 deletions src/frontend/screens/Observation/Buttons.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useState} from 'react';
import {Alert, StyleSheet, TouchableOpacity, View} from 'react-native';
import {Field, Observation} from '@comapeo/schema';
import {DARK_GREY} from '../../lib/styles';
Expand All @@ -8,16 +8,15 @@ import {useNavigationFromRoot} from '../../hooks/useNavigationWithTypes';
import {useDeleteObservation} from '../../hooks/server/observations';
import {Text} from '../../sharedComponents/Text';
import Share from 'react-native-share';
import {useAttachmentUrlQueries} from '../../hooks/server/media.ts';
import {useObservationWithPreset} from '../../hooks/useObservationWithPreset.ts';
import {formatCoords} from '../../lib/utils.ts';
import {UIActivityIndicator} from 'react-native-indicators';
import {convertUrlToBase64} from '../../utils/base64.ts';
import {useState} from 'react';
import {usePersistedSettings} from '../../hooks/persistedState/usePersistedSettings.ts';
import * as Sentry from '@sentry/react-native';
import {CoordinateFormat} from '../../sharedTypes/index.ts';
import {getValueLabel} from '../../sharedComponents/FormattedData.tsx';
import {useActiveProject} from '../../contexts/ActiveProjectContext';

const m = defineMessages({
delete: {
Expand Down Expand Up @@ -95,11 +94,8 @@ export const ButtonFields = ({
const deleteObservationMutation = useDeleteObservation();
const {observation, preset} = useObservationWithPreset(observationId);
const format = usePersistedSettings(store => store.coordinateFormat);
const attachmentUrlQueries = useAttachmentUrlQueries(
observation.attachments,
'original',
);
const [isShareButtonLoading, setShareButtonLoading] = useState(false);
const {projectApi} = useActiveProject();

function handlePressDelete() {
Alert.alert(t(m.deleteTitle), undefined, [
Expand All @@ -117,36 +113,47 @@ export const ButtonFields = ({
]);
}

async function handlePressShare() {
async function fetchFreshUrls() {
const {attachments} = observation;
setShareButtonLoading(true);
const getValidUrls = (queries: typeof attachmentUrlQueries) => {
const urls = queries
.map(query => query.data?.url)
.filter((url): url is string => url !== undefined && url !== null);

return urls;
};
if (!attachments || attachments.length === 0) {
return [];
}

let urls: string[] = [];
const urls = await Promise.all(
attachments.map(async attachment => {
if (attachment.type !== 'photo' && attachment.type !== 'audio') {
return [];
}

if (attachments.length > 0) {
urls = getValidUrls(attachmentUrlQueries);
try {
const url = await projectApi.$blobs.getUrl({
driveId: attachment.driveDiscoveryId,
name: attachment.name,
type: attachment.type,
variant: 'original',
});
return url;
} catch (error) {
return null;
}
}),
);

if (urls.length === 0) {
setShareButtonLoading(false);
Alert.alert('Error', 'Unable to share this observation.');
return;
}
}
return urls.filter((url): url is string => url !== null);
}

async function handlePressShare() {
setShareButtonLoading(true);

try {
const base64Urls = await Promise.all(
urls.map(url => convertUrlToBase64(url)),
);
const urls = await fetchFreshUrls();
const base64Urls =
urls.length > 0
? await Promise.all(urls.map(url => convertUrlToBase64(url)))
: [];

const completedFields: Array<{label: string; value: string}> = [];

for (const field of fields) {
const value = observation.tags[field.tagKey];

Expand All @@ -155,9 +162,7 @@ export const ButtonFields = ({
}

const displayedValue = (Array.isArray(value) ? value : [value])
.map(v => {
return getValueLabel(v, field).trim();
})
.map(v => getValueLabel(v, field).trim())
.join(', ');

completedFields.push({label: field.label, value: displayedValue});
Expand All @@ -177,6 +182,7 @@ export const ButtonFields = ({
timestamp: formatDate(observation.createdAt, {format: 'long'}),
titleText: t(m.shareMessageTitle),
}),
failOnCancel: false,
});
} catch (err) {
Sentry.captureException(err);
Expand Down
Loading