Skip to content

Commit

Permalink
feat: attachment click handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnyTheCarrot committed Aug 15, 2023
1 parent 3ae289f commit 6c24c87
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/Content/Attachment/ImageAttachment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import React from "react";
import * as Styles from "./style";
import type { APIAttachment } from "discord-api-types/v10";
import { t } from "i18next";
import { useConfig } from "../../core/ConfigContext";

interface ImageAttachmentProps {
attachment: APIAttachment;
}

function ImageAttachment(props: ImageAttachmentProps) {
const { openImageOnClick } = useConfig();

if (!props.attachment.width || !props.attachment.height) {
// todo: dev mode only
console.error(
Expand All @@ -23,11 +26,14 @@ function ImageAttachment(props: ImageAttachmentProps) {
props.attachment.height
);

console.log(openImageOnClick);

return (
<Styles.ImageAttachment
src={props.attachment.url}
width={width}
height={height}
onClick={() => openImageOnClick?.(props.attachment)}
placeholder={
<Styles.LazyImagePlaceholder style={{ width, height }}>
{t("loading")}
Expand Down
10 changes: 5 additions & 5 deletions src/Message/MessageAuthor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function MessageAuthor({
guildId,
...props
}: MessageAuthorProps) {
const { resolveRole, resolveMember, userMentionOnClick } = useConfig();
const { resolveRole, resolveMember, userOnClick } = useConfig();
const member = guildId ? resolveMember(author.id, guildId) : null;
const isGuildMember = member !== null;

Expand Down Expand Up @@ -74,9 +74,9 @@ function MessageAuthor({
if (onlyShowUsername) {
return (
<Styles.MessageAuthor
clickable={userMentionOnClick !== undefined}
clickable={userOnClick !== undefined}
{...props}
onClick={() => userMentionOnClick?.(author)}
onClick={() => userOnClick?.(author)}
>
<Styles.Username style={{ color: dominantRoleColor }}>
{displayName}
Expand All @@ -87,9 +87,9 @@ function MessageAuthor({

return (
<Styles.MessageAuthor
clickable={userMentionOnClick !== undefined}
clickable={userOnClick !== undefined}
{...props}
onClick={() => userMentionOnClick?.(author)}
onClick={() => userOnClick?.(author)}
>
<Styles.Avatar
src={getAvatar(author, {
Expand Down
6 changes: 4 additions & 2 deletions src/core/ConfigContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ReactElement} from "react";
import type { ReactElement } from "react";
import { createContext, useContext } from "react";
import type {
APIChannel,
Expand All @@ -11,6 +11,7 @@ import type {
} from "discord-api-types/v10";
import type { SvgConfig } from "./svgs";
import type { Tag } from "../ChatTag/style";
import type { APIAttachment } from "discord-api-types/v10";

export type PartialSvgConfig = Partial<SvgConfig>;

Expand Down Expand Up @@ -39,11 +40,12 @@ export type Config<SvgConfig extends PartialSvgConfig> = {
// Click handlers
currentUser(): APIUser | null;
seeThreadOnClick?(messageId: Snowflake, thread: APIChannel): void;
userMentionOnClick?(user: APIUser): void;
userOnClick?(user: APIUser): void;
roleMentionOnClick?(role: APIRole): void;
channelMentionOnClick?(channel: APIChannel): void;
openPinnedMessagesOnClick?(channel: APIChannel): void;
messageComponentButtonOnClick?(message: APIMessage, customId: string): void;
openImageOnClick?(attachment: APIAttachment): void;
};

export const ConfigContext = createContext<Config<PartialSvgConfig>>({
Expand Down
6 changes: 3 additions & 3 deletions src/markdown/render/elements/mentions/userMention.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface UserMentionProps {
}

function UserMention({ userId }: UserMentionProps) {
const { resolveUser, userMentionOnClick } = useConfig();
const { resolveUser, userOnClick } = useConfig();

// todo: resolve current channel to resolve member
const user = resolveUser(userId);
Expand All @@ -19,9 +19,9 @@ function UserMention({ userId }: UserMentionProps) {
return (
<Styles.Mention
onClick={() => {
if (user !== null) userMentionOnClick?.(user);
if (user !== null) userOnClick?.(user);
}}
canBeClicked={userMentionOnClick !== undefined}
canBeClicked={userOnClick !== undefined}
>
<Styles.MentionIcon>@</Styles.MentionIcon>
{text}
Expand Down
7 changes: 4 additions & 3 deletions src/stories/Wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,7 @@ const Wrapper: Decorator = (Story) => {
seeThreadOnClick={(messageId, thread) =>
alert(`See Thread "${thread.name}" clicked on message ${messageId}`)
}
userMentionOnClick={(user) =>
alert(`User "${getDisplayName(user)}" mention clicked!`)
}
userOnClick={(user) => alert(`User "${getDisplayName(user)}" clicked!`)}
roleMentionOnClick={(role) =>
alert(`Role "${role.name}" mention clicked!`)
}
Expand All @@ -403,6 +401,9 @@ const Wrapper: Decorator = (Story) => {

return null;
}}
openImageOnClick={(imageAttachment) => {
alert(`Image attachment ${imageAttachment.filename} clicked!`);
}}
>
{({ themeClass }) => (
<div className={themeClass}>
Expand Down

0 comments on commit 6c24c87

Please sign in to comment.