Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 2020 additions and 809 deletions
import Tippy, { TippyProps } from "@tippyjs/react";
import styled from "styled-components"; import styled from "styled-components";
import { Text } from "preact-i18n";
import { Children } from "../../types/Preact"; import { Children } from "../../types/Preact";
import { Position, Tooltip as TooltipCore, TooltipProps } from "react-tippy";
type Props = Omit<TooltipProps, 'html'> & { type Props = Omit<TippyProps, "children"> & {
position?: Position;
children: Children; children: Children;
content: Children; content: Children;
};
export default function Tooltip(props: Props) {
const { children, content, ...tippyProps } = props;
return (
<Tippy content={content} {...tippyProps}>
{/*
// @ts-expect-error Type mis-match. */}
<div style={`display: flex;`}>{children}</div>
</Tippy>
);
} }
const TooltipBase = styled.div` const PermissionTooltipBase = styled.div`
padding: 8px; display: flex;
font-size: 12px; align-items: center;
border-radius: 4px; flex-direction: column;
color: var(--foreground);
background: var(--secondary-background); span {
font-weight: 700;
text-transform: uppercase;
color: var(--secondary-foreground);
font-size: 11px;
}
code {
font-family: var(--monospace-font);
}
`; `;
export default function Tooltip(props: Props) { export function PermissionTooltip(
props: Omit<Props, "content"> & { permission: string },
) {
const { permission, ...tooltipProps } = props;
return ( return (
<TooltipCore <Tooltip
{...props} content={
// @ts-expect-error <PermissionTooltipBase>
html={<TooltipBase>{props.content}</TooltipBase>} /> <span>
<Text id="app.permissions.required" />
</span>
<code>{permission}</code>
</PermissionTooltipBase>
}
{...tooltipProps}
/>
); );
} }
/* eslint-disable react-hooks/rules-of-hooks */
import { Download, CloudDownload } from "@styled-icons/boxicons-regular";
import { useContext, useEffect, useState } from "preact/hooks";
import { internalSubscribe } from "../../lib/eventEmitter";
import { ThemeContext } from "../../context/Theme";
import IconButton from "../ui/IconButton";
import { updateSW } from "../../main";
import Tooltip from "./Tooltip";
let pendingUpdate = false;
internalSubscribe("PWA", "update", () => (pendingUpdate = true));
interface Props {
style: "titlebar" | "channel";
}
export default function UpdateIndicator({ style }: Props) {
const [pending, setPending] = useState(pendingUpdate);
useEffect(() => {
return internalSubscribe("PWA", "update", () => setPending(true));
});
if (!pending) return null;
const theme = useContext(ThemeContext);
if (style === "titlebar") {
return (
<div class="actions">
<Tooltip
content="A new update is available!"
placement="bottom">
<div onClick={() => updateSW(true)}>
<CloudDownload size={22} color={theme.success} />
</div>
</Tooltip>
</div>
);
}
if (window.isNative) return null;
return (
<IconButton onClick={() => updateSW(true)}>
<Download size={22} color={theme.success} />
</IconButton>
);
}
import Embed from "./embed/Embed"; import { observer } from "mobx-react-lite";
import UserIcon from "../user/UserIcon"; import { Message as MessageObject } from "revolt.js/dist/maps/Messages";
import { Username } from "../user/UserShort";
import Markdown from "../../markdown/Markdown";
import { Children } from "../../../types/Preact";
import Attachment from "./attachments/Attachment";
import { attachContextMenu } from "preact-context-menu"; import { attachContextMenu } from "preact-context-menu";
import { useUser } from "../../../context/revoltjs/hooks"; import { memo } from "preact/compat";
import { useState } from "preact/hooks";
import { QueuedMessage } from "../../../redux/reducers/queue"; import { QueuedMessage } from "../../../redux/reducers/queue";
import { MessageObject } from "../../../context/revoltjs/util";
import MessageBase, { MessageContent, MessageDetail, MessageInfo } from "./MessageBase"; import { useIntermediate } from "../../../context/intermediate/Intermediate";
import { useClient } from "../../../context/revoltjs/RevoltClient";
import Overline from "../../ui/Overline"; import Overline from "../../ui/Overline";
import { useContext } from "preact/hooks";
import { AppContext } from "../../../context/revoltjs/RevoltClient"; import { Children } from "../../../types/Preact";
import Markdown from "../../markdown/Markdown";
import UserIcon from "../user/UserIcon";
import { Username } from "../user/UserShort";
import MessageBase, {
MessageContent,
MessageDetail,
MessageInfo,
} from "./MessageBase";
import Attachment from "./attachments/Attachment";
import { MessageReply } from "./attachments/MessageReply";
import Embed from "./embed/Embed";
interface Props { interface Props {
attachContext?: boolean attachContext?: boolean;
queued?: QueuedMessage queued?: QueuedMessage;
message: MessageObject message: MessageObject;
contrast?: boolean highlight?: boolean;
content?: Children contrast?: boolean;
head?: boolean content?: Children;
head?: boolean;
} }
export default function Message({ attachContext, message, contrast, content: replacement, head, queued }: Props) { const Message = observer(
// TODO: Can improve re-renders here by providing a list ({
// TODO: of dependencies. We only need to update on u/avatar. highlight,
const user = useUser(message.author); attachContext,
const client = useContext(AppContext); message,
contrast,
const content = message.content as string; content: replacement,
return ( head: preferHead,
<MessageBase id={message._id} queued,
head={head} }: Props) => {
contrast={contrast} const client = useClient();
sending={typeof queued !== 'undefined'} const user = message.author;
mention={message.mentions?.includes(client.user!._id)}
failed={typeof queued?.error !== 'undefined'} const { openScreen } = useIntermediate();
onContextMenu={attachContext ? attachContextMenu('Menu', { message, contextualChannel: message.channel, queued }) : undefined}>
<MessageInfo> const content = message.content as string;
{ head ? const head =
<UserIcon target={user} size={36} /> : preferHead || (message.reply_ids && message.reply_ids.length > 0);
<MessageDetail message={message} position="left" /> }
</MessageInfo> // ! TODO: tell fatal to make this type generic
<MessageContent> // bree: Fatal please...
{ head && <span className="author"> const userContext = attachContext
<Username user={user} /> ? (attachContextMenu("Menu", {
<MessageDetail message={message} position="top" /> user: message.author_id,
</span> } contextualChannel: message.channel_id,
{ replacement ?? <Markdown content={content} /> } // eslint-disable-next-line
{ queued?.error && <Overline type="error" error={queued.error} /> } }) as any)
{ message.attachments?.map((attachment, index) => : undefined;
<Attachment key={index} attachment={attachment} hasContent={ index > 0 || content.length > 0 } />) }
{ message.embeds?.map((embed, index) => const openProfile = () =>
<Embed key={index} embed={embed} />) } openScreen({ id: "profile", user_id: message.author_id });
</MessageContent>
</MessageBase> // ! FIXME(?): animate on hover
) const [animate, setAnimate] = useState(false);
}
return (
<div id={message._id}>
{message.reply_ids?.map((message_id, index) => (
<MessageReply
key={message_id}
index={index}
id={message_id}
channel={message.channel!}
/>
))}
<MessageBase
highlight={highlight}
head={
(head &&
!(
message.reply_ids &&
message.reply_ids.length > 0
)) ??
false
}
contrast={contrast}
sending={typeof queued !== "undefined"}
mention={message.mention_ids?.includes(client.user!._id)}
failed={typeof queued?.error !== "undefined"}
onContextMenu={
attachContext
? attachContextMenu("Menu", {
message,
contextualChannel: message.channel_id,
queued,
})
: undefined
}
onMouseEnter={() => setAnimate(true)}
onMouseLeave={() => setAnimate(false)}>
<MessageInfo>
{head ? (
<UserIcon
target={user}
size={36}
onContextMenu={userContext}
onClick={openProfile}
animate={animate}
/>
) : (
<MessageDetail message={message} position="left" />
)}
</MessageInfo>
<MessageContent>
{head && (
<span className="detail">
<Username
className="author"
user={user}
onContextMenu={userContext}
onClick={openProfile}
/>
<MessageDetail
message={message}
position="top"
/>
</span>
)}
{replacement ?? <Markdown content={content} />}
{queued?.error && (
<Overline type="error" error={queued.error} />
)}
{message.attachments?.map((attachment, index) => (
<Attachment
key={index}
attachment={attachment}
hasContent={index > 0 || content.length > 0}
/>
))}
{message.embeds?.map((embed, index) => (
<Embed key={index} embed={embed} />
))}
</MessageContent>
</MessageBase>
</div>
);
},
);
export default memo(Message);
import dayjs from "dayjs"; import { observer } from "mobx-react-lite";
import Tooltip from "../Tooltip"; import { Message } from "revolt.js/dist/maps/Messages";
import styled, { css, keyframes } from "styled-components";
import { decodeTime } from "ulid"; import { decodeTime } from "ulid";
import { Text } from "preact-i18n"; import { Text } from "preact-i18n";
import styled, { css } from "styled-components";
import { MessageObject } from "../../../context/revoltjs/util"; import { useDictionary } from "../../../lib/i18n";
import { dayjs } from "../../../context/Locale";
import Tooltip from "../Tooltip";
export interface BaseMessageProps { export interface BaseMessageProps {
head?: boolean, head?: boolean;
failed?: boolean, failed?: boolean;
mention?: boolean, mention?: boolean;
blocked?: boolean, blocked?: boolean;
sending?: boolean, sending?: boolean;
contrast?: boolean contrast?: boolean;
highlight?: boolean;
} }
const highlight = keyframes`
0% { background: var(--mention); }
66% { background: var(--mention); }
100% { background: transparent; }
`;
export default styled.div<BaseMessageProps>` export default styled.div<BaseMessageProps>`
display: flex; display: flex;
overflow-x: none; overflow: none;
padding: .125rem; padding: 0.125rem;
flex-direction: row; flex-direction: row;
padding-right: 16px; padding-inline-end: 16px;
${ props => props.contrast && css` @media (pointer: coarse) {
padding: .3rem; user-select: none;
border-radius: 4px; }
background: var(--hover);
` }
${ props => props.head && css` ${(props) =>
margin-top: 12px; props.contrast &&
` } css`
padding: 0.3rem;
background: var(--hover);
border-radius: var(--border-radius);
`}
${ props => props.mention && css` ${(props) =>
background: var(--mention); props.head &&
` } css`
margin-top: 12px;
`}
${ props => props.blocked && css` ${(props) =>
filter: blur(4px); props.mention &&
transition: 0.2s ease filter; css`
background: var(--mention);
`}
&:hover { ${(props) =>
filter: none; props.blocked &&
} css`
` } filter: blur(4px);
transition: 0.2s ease filter;
${ props => props.sending && css` &:hover {
opacity: 0.8; filter: none;
color: var(--tertiary-foreground); }
` } `}
${ props => props.failed && css` ${(props) =>
color: var(--error); props.sending &&
` } css`
opacity: 0.8;
color: var(--tertiary-foreground);
`}
.author { ${(props) =>
props.failed &&
css`
color: var(--error);
`}
${(props) =>
props.highlight &&
css`
animation-name: ${highlight};
animation-timing-function: ease;
animation-duration: 3s;
`}
.detail {
gap: 8px; gap: 8px;
display: flex; display: flex;
align-items: center; align-items: center;
flex-shrink: 0;
} }
.author {
overflow: hidden;
cursor: pointer;
font-weight: 600 !important;
display: -webkit-box;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
text-overflow: ellipsis;
white-space: normal;
&:hover {
text-decoration: underline;
}
}
.copy { .copy {
width: 0; display: block;
opacity: 0; overflow: hidden;
} }
&:hover { &:hover {
...@@ -81,72 +135,125 @@ export const MessageInfo = styled.div` ...@@ -81,72 +135,125 @@ export const MessageInfo = styled.div`
flex-direction: row; flex-direction: row;
justify-content: center; justify-content: center;
::selection { .copyBracket {
background-color: transparent; opacity: 0;
color: var(--tertiary-foreground); position: absolute;
}
.copyTime {
opacity: 0;
position: absolute;
}
svg {
user-select: none;
cursor: pointer;
&:active {
transform: translateY(1px);
}
} }
time { time {
opacity: 0; opacity: 0;
}
time,
.edited {
margin-top: 1px;
cursor: default; cursor: default;
display: inline; display: inline;
font-size: 10px; font-size: 10px;
padding-top: 1px;
color: var(--tertiary-foreground); color: var(--tertiary-foreground);
} }
time,
.edited > div {
&::selection {
background-color: transparent;
color: var(--tertiary-foreground);
}
}
.header {
cursor: pointer;
}
`; `;
export const MessageContent = styled.div` export const MessageContent = styled.div`
min-width: 0; min-width: 0;
flex-grow: 1; flex-grow: 1;
display: flex; display: flex;
overflow: hidden; // overflow: hidden;
font-size: 0.875rem;
flex-direction: column; flex-direction: column;
justify-content: center; justify-content: center;
font-size: var(--text-size);
`; `;
export const DetailBase = styled.div` export const DetailBase = styled.div`
flex-shrink: 0;
gap: 4px; gap: 4px;
font-size: 10px; font-size: 10px;
display: inline-flex; display: inline-flex;
color: var(--tertiary-foreground); color: var(--tertiary-foreground);
.edited {
cursor: default;
&::selection {
background-color: transparent;
color: var(--tertiary-foreground);
}
}
`; `;
export function MessageDetail({ message, position }: { message: MessageObject, position: 'left' | 'top' }) { export const MessageDetail = observer(
if (position === 'left') { ({ message, position }: { message: Message; position: "left" | "top" }) => {
if (message.edited) { const dict = useDictionary();
return (
<span> if (position === "left") {
<span className="copy"> if (message.edited) {
[<time>{dayjs(decodeTime(message._id)).format("H:mm")}</time>] return (
</span> <>
<Tooltip content={dayjs(message.edited).format("LLLL")}> <time className="copyTime">
<Text id="app.main.channel.edited" /> <i className="copyBracket">[</i>
</Tooltip> {dayjs(decodeTime(message._id)).format(
</span> dict.dayjs?.timeFormat,
) )}
} else { <i className="copyBracket">]</i>
</time>
<span className="edited">
<Tooltip
content={dayjs(message.edited).format("LLLL")}>
<Text id="app.main.channel.edited" />
</Tooltip>
</span>
</>
);
}
return ( return (
<> <>
<time> <time>
<i className="copy">[</i> <i className="copyBracket">[</i>
{ dayjs(decodeTime(message._id)).format("H:mm") } {dayjs(decodeTime(message._id)).format(
<i className="copy">]</i> dict.dayjs?.timeFormat,
)}
<i className="copyBracket">]</i>
</time> </time>
</> </>
) );
} }
}
return ( return (
<DetailBase> <DetailBase>
<time> <time>{dayjs(decodeTime(message._id)).calendar()}</time>
{dayjs(decodeTime(message._id)).calendar()} {message.edited && (
</time> <Tooltip content={dayjs(message.edited).format("LLLL")}>
{ message.edited && <Tooltip content={dayjs(message.edited).format("LLLL")}> <span className="edited">
<Text id="app.main.channel.edited" /> <Text id="app.main.channel.edited" />
</Tooltip> } </span>
</DetailBase> </Tooltip>
) )}
} </DetailBase>
);
},
);
.actions.imageAction {
grid-template:
"name icon external download" auto
"size icon external download" auto
/ minmax(20px, 1fr) min-content min-content;
}
.actions {
display: grid;
grid-template:
"icon name external download" auto
"icon size external download" auto
/ min-content minmax(20px, 1fr) min-content;
align-items: center;
column-gap: 12px;
width: 100%;
padding: 8px;
overflow: none;
color: var(--foreground);
background: var(--secondary-background);
span {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.filesize {
grid-area: size;
font-size: 10px;
color: var(--secondary-foreground);
}
.downloadIcon {
grid-area: download;
}
.externalType {
grid-area: external;
}
.iconType {
grid-area: icon;
}
}
import styled from "styled-components";
import { Children } from "../../../../types/Preact";
const Grid = styled.div`
display: grid;
overflow: hidden;
max-width: min(var(--attachment-max-width), 100%, var(--width));
max-height: min(var(--attachment-max-height), var(--height));
aspect-ratio: var(--aspect-ratio);
img,
video {
min-width: 100%;
min-height: 100%;
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
grid-area: 1 / 1;
}
&.spoiler {
img,
video {
filter: blur(44px);
}
border-radius: var(--border-radius);
}
`;
export default Grid;
type Props = Omit<
JSX.HTMLAttributes<HTMLDivElement>,
"children" | "as" | "style"
> & {
style?: JSX.CSSProperties;
children?: Children;
width: number;
height: number;
};
export function SizedGrid(props: Props) {
const { width, height, children, style, ...divProps } = props;
return (
<Grid
{...divProps}
style={{
...style,
"--width": `${width}px`,
"--height": `${height}px`,
"--aspect-ratio": width / height,
}}>
{children}
</Grid>
);
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.