1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/app-mobile/components/NoteList.tsx

127 lines
3.6 KiB
TypeScript
Raw Normal View History

2019-07-29 15:43:53 +02:00
const React = require('react');
import { Component } from 'react';
import { connect } from 'react-redux';
import { FlatList, Text, StyleSheet, Button, View } from 'react-native';
import { FolderEntity, NoteEntity } from '@joplin/lib/services/database/types';
import { AppState } from '../utils/types';
import getEmptyFolderMessage from '@joplin/lib/components/shared/NoteList/getEmptyFolderMessage';
import Folder from '@joplin/lib/models/Folder';
const { _ } = require('@joplin/lib/locale');
import NoteItem from './NoteItem';
import { themeStyle } from './global-style';
2017-07-25 19:49:31 +02:00
interface NoteListProps {
themeId: number;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
dispatch: (action: any)=> void;
notesSource: string;
items: NoteEntity[];
folders: FolderEntity[];
noteSelectionEnabled?: boolean;
selectedFolderId: string|null;
}
class NoteListComponent extends Component<NoteListProps> {
private rootRef_: FlatList;
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
private styles_: Record<string, StyleSheet.NamedStyles<any>>;
public constructor(props: NoteListProps) {
super(props);
2020-02-09 16:51:12 +02:00
2017-07-25 19:49:31 +02:00
this.state = {
items: [],
selectedItemIds: [],
};
2017-07-30 23:33:54 +02:00
this.rootRef_ = null;
2017-08-01 20:29:01 +02:00
this.styles_ = {};
this.createNotebookButton_click = this.createNotebookButton_click.bind(this);
2017-08-01 20:29:01 +02:00
}
private styles() {
2020-09-15 15:01:07 +02:00
const themeId = this.props.themeId;
2017-08-01 20:29:01 +02:00
const theme = themeStyle(themeId);
if (this.styles_[themeId]) return this.styles_[themeId];
this.styles_ = {};
const styles = {
2017-08-01 20:29:01 +02:00
noItemMessage: {
paddingLeft: theme.marginLeft,
paddingRight: theme.marginRight,
paddingTop: theme.marginTop,
paddingBottom: theme.marginBottom,
fontSize: theme.fontSize,
color: theme.color,
textAlign: 'center',
},
noNotebookView: {
2017-08-01 20:29:01 +02:00
},
};
this.styles_[themeId] = StyleSheet.create(styles);
return this.styles_[themeId];
2017-07-25 19:49:31 +02:00
}
private createNotebookButton_click() {
this.props.dispatch({
type: 'NAV_GO',
routeName: 'Folder',
folderId: null,
});
}
public UNSAFE_componentWillReceiveProps(newProps: NoteListProps) {
2017-07-30 23:33:54 +02:00
// Make sure scroll position is reset when switching from one folder to another or to a tag list.
if (this.rootRef_ && newProps.notesSource !== this.props.notesSource) {
2020-02-09 16:51:12 +02:00
this.rootRef_.scrollToOffset({ offset: 0, animated: false });
2017-07-30 23:33:54 +02:00
}
2017-07-25 19:49:31 +02:00
}
public render() {
2017-07-25 19:49:31 +02:00
// `enableEmptySections` is to fix this warning: https://github.com/FaridSafi/react-native-gifted-listview/issues/39
2020-02-09 16:51:12 +02:00
if (this.props.items.length) {
return <FlatList
ref={ref => (this.rootRef_ = ref)}
2020-02-09 16:51:12 +02:00
data={this.props.items}
renderItem={({ item }) => <NoteItem note={item} />}
keyExtractor={item => item.id}
2020-02-09 16:51:12 +02:00
/>;
2017-07-25 19:49:31 +02:00
} else {
if (!Folder.atLeastOneRealFolderExists(this.props.folders)) {
const noItemMessage = _('You currently have no notebooks.');
return (
<View style={this.styles().noNotebookView}>
<Text style={this.styles().noItemMessage}>{noItemMessage}</Text>
<Button title={_('Create a notebook')} onPress={this.createNotebookButton_click} />
</View>
);
} else {
return <Text style={this.styles().noItemMessage}>
{getEmptyFolderMessage(this.props.folders, this.props.selectedFolderId)}
</Text>;
}
2017-07-25 19:49:31 +02:00
}
}
2017-05-15 21:46:34 +02:00
}
2017-05-15 21:10:00 +02:00
const NoteList = connect((state: AppState) => {
2019-07-29 15:43:53 +02:00
return {
items: state.notes,
folders: state.folders,
2019-07-29 15:43:53 +02:00
notesSource: state.notesSource,
2020-09-15 15:01:07 +02:00
themeId: state.settings.theme,
2019-07-29 15:43:53 +02:00
noteSelectionEnabled: state.noteSelectionEnabled,
selectedFolderId: state.selectedFolderId,
2019-07-29 15:43:53 +02:00
};
})(NoteListComponent);
2017-05-15 21:10:00 +02:00
export default NoteList;