New navigation model (#1)
* Flatten all routing into a single stack * Replace router with custom implementation * Add shell header and titles * Add tab selector * Add back/forward history menus on longpress * Fix: don't modify state during render * Add refresh() to navigation and reroute navigations to the current location to refresh instead of add to history * Cache screens during navigation to maintain scroll position and improve load-time for renders
This commit is contained in:
parent
d1470bad66
commit
97f52b6a03
57 changed files with 1382 additions and 1159 deletions
64
src/view/routes.ts
Normal file
64
src/view/routes.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import React from 'react'
|
||||
import {IconProp} from '@fortawesome/fontawesome-svg-core'
|
||||
import {Home} from './screens/Home'
|
||||
import {Search} from './screens/Search'
|
||||
import {Notifications} from './screens/Notifications'
|
||||
import {Login} from './screens/Login'
|
||||
import {Signup} from './screens/Signup'
|
||||
import {NotFound} from './screens/NotFound'
|
||||
import {Composer} from './screens/Composer'
|
||||
import {PostThread} from './screens/PostThread'
|
||||
import {PostLikedBy} from './screens/PostLikedBy'
|
||||
import {PostRepostedBy} from './screens/PostRepostedBy'
|
||||
import {Profile} from './screens/Profile'
|
||||
import {ProfileFollowers} from './screens/ProfileFollowers'
|
||||
import {ProfileFollows} from './screens/ProfileFollows'
|
||||
|
||||
export type ScreenParams = {
|
||||
params: Record<string, any>
|
||||
}
|
||||
export type Route = [React.FC<ScreenParams>, IconProp, RegExp]
|
||||
export type MatchResult = {
|
||||
Com: React.FC<ScreenParams>
|
||||
icon: IconProp
|
||||
params: Record<string, any>
|
||||
}
|
||||
|
||||
const r = (pattern: string) => new RegExp('^' + pattern + '([?]|$)', 'i')
|
||||
export const routes: Route[] = [
|
||||
[Home, 'house', r('/')],
|
||||
[Search, 'magnifying-glass', r('/search')],
|
||||
[Notifications, 'bell', r('/notifications')],
|
||||
[Profile, ['far', 'user'], r('/profile/(?<name>[^/]+)')],
|
||||
[ProfileFollowers, 'users', r('/profile/(?<name>[^/]+)/followers')],
|
||||
[ProfileFollows, 'users', r('/profile/(?<name>[^/]+)/follows')],
|
||||
[
|
||||
PostThread,
|
||||
['far', 'message'],
|
||||
r('/profile/(?<name>[^/]+)/post/(?<recordKey>[^/]+)'),
|
||||
],
|
||||
[
|
||||
PostLikedBy,
|
||||
'heart',
|
||||
r('/profile/(?<name>[^/]+)/post/(?<recordKey>[^/]+)/liked-by'),
|
||||
],
|
||||
[
|
||||
PostRepostedBy,
|
||||
'retweet',
|
||||
r('/profile/(?<name>[^/]+)/post/(?<recordKey>[^/]+)/reposted-by'),
|
||||
],
|
||||
[Composer, 'pen-nib', r('/compose')],
|
||||
[Login, ['far', 'user'], r('/login')],
|
||||
[Signup, ['far', 'user'], r('/signup')],
|
||||
]
|
||||
|
||||
export function match(url: string): MatchResult {
|
||||
for (const [Com, icon, pattern] of routes) {
|
||||
const res = pattern.exec(url)
|
||||
if (res) {
|
||||
// TODO: query params
|
||||
return {Com, icon, params: res.groups || {}}
|
||||
}
|
||||
}
|
||||
return {Com: NotFound, icon: 'magnifying-glass', params: {}}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue