groups: Use garden's prot. hand. prefs to determine app link

This commit is contained in:
Patrick O'Sullivan 2022-03-25 14:30:26 -05:00
parent 1399ef9fa5
commit ef77aaf802

View File

@ -1,15 +1,40 @@
import { BaseAnchor, Box, BoxProps, Button, Center, Col, H3, Icon, Image, Row, Text } from '@tlon/indigo-react'; import {
import { Association, GraphNode, resourceFromPath, GraphConfig, Treaty, deSig } from '@urbit/api'; BaseAnchor,
Box,
BoxProps,
Button,
Center,
Col,
H3,
Icon,
Image,
Row,
Text
} from '@tlon/indigo-react';
import {
Association,
GraphNode,
resourceFromPath,
GraphConfig,
Treaty,
deSig
} from '@urbit/api';
import React, { useCallback, useEffect, useState } from 'react'; import React, { useCallback, useEffect, useState } from 'react';
import _ from 'lodash'; import _ from 'lodash';
import { Link, useLocation } from 'react-router-dom'; import { Link, useLocation } from 'react-router-dom';
import api from '~/logic/api';
import { import {
getPermalinkForGraph, GraphPermalink as IGraphPermalink, parsePermalink, getPermalinkForGraph,
GraphPermalink as IGraphPermalink,
parsePermalink,
AppPermalink as IAppPermalink AppPermalink as IAppPermalink
} from '~/logic/lib/permalinks'; } from '~/logic/lib/permalinks';
import useGardenSettingsState, {
useProtocolHandling
} from '~/logic/state/gardenSettings';
import { getModuleIcon, GraphModule } from '~/logic/lib/util'; import { getModuleIcon, GraphModule } from '~/logic/lib/util';
import { useVirtualResizeProp } from '~/logic/lib/virtualContext'; import { useVirtualResizeProp } from '~/logic/lib/virtualContext';
import useGraphState from '~/logic/state/graph'; import useGraphState from '~/logic/state/graph';
import useMetadataState from '~/logic/state/metadata'; import useMetadataState from '~/logic/state/metadata';
import { GroupLink } from '~/views/components/GroupLink'; import { GroupLink } from '~/views/components/GroupLink';
import { TranscludedNode } from './TranscludedNode'; import { TranscludedNode } from './TranscludedNode';
@ -17,6 +42,7 @@ import styled from 'styled-components';
import Author from '~/views/components/Author'; import Author from '~/views/components/Author';
import useDocketState, { useTreaty } from '~/logic/state/docket'; import useDocketState, { useTreaty } from '~/logic/state/docket';
import { createJoinParams } from '~/views/landscape/components/Join/Join'; import { createJoinParams } from '~/views/landscape/components/Join/Join';
import { useBrowserId } from '~/logic/state/local';
function Placeholder(type) { function Placeholder(type) {
const lines = (type) => { const lines = (type) => {
@ -30,8 +56,8 @@ function Placeholder(type) {
} }
}; };
return ( return (
<Box p='12px 12px 6px'> <Box p="12px 12px 6px">
<Row mb='6px' height="4"> <Row mb="6px" height="4">
<Box <Box
backgroundColor="washedGray" backgroundColor="washedGray"
size="4" size="4"
@ -46,7 +72,7 @@ function Placeholder(type) {
/> />
</Row> </Row>
{_.times(lines(type), i => ( {_.times(lines(type), i => (
<Row margin="6px" ml='32px' height="4"> <Row margin="6px" ml="32px" height="4">
<Box <Box
backgroundColor="washedGray" backgroundColor="washedGray"
height="4" height="4"
@ -59,7 +85,7 @@ function Placeholder(type) {
); );
} }
function GroupPermalink(props: { group: string; }) { function GroupPermalink(props: { group: string }) {
const { group } = props; const { group } = props;
return ( return (
<GroupLink <GroupLink
@ -80,22 +106,28 @@ function GraphPermalink(
full?: boolean; full?: boolean;
} }
) { ) {
const { full = false, showOurContact, pending, graph, group, index, transcluded } = props; const {
full = false,
showOurContact,
pending,
graph,
group,
index,
transcluded
} = props;
const location = useLocation(); const location = useLocation();
const { ship, name } = resourceFromPath(graph); const { ship, name } = resourceFromPath(graph);
const node = useGraphState( const node = useGraphState(
useCallback(s => s.looseNodes?.[`${deSig(ship)}/${name}`]?.[index] as GraphNode, [ useCallback(
graph, s => s.looseNodes?.[`${deSig(ship)}/${name}`]?.[index] as GraphNode,
index [graph, index]
]) )
); );
const [errored, setErrored] = useState(false); const [errored, setErrored] = useState(false);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const getNode = useGraphState(s => s.getNode); const getNode = useGraphState(s => s.getNode);
const association = useMetadataState( const association = useMetadataState(
useCallback(s => s.associations.graph[graph] as Association | null, [ useCallback(s => s.associations.graph[graph] as Association | null, [graph])
graph
])
); );
useVirtualResizeProp(Boolean(node)); useVirtualResizeProp(Boolean(node));
@ -118,23 +150,19 @@ function GraphPermalink(
const showTransclusion = Boolean(association && node && transcluded < 1); const showTransclusion = Boolean(association && node && transcluded < 1);
const permalink = (() => { const permalink = (() => {
const link = `/perma${getPermalinkForGraph(group, graph, index).slice(16)}`; const link = `/perma${getPermalinkForGraph(group, graph, index).slice(16)}`;
return (!association && !loading) return !association && !loading
? { search: createJoinParams('groups', group, link) } : link; ? { search: createJoinParams('groups', group, link) }
: link;
})(); })();
const [nodeGroupHost, nodeGroupName] = association?.group.split('/').slice(-2) ?? ['Unknown', 'Unknown']; const [nodeGroupHost, nodeGroupName] = association?.group
.split('/')
.slice(-2) ?? ['Unknown', 'Unknown'];
const [nodeChannelHost, nodeChannelName] = association?.resource const [nodeChannelHost, nodeChannelName] = association?.resource
.split('/') .split('/')
.slice(-2) ?? ['Unknown', 'Unknown']; .slice(-2) ?? ['Unknown', 'Unknown'];
const [ const [locChannelName, locChannelHost, , , , locGroupName, locGroupHost] =
locChannelName, location.pathname.split('/').reverse();
locChannelHost,
,
,
,
locGroupName,
locGroupHost
] = location.pathname.split('/').reverse();
const isInSameResource = const isInSameResource =
locChannelHost === nodeChannelHost && locChannelHost === nodeChannelHost &&
@ -156,7 +184,10 @@ function GraphPermalink(
e.stopPropagation(); e.stopPropagation();
}} }}
> >
{loading && association && !errored && Placeholder((association.metadata.config as GraphConfig).graph)} {loading &&
association &&
!errored &&
Placeholder((association.metadata.config as GraphConfig).graph)}
{showTransclusion && index && !loading && ( {showTransclusion && index && !loading && (
<TranscludedNode <TranscludedNode
transcluded={transcluded + 1} transcluded={transcluded + 1}
@ -169,7 +200,9 @@ function GraphPermalink(
<PermalinkDetails <PermalinkDetails
known known
showTransclusion={showTransclusion} showTransclusion={showTransclusion}
icon={getModuleIcon((association.metadata.config as GraphConfig).graph as GraphModule)} icon={getModuleIcon(
(association.metadata.config as GraphConfig).graph as GraphModule
)}
title={association.metadata.title} title={association.metadata.title}
/> />
)} )}
@ -177,11 +210,13 @@ function GraphPermalink(
<PermalinkDetails <PermalinkDetails
known known
showTransclusion={showTransclusion} showTransclusion={showTransclusion}
icon={getModuleIcon((association.metadata.config as GraphConfig).graph as GraphModule)} icon={getModuleIcon(
(association.metadata.config as GraphConfig).graph as GraphModule
)}
title={association.metadata.title} title={association.metadata.title}
/> />
)} )}
{isInSameResource && transcluded !== 2 && !loading && <Row height='2' />} {isInSameResource && transcluded !== 2 && !loading && <Row height="2" />}
{!association && !loading && ( {!association && !loading && (
<PermalinkDetails <PermalinkDetails
icon="Groups" icon="Groups"
@ -245,8 +280,9 @@ const AppSkeleton = props => (
function AppPermalink({ link, ship, desk }: Omit<IAppPermalink, 'type'>) { function AppPermalink({ link, ship, desk }: Omit<IAppPermalink, 'type'>) {
const treaty = useTreaty(ship, desk); const treaty = useTreaty(ship, desk);
const hasProtocolHandling = Boolean(window?.navigator?.registerProtocolHandler); const browserId = useBrowserId();
const href = hasProtocolHandling ? link : `/apps/grid/perma?ext=${link}`; const protocolHandling = useProtocolHandling(browserId);
const href = protocolHandling ? link : `/apps/grid/perma?ext=${link}`;
useEffect(() => { useEffect(() => {
if (!treaty) { if (!treaty) {
@ -254,6 +290,12 @@ function AppPermalink({ link, ship, desk }: Omit<IAppPermalink, 'type'>) {
} }
}, [treaty, ship, desk]); }, [treaty, ship, desk]);
useEffect(() => {
const { initialize, getAll } = useGardenSettingsState.getState();
initialize(api);
getAll();
}, []);
return ( return (
<Row <Row
display="inline-flex" display="inline-flex"
@ -265,14 +307,22 @@ function AppPermalink({ link, ship, desk }: Omit<IAppPermalink, 'type'>) {
> >
<AppTile display={['none', 'block']} {...treaty} /> <AppTile display={['none', 'block']} {...treaty} />
<Col flex="1"> <Col flex="1">
<Row flexDirection={['row', 'column']} alignItems={['center', 'start']} marginBottom={2}> <Row
flexDirection={['row', 'column']}
alignItems={['center', 'start']}
marginBottom={2}
>
<AppTile display={['block', 'none']} {...treaty} /> <AppTile display={['block', 'none']} {...treaty} />
<Col> <Col>
<H3 color="black">{ treaty?.title || '%' + desk }</H3> <H3 color="black">{treaty?.title || '%' + desk}</H3>
<Author ship={treaty?.ship || ship} showImage dontShowTime={true} /> <Author ship={treaty?.ship || ship} showImage dontShowTime={true} />
</Col> </Col>
</Row> </Row>
{treaty && <ClampedText marginBottom={2} color="gray">{treaty.info}</ClampedText>} {treaty && (
<ClampedText marginBottom={2} color="gray">
{treaty.info}
</ClampedText>
)}
{!treaty && ( {!treaty && (
<> <>
<AppSkeleton /> <AppSkeleton />
@ -318,7 +368,7 @@ function PermalinkDetails(props: {
<Row gapX="2" alignItems="center"> <Row gapX="2" alignItems="center">
<Box width={4} height={4}> <Box width={4} height={4}>
<Center width={4} height={4}> <Center width={4} height={4}>
<Icon icon={icon} color='gray' /> <Icon icon={icon} color="gray" />
</Center> </Center>
</Box> </Box>
<Text gray mono={!known}> <Text gray mono={!known}>
@ -356,8 +406,6 @@ export function PermalinkEmbed(props: {
/> />
); );
case 'app': case 'app':
return ( return <AppPermalink {...permalink} />;
<AppPermalink {...permalink} />
);
} }
} }