|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { View } from 'react-native-ui-lib'; |
| 4 | + |
| 5 | +import { AppSettingsState } from '../types'; |
| 6 | +import { AppSettingsContainer } from './Container'; |
| 7 | +import { AppSettingsEntryFontSize, AppSettingsEntrySwitch } from './Entries'; |
| 8 | + |
| 9 | +export interface AppSettingsProps<TSettings extends Record<string, any>> { |
| 10 | + hook: () => AppSettingsState<TSettings>; |
| 11 | + sections: AppSettingsSection<TSettings>[]; |
| 12 | +} |
| 13 | + |
| 14 | +export interface AppSettingsSection<TSettings extends Record<string, any>> { |
| 15 | + title?: string; |
| 16 | + items: AppSettingsSectionItem<TSettings>[]; |
| 17 | +} |
| 18 | + |
| 19 | +export interface AppSettingsSectionItem<TSettings extends Record<string, any>> { |
| 20 | + field: keyof TSettings; |
| 21 | + component: 'switch' | 'font-size'; |
| 22 | + title: string; |
| 23 | + description?: string; |
| 24 | +} |
| 25 | + |
| 26 | +export function AppSettings<TSettings extends Record<string, any>>({ |
| 27 | + hook, |
| 28 | + sections, |
| 29 | +}: AppSettingsProps<TSettings>) { |
| 30 | + const settings = hook(); |
| 31 | + |
| 32 | + const renderSections = sections.map((section, index) => { |
| 33 | + const renderItems = section.items.map( |
| 34 | + ({ field, component, ...item }, index) => { |
| 35 | + let rendered = null; |
| 36 | + if (component === 'switch') { |
| 37 | + rendered = ( |
| 38 | + <AppSettingsEntrySwitch<TSettings> |
| 39 | + field={field} |
| 40 | + value={settings[field]} |
| 41 | + dispatch={settings.dispatch} |
| 42 | + {...item} |
| 43 | + /> |
| 44 | + ); |
| 45 | + } else if (component === 'font-size') { |
| 46 | + rendered = ( |
| 47 | + <AppSettingsEntryFontSize<TSettings> |
| 48 | + field={field} |
| 49 | + value={settings[field]} |
| 50 | + dispatch={settings.dispatch} |
| 51 | + {...item} |
| 52 | + /> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return ( |
| 57 | + <React.Fragment key={field.toString()}> |
| 58 | + {rendered} |
| 59 | + {index < section.items.length - 1 && ( |
| 60 | + <View height={1} bg-$backgroundNeutral /> |
| 61 | + )} |
| 62 | + </React.Fragment> |
| 63 | + ); |
| 64 | + } |
| 65 | + ); |
| 66 | + return ( |
| 67 | + <AppSettingsContainer key={index.toString()} title={section.title}> |
| 68 | + {renderItems} |
| 69 | + </AppSettingsContainer> |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + return <View>{renderSections}</View>; |
| 74 | +} |
0 commit comments