landscape: refactor notification redirects

This commit is contained in:
Liam Fitzgerald 2021-10-05 14:08:01 +10:00
parent 88216c9f9c
commit 36a9fceab3
2 changed files with 92 additions and 90 deletions

View File

@ -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);
}
}

View File

@ -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}`);