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

chore: media management logic #885

Merged
merged 5 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 1 addition & 5 deletions src/frontend/hooks/persistedState/usePersistedSettings.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import {type StateCreator} from 'zustand';
import {createPersistedState} from './createPersistedState';
import {CoordinateFormat, MediaSyncSetting} from '../../sharedTypes';
import {CoordinateFormat} from '../../sharedTypes';

type SettingsSlice = {
coordinateFormat: CoordinateFormat;
manualCoordinateEntryFormat: CoordinateFormat;
mediaSyncSetting: MediaSyncSetting;
actions: {
setCoordinateFormat: (coordinateFormat: CoordinateFormat) => void;
setManualCoordinateEntryFormat: (
coordinateFormat: CoordinateFormat,
) => void;
setMediaSyncSetting: (mediaSyncSetting: MediaSyncSetting) => void;
};
};

const settingsSlice: StateCreator<SettingsSlice> = (set, get) => ({
coordinateFormat: 'utm',
manualCoordinateEntryFormat: 'utm',
mediaSyncSetting: 'everything',
actions: {
setCoordinateFormat: coordinateFormat => set({coordinateFormat}),
setManualCoordinateEntryFormat: coordinateFormat =>
set({manualCoordinateEntryFormat: coordinateFormat}),
setMediaSyncSetting: mediaSyncSetting => set({mediaSyncSetting}),
},
});

Expand Down
40 changes: 40 additions & 0 deletions src/frontend/hooks/server/mediaSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query';
import {useApi} from '../../contexts/ApiContext';
import {MediaSyncSetting} from '../../sharedTypes';

export const MEDIA_SYNC_SETTING_KEY = 'media_sync_setting';

export function convertMediaSyncSetting(isArchive: boolean): MediaSyncSetting {
return isArchive ? 'everything' : 'previews';
}

export function isArchiveDevice(value: MediaSyncSetting): boolean {
return value === 'everything';
}

export function useGetMediaSyncSetting() {
const api = useApi();

return useQuery({
cimigree marked this conversation as resolved.
Show resolved Hide resolved
queryKey: [MEDIA_SYNC_SETTING_KEY],
queryFn: async () => {
const isArchive = await api.getIsArchiveDevice();

Check failure on line 21 in src/frontend/hooks/server/mediaSync.ts

View workflow job for this annotation

GitHub Actions / frontend

Property 'getIsArchiveDevice' does not exist on type 'MapeoClientApi'.
return convertMediaSyncSetting(isArchive);
},
});
}

export function useSetMediaSyncSetting() {
const api = useApi();
const queryClient = useQueryClient();

return useMutation({
mutationFn: async (newSetting: MediaSyncSetting) => {
const isArchive = isArchiveDevice(newSetting);
await api.setIsArchiveDevice(isArchive);

Check failure on line 34 in src/frontend/hooks/server/mediaSync.ts

View workflow job for this annotation

GitHub Actions / frontend

Property 'setIsArchiveDevice' does not exist on type 'MapeoClientApi'.
},
onSuccess: () => {
queryClient.invalidateQueries({queryKey: [MEDIA_SYNC_SETTING_KEY]});
},
});
}
33 changes: 17 additions & 16 deletions src/frontend/screens/Settings/ProjectSettings/MediaSyncSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import {ScrollView, StyleSheet} from 'react-native';
import {useIntl, defineMessages} from 'react-intl';
import {SelectOne} from '../../../sharedComponents/SelectOne';
import {SYNC_BACKGROUND} from '../../../lib/styles';
import {
usePersistedSettings,
usePersistedSettingsAction,
} from '../../../hooks/persistedState/usePersistedSettings';
import {MediaSyncActionSheetContent} from './MediaSyncActionSheetContent';
import {
useBottomSheetModal,
BottomSheetModal,
} from '../../../sharedComponents/BottomSheetModal';
import {MediaSyncSetting} from '../../../sharedTypes';
import {
useGetMediaSyncSetting,
useSetMediaSyncSetting,
} from '../../../hooks/server/mediaSync';

const m = defineMessages({
syncSettingsTitle: {
Expand Down Expand Up @@ -71,14 +71,17 @@ const m = defineMessages({

export const MediaSyncSettings = () => {
const {formatMessage: t} = useIntl();
const mediaSyncSetting = usePersistedSettings(
store => store.mediaSyncSetting,
);
const {setMediaSyncSetting} = usePersistedSettingsAction();
const {data: mediaSyncSetting, isLoading} = useGetMediaSyncSetting();
const {mutate: setMediaSyncSetting} = useSetMediaSyncSetting();

const [modalType, setModalType] = React.useState<MediaSyncSetting>(
() => mediaSyncSetting,
);
const [modalType, setModalType] =
React.useState<MediaSyncSetting>('everything');

React.useEffect(() => {
if (!isLoading && mediaSyncSetting) {
setModalType(mediaSyncSetting);
}
}, [mediaSyncSetting]);
cimigree marked this conversation as resolved.
Show resolved Hide resolved

const {isOpen, openSheet, closeSheet, sheetRef} = useBottomSheetModal({
openOnMount: false,
Expand All @@ -97,7 +100,7 @@ export const MediaSyncSettings = () => {
const options: {
value: MediaSyncSetting;
label: string;
hint?: React.ReactNode;
hint: React.ReactNode;
}[] = [
{
value: 'previews',
Expand All @@ -120,7 +123,7 @@ export const MediaSyncSettings = () => {
return (
<ScrollView style={styles.container}>
<SelectOne
value={mediaSyncSetting}
value={modalType}
cimigree marked this conversation as resolved.
Show resolved Hide resolved
onChange={handleOptionChange}
options={options}
radioButtonPosition="right"
Expand Down Expand Up @@ -150,9 +153,7 @@ export const MediaSyncSettings = () => {
: t(m.syncEverything)
}
confirmAction={handleConfirm}
onDismiss={() => {
closeSheet();
}}
onDismiss={closeSheet}
/>
</BottomSheetModal>
</ScrollView>
Expand Down
18 changes: 7 additions & 11 deletions src/frontend/screens/Settings/ProjectSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,13 @@ export const ProjectSettings: NativeNavigationComponent<'ProjectSettings'> = ({
},
]
: []),
...(process.env.EXPO_PUBLIC_FEATURE_MEDIA_MANAGER
? [
{
onPress: () => {
navigation.navigate('MediaSyncSettings');
},
primaryText: formatMessage(m.mediaSyncSettings),
testID: 'AIN.sync-list-item',
},
]
: []),
{
onPress: () => {
navigation.navigate('MediaSyncSettings');
},
primaryText: formatMessage(m.mediaSyncSettings),
testID: 'MAIN.sync-list-item',
},
];

return <FullScreenMenuList data={MenuItems} />;
Expand Down
Loading