hark: only allow marking as read

Fixes urbit/landscape#149.
This commit is contained in:
Matilde Park 2021-01-14 19:08:51 -05:00
parent faef9b1d42
commit a0f324b800
4 changed files with 4 additions and 133 deletions

View File

@ -1,105 +0,0 @@
import React, { useCallback } from "react";
import _ from "lodash";
import { Link } from "react-router-dom";
import GlobalApi from "~/logic/api/global";
import {
Rolodex,
Associations,
ChatNotifIndex,
ChatNotificationContents,
Groups,
} from "~/types";
import { BigInteger } from "big-integer";
import { Box, Col } from "@tlon/indigo-react";
import { Header } from "./header";
import { pluralize } from "~/logic/lib/util";
import ChatMessage from "../chat/components/ChatMessage";
function describeNotification(mention: boolean, lent: number) {
const msg = pluralize("message", lent !== 1);
if (mention) {
return `mentioned you in ${msg} in`;
}
return `sent ${msg} in`;
}
export function ChatNotification(props: {
index: ChatNotifIndex;
contents: ChatNotificationContents;
archived: boolean;
read: boolean;
time: number;
timebox: BigInteger;
associations: Associations;
contacts: Rolodex;
groups: Groups;
api: GlobalApi;
}) {
const { index, contents, read, time, api, timebox } = props;
const authors = _.map(contents, "author");
const { chat, mention } = index;
const association = props?.associations?.chat?.[chat];
const groupPath = association?.["group-path"];
const appPath = index?.chat;
const group = props?.groups?.[groupPath];
const desc = describeNotification(mention, contents.length);
const groupContacts = props.contacts[groupPath] || {};
const onClick = useCallback(() => {
if (props.archived) {
return;
}
const func = read ? "unread" : "read";
return api.hark[func](timebox, { chat: index });
}, [api, timebox, index, read]);
return (
<Col onClick={onClick} flexGrow="1" p="2">
<Header
chat
associations={props.associations}
read={read}
archived={props.archived}
time={time}
authors={authors}
moduleIcon="Chat"
channel={chat}
contacts={props.contacts}
group={groupPath}
description={desc}
/>
<Col pb="3" pl="5">
{_.map(_.take(contents, 5), (content, idx) => {
let workspace = groupPath;
if (workspace === undefined || group?.hidden) {
workspace = '/home';
}
const to = `/~landscape${workspace}/resource/chat${appPath}?msg=${content.number}`;
return (
<Link key={idx} to={to}>
<ChatMessage
measure={() => {}}
msg={content}
isLastRead={false}
group={group}
contacts={groupContacts}
fontSize='0'
pt='2'
/>
</Link>
);
})}
{contents.length > 5 && (
<Box ml="4" mt="3" mb="2" color="gray" fontSize="1">
and {contents.length - 5} other message
{contents.length > 6 ? "s" : ""}
</Box>
)}
</Col>
</Col>
);
}

View File

@ -247,12 +247,11 @@ export function GraphNotification(props: {
const desc = describeNotification(index.description, contents.length !== 1);
const onClick = useCallback(() => {
if (props.archived) {
if (props.archived || read) {
return;
}
const func = read ? "unread" : "read";
return api.hark[func](timebox, { graph: index });
return api.hark["read"](timebox, { graph: index });
}, [api, timebox, index, read]);
return (

View File

@ -75,8 +75,9 @@ export function Header(props: {
{!props.archived && (
<Icon
display="block"
opacity={read ? 0 : 1}
mr={2}
icon={read ? "Circle" : "Bullet"}
icon="Bullet"
color="blue"
/>
)}

View File

@ -16,7 +16,6 @@ import { getParentIndex } from "~/logic/lib/notification";
import { StatelessAsyncAction } from "~/views/components/StatelessAsyncAction";
import { GroupNotification } from "./group";
import { GraphNotification } from "./graph";
import { ChatNotification } from "./chat";
import { BigInteger } from "big-integer";
import { useHovering } from "~/logic/lib/util";
@ -55,9 +54,6 @@ function getMuted(
if ("group" in index) {
return _.findIndex(groups || [], (g) => g === index.group.group) === -1;
}
if ("chat" in index) {
return _.findIndex(chat || [], (c) => c === index.chat.chat) === -1;
}
return false;
}
@ -176,26 +172,6 @@ export function Notification(props: NotificationProps) {
</Wrapper>
);
}
if ("chat" in notification.index) {
const index = notification.index.chat;
const c: ChatNotificationContents = (contents as any).chat;
return (
<Wrapper>
<ChatNotification
api={props.api}
index={index}
contents={c}
contacts={props.contacts}
read={read}
archived={archived}
groups={props.groups}
timebox={props.time}
time={time}
associations={associations}
/>
</Wrapper>
);
}
return null;
}