@planningcenter/olio-react-native
v0.3.7
Published
Components and utilities for the react-native space
Readme
Olio is a component library that is being developed for sharing mobile components across multiple React Native apps. It is meant to be hodgepodge of components and other shared concerns, which is why it’s named “olio”.
“Olio” definition:
- a highly spiced stew of various meats and vegetables originating from Spain and Portugal.
- a miscellaneous collection of things.
Installation
yarn add @planningcenter/olio-react-nativeRichTextEditor
Our rich text editor is powered by trix and react-native-webview. It's a zero configuration text editor that works offline and is version controlled.
import { RichTextEditor } from '@planningcenter/olio-react-native'
const [value, setValue] = useState('')
<RichTextEditor value={value} onChange={setValue} />DevTools
In order to use the menu, you will need to do the following:
Install
@planningcenter/olio-react-nativeIn a main file (e.g. app.js), add the following:
import { DevTools } from '@planningcenter/olio-react-native' function App() { ... return ( ... <DevTools /> ) }
IMPORTANT: Make sure that this is only added once to an app as initializing it multiple times could lead to issues in development
Tools
There are a few tools that you can use to help with the development process. To add a tool to the menu, you simply need to import that tool into your file and add it under the DevTools component.
JoltSection
This tool shows the current Jolt connection ID and the active channel subscriptions, with a subscriber count for each channel. That makes it easy to spot duplicate subscribes. Your Jolt client needs a setOnStateChangeHandlerForDev(handler) method that calls handler with { connectionId, subscriptions: [{ channelName, subscriberCount }] } whenever its state changes, and stops calling it when passed undefined. Pass that client to useJoltManager wherever it's created.
Example
import { DevTools, useJoltManager } from '@planningcenter/olio-react-native'
function JoltProvider({ children }) {
const jolt = useMyJoltClient()
useJoltManager(jolt)
...
}
export default function MyDevTools() {
return (
<DevTools>
<DevTools.JoltSection />
</DevTools>
)
}Adapting @planningcenter/jolt-client
@planningcenter/jolt-client doesn't have setOnStateChangeHandlerForDev, so wrap it in a class that counts subscribers per channel and reports them. Your app then subscribes and unsubscribes through the wrapper instead of the client. This one also shares a single subscription between everyone on the same channel, so one screen unsubscribing doesn't cut off another.
import JoltClient from '@planningcenter/jolt-client'
import type { JoltState } from '@planningcenter/olio-react-native'
type SubscribeOptions = Parameters<JoltClient['subscribe']>[1]
export class DevJoltClient {
jolt: JoltClient
private channels: Record<
string,
{ subscription: ReturnType<JoltClient['subscribe']>; count: number }
> = {}
private onStateChange?: (state: JoltState) => void
constructor(...args: ConstructorParameters<typeof JoltClient>) {
this.jolt = new JoltClient(...args)
}
subscribe(channel: string, options?: SubscribeOptions) {
this.channels[channel] ??= { subscription: this.jolt.subscribe(channel, options), count: 0 }
this.channels[channel].count += 1
this.reportState()
return this.channels[channel].subscription
}
unsubscribe(channel: string) {
const entry = this.channels[channel]
if (!entry) return
entry.count -= 1
if (entry.count === 0) {
this.jolt.unsubscribe(channel)
delete this.channels[channel]
}
this.reportState()
}
setOnStateChangeHandlerForDev(handler: ((state: JoltState) => void) | undefined) {
this.onStateChange = handler
this.reportState()
}
private async reportState() {
if (!this.onStateChange) return
const connectionId = await this.jolt.connection.connectionId
this.onStateChange?.({
connectionId,
subscriptions: Object.entries(this.channels).map(([channelName, { count }]) => ({
channelName,
subscriberCount: count,
})),
})
}
}LogSection
This tool allows you to filter out certain console logs. You can filter by log types (e.g. log, warn, error) or by message regexes. Filters can be combined to better control your console outputs. You will need to import and call useLogManager around app initialization to get the filters to work without having to open the dev tools menu.
Example
import { DevTools, useLogManager } from '@planningcenter/olio-react-native'
export default function MyDevTools() {
useLogManager()
return (
<DevTools>
<DevTools.LogSection />
</DevTools>
)
}NavigationSection
This tool allows you to jump to a specific screen immediately instead of trying to find a link to that screen. This requires an onNavigate function that takes in a routeName and routeParams to determine how to handle navigation.
Example
import { DevTools } from '@planningcenter/olio-react-native'
import { useNavigation } from '@react-navigation/native'
export default function MyDevTools() {
const navigation = useNavigation()
return (
<DevTools>
<DevTools.NavigationSection onNavigate={navigation.navigate} />
</DevTools>
)
}NotificationSection
This tool sends a local test notification with a title, body, link, and notification ID. The form is saved between launches. Notification payloads differ between apps, so you schedule the notification yourself in onSend. Throw from onSend to show an error alert.
Example
import * as Notifications from 'expo-notifications'
import { DevTools } from '@planningcenter/olio-react-native'
export default function MyDevTools() {
return (
<DevTools>
<DevTools.NotificationSection
onSend={async ({ title, body, link, notificationId }) => {
await Notifications.scheduleNotificationAsync({
content: { title, body, data: { link, key: notificationId } },
trigger: null,
})
}}
/>
</DevTools>
)
}RequestSection
This tool allows you to override certain urls with custom responses. This can be useful for debugging when certain requests fail, testing different responses, and mocking endpoints that don't exist yet. The last 20 requests the app made to the domain passed to useRequestManager are listed under "Recent Requests", and pressing one creates and applies an override with its status and JSON response. You will need to import and call useRequestManager around app initialization to get the overrides to work without having to open the dev tools menu.
Pass your API's domain to useRequestManager. Only requests whose URL contains it are listed under "Recent Requests" or logged, which keeps third-party traffic like connectivity checks out. Without a domain, every request is included.
Example
import { DevTools, useRequestManager } from '@planningcenter/olio-react-native'
export default function MyDevTools() {
useRequestManager('api.example.com')
return (
<DevTools>
<DevTools.RequestSection />
</DevTools>
)
}StorageSection
This tool lists the keys stored in AsyncStorage, grouping keys that share a prefix before a :. Press a key to copy its value, or delete keys individually or all at once.
Example
import { DevTools } from '@planningcenter/olio-react-native'
export default function MyDevTools() {
return (
<DevTools>
<DevTools.StorageSection />
</DevTools>
)
}Custom tools
If you would like to create a custom tool, all you need to do is add children inside the DevTool component. You can use the following components to have a more consistent look
- Section (wrapper for new tools)
- Button
- Heading
- Icon
- Switch
- Text
- TextInput
For example, if you wanted to create a tool that tests alert, you could do something like this:
import { Alert, Text, View } from 'react-native'
import { DevTools } from '@planningcenter/olio-react-native'
...
const [value, setValue] = useState('')
<DevTools settings={mySettings}>
<DevTools.Section title="Alert Test">
<Text>Enter text to send alert</Text>
<View style={{ flexDirection: 'row', alignItems: 'center', columnGap: 8 }}>
<DevTools.TextInput value={value} onChangeText={setValue} />
<DevTools.Button text="Send" onPress={() => Alert.alert(value)} />
</View>
</DevTools.Section>
</DevTools>
...Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
Made with create-react-native-library
