import React, { RefObject } from 'react'; import { FlashList, MasonryFlashList } from '@shopify/flash-list'; import { NativeSyntheticEvent, NativeScrollEvent, StyleSheet, } from 'react-native'; import { useSelector } from 'react-redux'; import { Divider, HelperText } from 'react-native-paper'; import { useSafeAreaFrame } from 'react-native-safe-area-context'; import { Meme } from '../../../database'; import { RootState } from '../../../state'; import { VIEW } from '../../../types'; import { getFlashListItemHeight } from '../../../utilities'; import MemesMasonryItem from './memesMasonryItem'; import MemesGridItem from './memesGridItem'; import MemesListItem from './memesListItem'; import { AndroidScoped } from 'react-native-file-access'; const sharedMemesListStyles = StyleSheet.create({ flashList: { paddingBottom: 100, }, helperText: { textAlign: 'center', marginVertical: 15, }, }); const memesMasonryListStyles = StyleSheet.create({ flashList: { // Needed to prevent fucky MasonryFlashList, see https://github.com/Shopify/flash-list/issues/876 paddingHorizontal: 0.1, }, }); const memesGridListStyles = StyleSheet.create({ flashList: { paddingHorizontal: 2.5, }, }); const memesListListStyles = StyleSheet.create({ flashList: { paddingHorizontal: 5, }, }); const MemesList = ({ memes, flashListRef, flashListPadding, handleScroll, focusMeme, }: { memes: Realm.Results>; flashListRef: RefObject>; flashListPadding: number; handleScroll: (event: NativeSyntheticEvent) => void; focusMeme: (index: number) => void; }) => { const { height, width } = useSafeAreaFrame(); const view = useSelector((state: RootState) => state.memes.view); const masonryColumns = useSelector( (state: RootState) => state.settings.masonryColumns, ); const gridColumns = useSelector( (state: RootState) => state.settings.gridColumns, ); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const storageUri = useSelector( (state: RootState) => state.settings.storageUri, )!; return ( <> {view === VIEW.MASONRY && ( ( focusMeme(index)} uri={AndroidScoped.appendPath(storageUri, meme.filename)} columns={masonryColumns} /> )} contentContainerStyle={{ paddingTop: flashListPadding, ...sharedMemesListStyles.flashList, ...memesMasonryListStyles.flashList, }} ListEmptyComponent={() => ( No memes found )} onScroll={handleScroll} fadingEdgeLength={100} overScrollMode="never" /> )} {view === VIEW.GRID && ( ( focusMeme(index)} uri={AndroidScoped.appendPath(storageUri, meme.filename)} columns={gridColumns} /> )} contentContainerStyle={{ paddingTop: flashListPadding, ...sharedMemesListStyles.flashList, ...memesGridListStyles.flashList, }} ListEmptyComponent={() => ( No memes found )} onScroll={handleScroll} fadingEdgeLength={100} overScrollMode="never" /> )} {view === VIEW.LIST && ( ( focusMeme(index)} uri={AndroidScoped.appendPath(storageUri, meme.filename)} /> )} ItemSeparatorComponent={() => } contentContainerStyle={{ paddingTop: flashListPadding, ...sharedMemesListStyles.flashList, ...memesListListStyles.flashList, }} ListEmptyComponent={() => ( No memes found )} onScroll={handleScroll} fadingEdgeLength={100} overScrollMode="never" /> )} ); }; export default MemesList;