Files
notesnook/apps/mobile/src/components/SimpleList/sectionheader.js

136 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-07-13 14:10:51 +05:00
import React, {useEffect, useState} from 'react';
2021-07-10 14:24:08 +05:00
import {TouchableOpacity, useWindowDimensions, View} from 'react-native';
import {useTracked} from '../../provider';
2021-07-17 20:40:49 +05:00
import {useSettingStore} from '../../provider/stores';
2021-07-13 14:10:51 +05:00
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
} from '../../services/EventManager';
2021-07-17 20:40:49 +05:00
import SettingsService from '../../services/SettingsService';
2021-07-13 14:10:51 +05:00
import {SORT} from '../../utils';
2021-07-17 20:40:49 +05:00
import {COLORS_NOTE} from '../../utils/Colors';
2021-07-13 14:10:51 +05:00
import {db} from '../../utils/DB';
import {eOpenJumpToDialog, eOpenSortDialog} from '../../utils/Events';
2021-07-10 14:24:08 +05:00
import {SIZE} from '../../utils/SizeUtils';
2021-07-17 20:40:49 +05:00
import {ActionIcon} from '../ActionIcon';
2021-07-13 14:10:51 +05:00
import {Button} from '../Button';
2021-02-09 11:33:57 +05:00
import Heading from '../Typography/Heading';
2021-07-17 20:40:49 +05:00
export const SectionHeader = ({item, index, type, title}) => {
2021-06-08 12:26:55 +05:00
const [state] = useTracked();
const {colors} = state;
const {fontScale} = useWindowDimensions();
2021-07-13 14:10:51 +05:00
const [groupOptions, setGroupOptions] = useState(
db.settings?.getGroupOptions(type),
);
let groupBy = Object.keys(SORT).find(
key => SORT[key] === groupOptions.groupBy,
);
2021-07-17 20:40:49 +05:00
const color = COLORS_NOTE[title.toLowerCase()] || colors.accent;
const settings = useSettingStore(state => state.settings);
const listMode =
type === 'notebooks' ? settings.notebooksListMode : settings.notesListMode;
2021-07-13 14:10:51 +05:00
groupBy = !groupBy
? 'Default'
: groupBy.slice(0, 1).toUpperCase() + groupBy.slice(1, groupBy.length);
const onUpdate = () => {
setGroupOptions({...db.settings?.getGroupOptions(type)});
};
2021-07-17 20:40:49 +05:00
2021-07-13 14:10:51 +05:00
useEffect(() => {
eSubscribeEvent('groupOptionsUpdate', onUpdate);
return () => {
eUnSubscribeEvent('groupOptionsUpdate', onUpdate);
};
}, []);
2021-06-08 12:26:55 +05:00
return (
<View
style={{
flexDirection: 'row',
alignItems: 'center',
2021-07-17 20:40:49 +05:00
width: '95%',
2021-06-08 12:26:55 +05:00
justifyContent: 'space-between',
paddingHorizontal: 12,
2021-07-09 12:15:06 +05:00
height: 35 * fontScale,
2021-07-17 20:40:49 +05:00
backgroundColor: colors.nav,
alignSelf: 'center',
borderRadius: 5,
marginVertical: 5,
2021-06-08 12:26:55 +05:00
}}>
<TouchableOpacity
onPress={() => {
2021-07-17 20:40:49 +05:00
eSendEvent(eOpenJumpToDialog, type);
2021-06-08 12:26:55 +05:00
}}
activeOpacity={0.9}
hitSlop={{top: 10, left: 10, right: 30, bottom: 15}}
style={{
height: '100%',
justifyContent: 'center',
}}>
<Heading
2021-07-17 20:40:49 +05:00
color={color}
2021-06-08 12:26:55 +05:00
size={SIZE.sm}
style={{
minWidth: 60,
alignSelf: 'center',
textAlignVertical: 'center',
}}>
{!item.title || item.title === '' ? 'Pinned' : item.title}
</Heading>
</TouchableOpacity>
2021-07-13 14:10:51 +05:00
2021-07-17 20:40:49 +05:00
<View
style={{
flexDirection: 'row',
alignItems: 'center',
}}>
{index === 0 ? (
<Button
onPress={() => {
eSendEvent(eOpenSortDialog, type);
}}
title={groupBy}
icon={
groupOptions.sortDirection === 'asc'
? 'sort-ascending'
: 'sort-descending'
}
height={25}
style={{
borderRadius: 100,
paddingHorizontal: 0,
backgroundColor: 'transparent',
marginRight: type === 'notes' || type === "home" || type === 'notebooks' ? 10 : 0,
}}
type="gray"
iconPosition="right"
/>
) : null}
{type === 'notes' || type === 'notebooks' || type === 'home' ? (
<ActionIcon
customStyle={{
width: 25,
height: 25,
}}
color={colors.icon}
name={listMode == 'compact' ? 'view-list' : 'view-list-outline'}
onPress={() => {
SettingsService.set(
type !== 'notebooks' ? 'notesListMode' : 'notebooksListMode',
listMode === 'normal' ? 'compact' : 'normal',
);
}}
size={SIZE.md}
/>
) : null}
</View>
2021-06-08 12:26:55 +05:00
</View>
);
};