Add more info to routing

zio/stable
Paul Frazee 2022-11-14 16:30:06 -06:00
parent 700d4e99b9
commit c98edca116
1 changed files with 30 additions and 13 deletions

View File

@ -21,53 +21,70 @@ export type ScreenParams = {
visible: boolean visible: boolean
scrollElRef?: MutableRefObject<FlatList<any> | undefined> scrollElRef?: MutableRefObject<FlatList<any> | undefined>
} }
export type Route = [React.FC<ScreenParams>, IconProp, RegExp] export type Route = [React.FC<ScreenParams>, string, IconProp, RegExp]
export type MatchResult = { export type MatchResult = {
Com: React.FC<ScreenParams> Com: React.FC<ScreenParams>
defaultTitle: string
icon: IconProp icon: IconProp
params: Record<string, any> params: Record<string, any>
isNotFound?: boolean
} }
const r = (pattern: string) => new RegExp('^' + pattern + '([?]|$)', 'i') const r = (pattern: string) => new RegExp('^' + pattern + '([?]|$)', 'i')
export const routes: Route[] = [ export const routes: Route[] = [
[Home, 'house', r('/')], [Home, 'Home', 'house', r('/')],
[Contacts, ['far', 'circle-user'], r('/contacts')], [Contacts, 'Contacts', ['far', 'circle-user'], r('/contacts')],
[Search, 'magnifying-glass', r('/search')], [Search, 'Search', 'magnifying-glass', r('/search')],
[Notifications, 'bell', r('/notifications')], [Notifications, 'Notifications', 'bell', r('/notifications')],
[Settings, 'bell', r('/settings')], [Settings, 'Settings', 'bell', r('/settings')],
[Profile, ['far', 'user'], r('/profile/(?<name>[^/]+)')], [Profile, 'User', ['far', 'user'], r('/profile/(?<name>[^/]+)')],
[ProfileFollowers, 'users', r('/profile/(?<name>[^/]+)/followers')], [
[ProfileFollows, 'users', r('/profile/(?<name>[^/]+)/follows')], ProfileFollowers,
[ProfileMembers, 'users', r('/profile/(?<name>[^/]+)/members')], 'Followers',
'users',
r('/profile/(?<name>[^/]+)/followers'),
],
[ProfileFollows, 'Follows', 'users', r('/profile/(?<name>[^/]+)/follows')],
[ProfileMembers, 'Members', 'users', r('/profile/(?<name>[^/]+)/members')],
[ [
PostThread, PostThread,
'Post',
['far', 'message'], ['far', 'message'],
r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)'), r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)'),
], ],
[ [
PostUpvotedBy, PostUpvotedBy,
'Upvoted by',
'heart', 'heart',
r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)/upvoted-by'), r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)/upvoted-by'),
], ],
[ [
PostDownvotedBy, PostDownvotedBy,
'Downvoted by',
'heart', 'heart',
r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)/downvoted-by'), r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)/downvoted-by'),
], ],
[ [
PostRepostedBy, PostRepostedBy,
'Reposted by',
'retweet', 'retweet',
r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)/reposted-by'), r('/profile/(?<name>[^/]+)/post/(?<rkey>[^/]+)/reposted-by'),
], ],
] ]
export function match(url: string): MatchResult { export function match(url: string): MatchResult {
for (const [Com, icon, pattern] of routes) { for (const [Com, defaultTitle, icon, pattern] of routes) {
const res = pattern.exec(url) const res = pattern.exec(url)
if (res) { if (res) {
// TODO: query params // TODO: query params
return {Com, icon, params: res.groups || {}} return {Com, defaultTitle, icon, params: res.groups || {}}
} }
} }
return {Com: NotFound, icon: 'magnifying-glass', params: {}} return {
Com: NotFound,
defaultTitle: 'Not found',
icon: 'magnifying-glass',
params: {},
isNotFound: true,
}
} }