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

142 lines
4.2 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,
2021-07-28 14:15:20 +05:00
eUnSubscribeEvent
2021-07-13 14:10:51 +05:00
} from '../../services/EventManager';
2021-07-17 20:40:49 +05:00
import SettingsService from '../../services/SettingsService';
2021-09-14 11:14:48 +05:00
import {GROUP, SORT} from '../../utils';
2021-07-17 20:40:49 +05:00
import {COLORS_NOTE} from '../../utils/Colors';
2021-10-02 10:26:29 +05:00
import {db} from '../../utils/database';
2021-07-13 14:10:51 +05:00
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-09-13 13:42:01 +05:00
export const SectionHeader = ({item, index, type, title,color}) => {
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(
2021-07-28 14:15:20 +05:00
db.settings?.getGroupOptions(type)
2021-07-13 14:10:51 +05:00
);
2021-09-14 11:14:48 +05:00
let groupBy = Object.keys(GROUP).find(
key => GROUP[key] === groupOptions.groupBy
2021-07-13 14:10:51 +05:00
);
2021-07-17 20:40:49 +05:00
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 = () => {
2021-09-14 11:14:48 +05:00
console.log(groupOptions);
2021-07-13 14:10:51 +05:00
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,
2021-07-28 14:15:20 +05:00
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%',
2021-07-28 14:15:20 +05:00
justifyContent: 'center'
2021-06-08 12:26:55 +05:00
}}>
<Heading
color={COLORS_NOTE[color?.toLowerCase()] || colors.accent }
2021-06-08 12:26:55 +05:00
size={SIZE.sm}
style={{
minWidth: 60,
alignSelf: 'center',
2021-07-28 14:15:20 +05:00
textAlignVertical: 'center'
2021-06-08 12:26:55 +05:00
}}>
{!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',
2021-07-28 14:15:20 +05:00
alignItems: 'center'
2021-07-17 20:40:49 +05:00
}}>
{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
2021-07-28 14:15:20 +05:00
: 0
}}
type="gray"
iconPosition="right"
/>
{type === 'notes' || type === 'notebooks' || type === 'home' ? (
<ActionIcon
customStyle={{
width: 25,
2021-07-28 14:15:20 +05:00
height: 25
}}
color={colors.icon}
name={listMode == 'compact' ? 'view-list' : 'view-list-outline'}
onPress={() => {
SettingsService.set(
type !== 'notebooks'
? 'notesListMode'
: 'notebooksListMode',
2021-07-28 14:15:20 +05:00
listMode === 'normal' ? 'compact' : 'normal'
);
}}
2021-07-28 14:15:20 +05:00
size={SIZE.lg - 2}
/>
) : null}
</>
2021-07-17 20:40:49 +05:00
) : null}
</View>
2021-06-08 12:26:55 +05:00
</View>
);
};