From 36a9fceab30d6c5b70c68aee7a9250a1c61a92b6 Mon Sep 17 00:00:00 2001 From: Liam Fitzgerald Date: Tue, 5 Oct 2021 14:08:01 +1000 Subject: [PATCH] landscape: refactor notification redirects --- .../src/logic/lib/notificationRedirects.ts | 90 ++++++++++++++++++ .../views/landscape/components/Content.tsx | 92 +------------------ 2 files changed, 92 insertions(+), 90 deletions(-) create mode 100644 pkg/interface/src/logic/lib/notificationRedirects.ts diff --git a/pkg/interface/src/logic/lib/notificationRedirects.ts b/pkg/interface/src/logic/lib/notificationRedirects.ts new file mode 100644 index 000000000..83f459bc6 --- /dev/null +++ b/pkg/interface/src/logic/lib/notificationRedirects.ts @@ -0,0 +1,90 @@ +import useMetadataState from '../state/metadata'; +import ob from 'urbit-ob'; + +function getGroupResourceRedirect(key: string) { + const association = useMetadataState.getState().associations.graph[`/ship/${key}`]; + const { metadata } = association; + if(!association || !('graph' in metadata.config)) { + return ''; + } + return `/~landscape${association.group}/resource/${metadata.config.graph}${association.resource}`; +} + +function getPostRedirect(key: string, segs: string[]) { + const association = useMetadataState.getState().associations.graph[`/ship/${key}`]; + const { metadata } = association; + if(!association || !('graph' in metadata.config)) { + return ''; + } + return `/~landscape${association.group}/feed/thread/${segs.slice(0, -1).join('/')}`; +} + +function getChatRedirect(chat: string, segs: string[]) { + const qs = segs.length > 0 ? `?msg=${segs[0]}` : ''; + return `${getGroupResourceRedirect(chat)}${qs}`; +} + +function getPublishRedirect(graphKey: string, segs: string[]) { + const base = getGroupResourceRedirect(graphKey); + if(segs.length === 3) { + return `${base}/note/${segs[0]}`; + } else if (segs.length === 4) { + return `${base}/note/${segs[0]}?selected=${segs[2]}`; + } + return base; +} + +function getLinkRedirect(graphKey: string, segs: string[]) { + const base = getGroupResourceRedirect(graphKey); + if(segs.length === 1) { + return `${base}/index/${segs[0]}`; + } else if (segs.length === 3) { + return `${base}/index/${segs[0]}?selected=${segs[1]}`; + } + return base; +} + +function getGraphRedirect(link: string) { + const [,mark, ship, name, ...rest] = link.split('/'); + const graphKey = `${ship}/${name}`; + switch(mark) { + case 'graph-validator-dm': + return `/~landscape/messages/dm/${ob.patp(rest[0])}`; + case 'graph-validator-chat': + return getChatRedirect(graphKey, rest); + case 'graph-validator-publish': + return getPublishRedirect(graphKey, rest); + case 'graph-validator-link': + return getLinkRedirect(graphKey, rest); + case 'graph-validator-post': + return getPostRedirect(graphKey, rest); + default: + return''; + } +} + +function getInviteRedirect(link: string) { + const [,,app,uid] = link.split('/'); + return `/invites/${app}/${uid}`; +} + +function getDmRedirect(link: string) { + const [,,ship] = link.split('/'); + return `/~landscape/messages/dm/${ship}`; +} +function getGroupRedirect(link: string) { + const [,,ship,name] = link.split('/'); + return `/~landscape/ship/${ship}/${name}`; +} + +export function getNotificationRedirect(link: string) { + if(link.startsWith('/graph-validator')) { + return getGraphRedirect(link); + } else if (link.startsWith('/invite')) { + return getInviteRedirect(link); + } else if (link.startsWith('/dm')) { + return getDmRedirect(link); + } else if (link.startsWith('/groups')) { + return getGroupRedirect(link); + } +} diff --git a/pkg/interface/src/views/landscape/components/Content.tsx b/pkg/interface/src/views/landscape/components/Content.tsx index e6d1e40e9..a802960b0 100644 --- a/pkg/interface/src/views/landscape/components/Content.tsx +++ b/pkg/interface/src/views/landscape/components/Content.tsx @@ -2,7 +2,6 @@ import { Box } from '@tlon/indigo-react'; import React, { useCallback, useEffect } from 'react'; import { Route, Switch, useHistory, useLocation } from 'react-router-dom'; import styled from 'styled-components'; -import ob from 'urbit-ob'; import { useLocalStorageState } from '~/logic/lib/useLocalStorageState'; import useMetadataState from '~/logic/state/metadata'; import LaunchApp from '~/views/apps/launch/App'; @@ -15,6 +14,7 @@ import { useShortcut } from '~/logic/state/settings'; import Landscape from '~/views/landscape/index'; import GraphApp from '../../apps/graph/App'; +import { getNotificationRedirect } from '~/logic/lib/notificationRedirects'; export const Container = styled(Box)` flex-grow: 1; @@ -23,94 +23,6 @@ export const Container = styled(Box)` height: calc(100% - 62px); `; -function getGroupResourceRedirect(key: string) { - const association = useMetadataState.getState().associations.graph[`/ship/${key}`]; - const { metadata } = association; - if(!association || !('graph' in metadata.config)) { - return ''; - } - return `/~landscape${association.group}/resource/${metadata.config.graph}${association.resource}`; -} - -function getPostRedirect(key: string, segs: string[]) { - const association = useMetadataState.getState().associations.graph[`/ship/${key}`]; - const { metadata } = association; - if(!association || !('graph' in metadata.config)) { - return ''; - } - return `/~landscape${association.group}/feed/thread/${segs.slice(0, -1).join('/')}`; -} - -function getChatRedirect(chat: string, segs: string[]) { - const qs = segs.length > 0 ? `?msg=${segs[0]}` : ''; - return `${getGroupResourceRedirect(chat)}${qs}`; -} - -function getPublishRedirect(graphKey: string, segs: string[]) { - const base = getGroupResourceRedirect(graphKey); - if(segs.length === 3) { - return `${base}/note/${segs[0]}`; - } else if (segs.length === 4) { - return `${base}/note/${segs[0]}?selected=${segs[2]}`; - } - return base; -} - -function getLinkRedirect(graphKey: string, segs: string[]) { - const base = getGroupResourceRedirect(graphKey); - if(segs.length === 1) { - return `${base}/index/${segs[0]}`; - } else if (segs.length === 3) { - return `${base}/index/${segs[0]}?selected=${segs[1]}`; - } - return base; -} - -function getGraphRedirect(link: string) { - const [,mark, ship, name, ...rest] = link.split('/'); - const graphKey = `${ship}/${name}`; - switch(mark) { - case 'graph-validator-dm': - return `/~landscape/messages/dm/${ob.patp(rest[0])}`; - case 'graph-validator-chat': - return getChatRedirect(graphKey, rest); - case 'graph-validator-publish': - return getPublishRedirect(graphKey, rest); - case 'graph-validator-link': - return getLinkRedirect(graphKey, rest); - case 'graph-validator-post': - return getPostRedirect(graphKey, rest); - default: - return''; - } -} - -function getInviteRedirect(link: string) { - const [,,app,uid] = link.split('/'); - return `/invites/${app}/${uid}`; -} - -function getDmRedirect(link: string) { - const [,,ship] = link.split('/'); - return `/~landscape/messages/dm/${ship}`; -} -function getGroupRedirect(link: string) { - const [,,ship,name] = link.split('/'); - return `/~landscape/ship/${ship}/${name}`; -} - -function getNotificationRedirect(link: string) { - if(link.startsWith('/graph-validator')) { - return getGraphRedirect(link); - } else if (link.startsWith('/invite')) { - return getInviteRedirect(link); - } else if (link.startsWith('/dm')) { - return getDmRedirect(link); - } else if (link.startsWith('/groups')) { - return getGroupRedirect(link); - } -} - export const Content = (props) => { const history = useHistory(); const location = useLocation(); @@ -119,7 +31,7 @@ export const Content = (props) => { useEffect(() => { const query = new URLSearchParams(location.search); if(mdLoaded && query.has('grid-note')) { - history.push(getNotificationRedirect(query.get('grid-note'))); + history.push(getNotificationRedirect(query.get('grid-note')!)); } else if(mdLoaded && query.has('grid-link')) { const link = decodeURIComponent(query.get('grid-link')!); history.push(`/perma${link}`);