| 26 | } |
| 27 | |
| 28 | export const getPatchyContext = ({ |
| 29 | path, |
| 30 | query, |
| 31 | post, |
| 32 | }: PatchyContextInput): PatchyContext => { |
| 33 | // A post modal opens over any page, so the post wins. |
| 34 | if (post?.id) { |
| 35 | return { kind: 'post', post, tag: post.tags?.[0] }; |
| 36 | } |
| 37 | const [, first, second, third] = path.split('?')[0].split('/'); |
| 38 | if (!first || first === 'my-feed') { |
| 39 | return { kind: 'forYou' }; |
| 40 | } |
| 41 | if (first === 'tags' && second) { |
| 42 | return { kind: 'tag', tag: decodeURIComponent(second).toLowerCase() }; |
| 43 | } |
| 44 | if (first === 'sources' && second) { |
| 45 | return { kind: 'source', source: decodeURIComponent(second) }; |
| 46 | } |
| 47 | if (first === 'bookmarks') { |
| 48 | return { kind: 'bookmarks' }; |
| 49 | } |
| 50 | if (first === 'search') { |
| 51 | return { kind: 'search', query: firstValue(query.q)?.trim() || undefined }; |
| 52 | } |
| 53 | // /posts/best-of and friends are lists of posts too; /posts/<id> without |
| 54 | // an open post (still loading) falls back to a feed. |
| 55 | if (FEED_PATHS.includes(`/${first}`) && third === undefined) { |
| 56 | return { kind: 'feed' }; |
| 57 | } |
| 58 | return { kind: 'other' }; |
| 59 | }; |
| 60 | |
| 61 | // The line under his face in the Streak panel. |
| 62 | export const getPatchyContextLine = (context: PatchyContext): string | null => { |