-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restructure content templates Closes #287
- Loading branch information
1 parent
2d713ce
commit ec46821
Showing
16 changed files
with
191 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { useSuspenseQuery } from '@tanstack/react-query' | ||
import { StyleSheet } from 'react-native' | ||
|
||
import Container from '~/components/atoms/Container' | ||
import GurmukhiLine from '~/components/molecules/GurmukhiLine' | ||
import { baniQuery } from '~/services/data' | ||
import { units } from '~/themes' | ||
|
||
import Lines from '../Lines' | ||
|
||
const styles = StyleSheet.create( { | ||
header: { | ||
textAlign: 'center', | ||
fontSize: units.title1, | ||
paddingTop: units.base * 2, | ||
}, | ||
} ) | ||
|
||
type BaniProps = { | ||
id: string, | ||
} | ||
|
||
const Bani = ( { id }: BaniProps ) => { | ||
const { data } = useSuspenseQuery( baniQuery( id ) ) | ||
|
||
const header = <GurmukhiLine style={styles.header}>{data.nameGurmukhi}</GurmukhiLine> | ||
|
||
return ( | ||
<Container> | ||
<Lines | ||
Header={() => header} | ||
lines={data.lines} | ||
/> | ||
</Container> | ||
) | ||
} | ||
|
||
export default Bani |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { render, screen } from '@testing-library/react-native' | ||
|
||
import { settings } from '~/services/settings' | ||
import * as factories from '~/test/factories' | ||
import AtomProvider from '~/test/providers/AtomProvider' | ||
|
||
import Lines from '.' | ||
|
||
describe( '<Lines />', () => { | ||
describe( 'given reader mode is off', () => { | ||
it( 'should load and render lines in grouped mode', () => { | ||
const lines = factories.line.buildList( 15 ) | ||
|
||
render( | ||
<AtomProvider initialValues={[ [ settings.readerMode, false ] ]}> | ||
<Lines lines={lines} /> | ||
</AtomProvider> | ||
) | ||
|
||
expect( screen.queryByText( lines[ 0 ].translations[ 0 ].translation ) ).toBeTruthy() | ||
} ) | ||
} ) | ||
|
||
describe( 'given reader mode is on', () => { | ||
it( 'should load and render lines in reader mode', () => { | ||
const lines = factories.line.buildList( 15 ) | ||
|
||
render( | ||
<AtomProvider initialValues={[ [ settings.readerMode, true ] ]}> | ||
<Lines lines={lines} /> | ||
</AtomProvider> | ||
) | ||
|
||
expect( screen.queryByText( lines[ 0 ].translations[ 0 ].translation ) ).toBeFalsy() | ||
} ) | ||
} ) | ||
} ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { ComponentType } from 'react' | ||
|
||
import { settings, useSetting } from '~/services/settings' | ||
import { LineData } from '~/types/data' | ||
|
||
import GroupedLines from '../GroupedLines' | ||
import ReaderLines from '../ReaderLines' | ||
|
||
type LinesProps = { | ||
lines: LineData[], | ||
Header?: ComponentType, | ||
} | ||
|
||
const Lines = ( { lines, Header }: LinesProps ) => { | ||
const [ isReaderMode ] = useSetting( settings.readerMode ) | ||
|
||
return isReaderMode | ||
? <ReaderLines lines={lines} Header={Header} /> | ||
: <GroupedLines lines={lines} Header={Header} /> | ||
} | ||
|
||
export default Lines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useSuspenseQuery } from '@tanstack/react-query' | ||
|
||
import Container from '~/components/atoms/Container' | ||
import { shabadQuery } from '~/services/data' | ||
|
||
import Lines from '../Lines' | ||
|
||
type ShabadProps = { | ||
id: string, | ||
} | ||
|
||
const Shabad = ( { id }: ShabadProps ) => { | ||
const { data } = useSuspenseQuery( shabadQuery( id ) ) | ||
|
||
return ( | ||
<Container> | ||
<Lines lines={data.lines} /> | ||
</Container> | ||
) | ||
} | ||
|
||
export default Shabad |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,27 @@ | ||
import { useSuspenseQuery } from '@tanstack/react-query' | ||
import { ComponentType } from 'react' | ||
|
||
import Container from '~/components/atoms/Container' | ||
import { getBani, getShabad } from '~/services/data' | ||
import { settings, useSetting } from '~/services/settings' | ||
import { ContentType, LineData } from '~/types/data' | ||
import Empty from '~/components/atoms/Empty' | ||
import { ContentType } from '~/types/data' | ||
|
||
import DefaultLines from './DefaultLines' | ||
import ReaderLines from './ReaderLines' | ||
import Bani from './Bani' | ||
import Shabad from './Shabad' | ||
|
||
type Loaders = { | ||
[screen in ContentType]: ( id: string ) => Promise<{ id: string, lines: LineData[] }> | ||
} | ||
|
||
// ? Loaders return a common interface. Is there a better way to deal with specifics of each type? | ||
const loaders: Loaders = { | ||
shabad: ( id: string ) => getShabad( id ), | ||
bani: ( id: string ) => getBani( id ), | ||
ang: () => Promise.resolve( { id: '', lines: [] } ), | ||
} | ||
// Maybe these should be moved into their own standalone templates entirely | ||
const templates = { | ||
ang: Empty, | ||
bani: Bani, | ||
shabad: Shabad, | ||
} satisfies Record<ContentType, ComponentType<{ id: string }>> | ||
|
||
type ContentTemplateProps = { | ||
id: string, | ||
type: ContentType, | ||
} | ||
|
||
const ContentTemplate = ( { | ||
id, | ||
type, | ||
}: ContentTemplateProps ) => { | ||
const { data } = useSuspenseQuery( { | ||
queryKey: [ 'content', type, id ], | ||
queryFn: () => loaders[ type ]( id ), | ||
} ) | ||
|
||
const [ isReaderMode ] = useSetting( settings.readerMode ) | ||
const Lines = isReaderMode ? ReaderLines : DefaultLines | ||
const ContentTemplate = ( { id, type }: ContentTemplateProps ) => { | ||
const Template = templates[ type ] | ||
|
||
return ( | ||
<Container> | ||
<Lines key={data.id} lines={data.lines} /> | ||
</Container> | ||
) | ||
return <Template id={id} /> | ||
} | ||
|
||
export default ContentTemplate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as atomWithKvStorage } from './atom-with-kv-storage' | ||
export { default as mmkv } from './mmkv' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Provider, WritableAtom } from 'jotai' | ||
import { INTERNAL_InferAtomTuples } from 'jotai/react/utils/useHydrateAtoms' | ||
import { useHydrateAtoms } from 'jotai/utils' | ||
import { ReactNode } from 'react' | ||
|
||
type HydrateAtomsProps<T> = { | ||
initialValues: INTERNAL_InferAtomTuples<T>, | ||
children?: ReactNode, | ||
} | ||
|
||
const HydrateAtoms = <T extends ( readonly [ | ||
WritableAtom<unknown, never[], unknown>, | ||
unknown, | ||
] )[],>( { initialValues, children }: HydrateAtomsProps<T> ) => { | ||
useHydrateAtoms( initialValues ) | ||
|
||
return children | ||
} | ||
|
||
const AtomProvider = <T extends ( readonly [ | ||
WritableAtom<unknown, never[], unknown>, | ||
unknown, | ||
] )[],>( { initialValues, children }: HydrateAtomsProps<T> ) => ( | ||
<Provider> | ||
<HydrateAtoms initialValues={initialValues}>{children}</HydrateAtoms> | ||
</Provider> | ||
) | ||
|
||
export default AtomProvider |