about summary refs log tree commit diff
path: root/src/view/routes.ts
blob: d31dbae3542178969c4b756b611c404ecdbd348f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 {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>
  visible: boolean
}
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'),
  ],
  [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: {}}
}