mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-02-13 16:13:24 +01:00
add option to edit pack meta
This commit is contained in:
parent
77ebd1a174
commit
e9d8a7b26e
8 changed files with 524 additions and 163 deletions
|
@ -1,7 +1,22 @@
|
||||||
import React, { useMemo } from 'react';
|
import React, { FormEventHandler, useCallback, useMemo, useState } from 'react';
|
||||||
import { as, Box, Text, config, Avatar, AvatarImage, AvatarFallback, Button } from 'folds';
|
import {
|
||||||
|
as,
|
||||||
|
Box,
|
||||||
|
Text,
|
||||||
|
config,
|
||||||
|
Avatar,
|
||||||
|
AvatarImage,
|
||||||
|
AvatarFallback,
|
||||||
|
Button,
|
||||||
|
Icon,
|
||||||
|
Icons,
|
||||||
|
Input,
|
||||||
|
TextArea,
|
||||||
|
Chip,
|
||||||
|
Menu,
|
||||||
|
} from 'folds';
|
||||||
import Linkify from 'linkify-react';
|
import Linkify from 'linkify-react';
|
||||||
import { ImagePack, PackMetaReader } from '../../plugins/custom-emoji';
|
import { ImagePack, ImageUsage, packMetaEqual, PackMetaReader } from '../../plugins/custom-emoji';
|
||||||
import { mxcUrlToHttp } from '../../utils/matrix';
|
import { mxcUrlToHttp } from '../../utils/matrix';
|
||||||
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
|
||||||
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
import { useMatrixClient } from '../../hooks/useMatrixClient';
|
||||||
|
@ -11,28 +26,219 @@ import { BreakWord } from '../../styles/Text.css';
|
||||||
import { LINKIFY_OPTS } from '../../plugins/react-custom-html-parser';
|
import { LINKIFY_OPTS } from '../../plugins/react-custom-html-parser';
|
||||||
import { ContainerColor } from '../../styles/ContainerColor.css';
|
import { ContainerColor } from '../../styles/ContainerColor.css';
|
||||||
import { ImageTile } from './ImageTile';
|
import { ImageTile } from './ImageTile';
|
||||||
|
import { SettingTile } from '../setting-tile';
|
||||||
|
import { UsageSwitcher } from './UsageSwitcher';
|
||||||
|
import { useFilePicker } from '../../hooks/useFilePicker';
|
||||||
|
import { useObjectURL } from '../../hooks/useObjectURL';
|
||||||
|
import { createUploadAtom, UploadSuccess } from '../../state/upload';
|
||||||
|
import { CompactUploadCardRenderer } from '../upload-card';
|
||||||
|
import * as css from './style.css';
|
||||||
|
|
||||||
type ImagePackAvatarProps = {
|
type ImagePackAvatarProps = {
|
||||||
meta: PackMetaReader;
|
url?: string;
|
||||||
|
name?: string;
|
||||||
};
|
};
|
||||||
function ImagePackAvatar({ meta }: ImagePackAvatarProps) {
|
function ImagePackAvatar({ url, name }: ImagePackAvatarProps) {
|
||||||
const mx = useMatrixClient();
|
|
||||||
const useAuthentication = useMediaAuthentication();
|
|
||||||
const packAvatar = meta.avatar ? mxcUrlToHttp(mx, meta.avatar, useAuthentication) : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Avatar size="400" className={ContainerColor({ variant: 'Secondary' })}>
|
<Avatar size="500" className={ContainerColor({ variant: 'Secondary' })}>
|
||||||
{packAvatar ? (
|
{url ? (
|
||||||
<AvatarImage src={packAvatar} alt={meta.name ?? 'Unknown'} />
|
<AvatarImage src={url} alt={name ?? 'Unknown'} />
|
||||||
) : (
|
) : (
|
||||||
<AvatarFallback>
|
<AvatarFallback>
|
||||||
<Text size="H4">{nameInitials(meta.name ?? 'Unknown')}</Text>
|
<Text size="H2">{nameInitials(name ?? 'Unknown')}</Text>
|
||||||
</AvatarFallback>
|
</AvatarFallback>
|
||||||
)}
|
)}
|
||||||
</Avatar>
|
</Avatar>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ImagePackProfileProps = {
|
||||||
|
meta: PackMetaReader;
|
||||||
|
canEdit?: boolean;
|
||||||
|
onEdit?: () => void;
|
||||||
|
};
|
||||||
|
function ImagePackProfile({ meta, canEdit, onEdit }: ImagePackProfileProps) {
|
||||||
|
const mx = useMatrixClient();
|
||||||
|
const useAuthentication = useMediaAuthentication();
|
||||||
|
const avatarUrl = meta.avatar
|
||||||
|
? mxcUrlToHttp(mx, meta.avatar, useAuthentication) ?? undefined
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box gap="400">
|
||||||
|
<Box grow="Yes" direction="Column" gap="300">
|
||||||
|
<Box direction="Column" gap="100">
|
||||||
|
<Text className={BreakWord} size="H5">
|
||||||
|
{meta.name ?? 'Unknown'}
|
||||||
|
</Text>
|
||||||
|
{meta.attribution && (
|
||||||
|
<Text className={BreakWord} size="T200">
|
||||||
|
<Linkify options={LINKIFY_OPTS}>{meta.attribution}</Linkify>
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
{canEdit && (
|
||||||
|
<Box gap="200">
|
||||||
|
<Chip
|
||||||
|
variant="Secondary"
|
||||||
|
fill="Soft"
|
||||||
|
radii="300"
|
||||||
|
before={<Icon size="50" src={Icons.Pencil} />}
|
||||||
|
onClick={onEdit}
|
||||||
|
outlined
|
||||||
|
>
|
||||||
|
<Text size="B300">Edit</Text>
|
||||||
|
</Chip>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
<Box shrink="No">
|
||||||
|
<ImagePackAvatar url={avatarUrl} name={meta.name} />
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
type ImagePackProfileEditProps = {
|
||||||
|
meta: PackMetaReader;
|
||||||
|
onCancel: () => void;
|
||||||
|
onSave: (meta: PackMetaReader) => void;
|
||||||
|
};
|
||||||
|
function ImagePackProfileEdit({ meta, onCancel, onSave }: ImagePackProfileEditProps) {
|
||||||
|
const mx = useMatrixClient();
|
||||||
|
const useAuthentication = useMediaAuthentication();
|
||||||
|
const [avatar, setAvatar] = useState(meta.avatar);
|
||||||
|
|
||||||
|
const avatarUrl = avatar ? mxcUrlToHttp(mx, avatar, useAuthentication) ?? undefined : undefined;
|
||||||
|
|
||||||
|
const [imageFile, setImageFile] = useState<File>();
|
||||||
|
const avatarFileUrl = useObjectURL(imageFile);
|
||||||
|
const uploadingAvatar = avatarFileUrl ? avatar === meta.avatar : false;
|
||||||
|
const uploadAtom = useMemo(() => {
|
||||||
|
if (imageFile) return createUploadAtom(imageFile);
|
||||||
|
return undefined;
|
||||||
|
}, [imageFile]);
|
||||||
|
|
||||||
|
const pickFile = useFilePicker(setImageFile, false);
|
||||||
|
|
||||||
|
const handleRemoveUpload = useCallback(() => {
|
||||||
|
setImageFile(undefined);
|
||||||
|
setAvatar(meta.avatar);
|
||||||
|
}, [meta.avatar]);
|
||||||
|
|
||||||
|
const handleUploaded = useCallback((upload: UploadSuccess) => {
|
||||||
|
setAvatar(upload.mxc);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleSubmit: FormEventHandler<HTMLFormElement> = (evt) => {
|
||||||
|
evt.preventDefault();
|
||||||
|
if (uploadingAvatar) return;
|
||||||
|
|
||||||
|
const target = evt.target as HTMLFormElement | undefined;
|
||||||
|
const nameInput = target?.nameInput as HTMLInputElement | undefined;
|
||||||
|
const attributionTextArea = target?.attributionTextArea as HTMLTextAreaElement | undefined;
|
||||||
|
if (!nameInput || !attributionTextArea) return;
|
||||||
|
|
||||||
|
const name = nameInput.value.trim();
|
||||||
|
const attribution = attributionTextArea.value.trim();
|
||||||
|
if (!name) return;
|
||||||
|
|
||||||
|
const metaReader = new PackMetaReader({
|
||||||
|
avatar_url: avatar,
|
||||||
|
display_name: name,
|
||||||
|
attribution,
|
||||||
|
});
|
||||||
|
onSave(metaReader);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box as="form" onSubmit={handleSubmit} direction="Column" gap="400">
|
||||||
|
<Box gap="400">
|
||||||
|
<Box grow="Yes" direction="Column" gap="100">
|
||||||
|
<Text size="L400">Pack Avatar</Text>
|
||||||
|
{uploadAtom ? (
|
||||||
|
<Box gap="200" direction="Column">
|
||||||
|
<CompactUploadCardRenderer
|
||||||
|
uploadAtom={uploadAtom}
|
||||||
|
onRemove={handleRemoveUpload}
|
||||||
|
onComplete={handleUploaded}
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
) : (
|
||||||
|
<Box gap="200">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
size="300"
|
||||||
|
variant="Secondary"
|
||||||
|
fill="Soft"
|
||||||
|
radii="300"
|
||||||
|
onClick={() => pickFile('image/*')}
|
||||||
|
>
|
||||||
|
<Text size="B300">Upload</Text>
|
||||||
|
</Button>
|
||||||
|
{!avatar && meta.avatar && (
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
size="300"
|
||||||
|
variant="Success"
|
||||||
|
fill="None"
|
||||||
|
radii="300"
|
||||||
|
onClick={() => setAvatar(meta.avatar)}
|
||||||
|
>
|
||||||
|
<Text size="B300">Reset</Text>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
{avatar && (
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
size="300"
|
||||||
|
variant="Critical"
|
||||||
|
fill="None"
|
||||||
|
radii="300"
|
||||||
|
onClick={() => setAvatar(undefined)}
|
||||||
|
>
|
||||||
|
<Text size="B300">Remove</Text>
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
<Box shrink="No">
|
||||||
|
<ImagePackAvatar url={avatarFileUrl ?? avatarUrl} name={meta.name} />
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
<Box direction="Inherit" gap="100">
|
||||||
|
<Text size="L400">Name</Text>
|
||||||
|
<Input name="nameInput" defaultValue={meta.name} variant="Secondary" radii="300" required />
|
||||||
|
</Box>
|
||||||
|
<Box direction="Inherit" gap="100">
|
||||||
|
<Text size="L400">Attribution</Text>
|
||||||
|
<TextArea
|
||||||
|
name="attributionTextArea"
|
||||||
|
defaultValue={meta.attribution}
|
||||||
|
variant="Secondary"
|
||||||
|
radii="300"
|
||||||
|
/>
|
||||||
|
</Box>
|
||||||
|
<Box gap="300">
|
||||||
|
<Button type="submit" variant="Success" size="300" radii="300" disabled={uploadingAvatar}>
|
||||||
|
<Text size="B300">Save</Text>
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
type="reset"
|
||||||
|
onClick={onCancel}
|
||||||
|
variant="Secondary"
|
||||||
|
fill="Soft"
|
||||||
|
size="300"
|
||||||
|
radii="300"
|
||||||
|
>
|
||||||
|
<Text size="B300">Cancel</Text>
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export type ImagePackContentProps = {
|
export type ImagePackContentProps = {
|
||||||
imagePack: ImagePack;
|
imagePack: ImagePack;
|
||||||
canEdit?: boolean;
|
canEdit?: boolean;
|
||||||
|
@ -41,56 +247,151 @@ export type ImagePackContentProps = {
|
||||||
export const ImagePackContent = as<'div', ImagePackContentProps>(
|
export const ImagePackContent = as<'div', ImagePackContentProps>(
|
||||||
({ imagePack, canEdit, ...props }, ref) => {
|
({ imagePack, canEdit, ...props }, ref) => {
|
||||||
const useAuthentication = useMediaAuthentication();
|
const useAuthentication = useMediaAuthentication();
|
||||||
|
|
||||||
const { meta } = imagePack;
|
|
||||||
const images = useMemo(() => Array.from(imagePack.images.collection.values()), [imagePack]);
|
const images = useMemo(() => Array.from(imagePack.images.collection.values()), [imagePack]);
|
||||||
|
|
||||||
|
const [metaEditing, setMetaEditing] = useState(false);
|
||||||
|
const [unsavedMeta, setUnsavedMeta] = useState<PackMetaReader>();
|
||||||
|
const currentMeta = unsavedMeta ?? imagePack.meta;
|
||||||
|
|
||||||
|
const handleMetaSave = useCallback(
|
||||||
|
(editedMeta: PackMetaReader) => {
|
||||||
|
setMetaEditing(false);
|
||||||
|
setUnsavedMeta(
|
||||||
|
(m) =>
|
||||||
|
new PackMetaReader({
|
||||||
|
...imagePack.meta.content,
|
||||||
|
...m?.content,
|
||||||
|
...editedMeta.content,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[imagePack.meta]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleMetaCancel = () => setMetaEditing(false);
|
||||||
|
|
||||||
|
const handlePackUsageChange = useCallback(
|
||||||
|
(usg: ImageUsage[]) => {
|
||||||
|
setUnsavedMeta(
|
||||||
|
(m) =>
|
||||||
|
new PackMetaReader({
|
||||||
|
...imagePack.meta.content,
|
||||||
|
...m?.content,
|
||||||
|
usage: usg,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
},
|
||||||
|
[imagePack.meta]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleResetSavedChanges = () => {
|
||||||
|
setUnsavedMeta(undefined);
|
||||||
|
};
|
||||||
|
const handleApplySavedChanges = () => {
|
||||||
|
setUnsavedMeta(undefined);
|
||||||
|
};
|
||||||
|
|
||||||
|
const unsavedChanges = unsavedMeta && !packMetaEqual(imagePack.meta, unsavedMeta);
|
||||||
|
const canApplyChanges = !metaEditing;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box grow="Yes" direction="Column" gap="700" {...props} ref={ref}>
|
<Box grow="Yes" direction="Column" gap="700" {...props} ref={ref}>
|
||||||
<Box gap="400">
|
{unsavedChanges && (
|
||||||
<Box shrink="No">
|
<Menu className={css.UnsavedMenu} variant="Success">
|
||||||
<ImagePackAvatar meta={meta} />
|
<Box alignItems="Center" gap="400">
|
||||||
</Box>
|
<Box grow="Yes" direction="Column">
|
||||||
<Box grow="Yes" direction="Column" gap="300">
|
<Text size="T300">
|
||||||
<Box direction="Column" gap="100">
|
<b>You have saved changes!</b>
|
||||||
<Text className={BreakWord} size="H5">
|
|
||||||
{meta.name ?? 'Unknown'}
|
|
||||||
</Text>
|
|
||||||
{meta.attribution && (
|
|
||||||
<Text className={BreakWord} size="T200">
|
|
||||||
<Linkify options={LINKIFY_OPTS}>{meta.attribution}</Linkify>
|
|
||||||
</Text>
|
</Text>
|
||||||
)}
|
</Box>
|
||||||
|
<Box shrink="No" gap="200">
|
||||||
|
<Button
|
||||||
|
size="300"
|
||||||
|
variant="Success"
|
||||||
|
fill="None"
|
||||||
|
radii="300"
|
||||||
|
disabled={!canApplyChanges}
|
||||||
|
onClick={handleResetSavedChanges}
|
||||||
|
>
|
||||||
|
<Text size="B300">Reset</Text>
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
size="300"
|
||||||
|
variant="Success"
|
||||||
|
radii="300"
|
||||||
|
disabled={!canApplyChanges}
|
||||||
|
onClick={handleApplySavedChanges}
|
||||||
|
>
|
||||||
|
<Text size="B300">Apply Changes</Text>
|
||||||
|
</Button>
|
||||||
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Menu>
|
||||||
{canEdit && (
|
)}
|
||||||
<Box gap="200" wrap="Wrap">
|
|
||||||
<Button variant="Secondary" fill="Soft" size="400" radii="300">
|
|
||||||
<Text size="B400">Edit</Text>
|
|
||||||
</Button>
|
|
||||||
</Box>
|
|
||||||
)}
|
|
||||||
</Box>
|
|
||||||
<Box direction="Column" gap="100">
|
<Box direction="Column" gap="100">
|
||||||
<Text size="L400">Images</Text>
|
<Text size="L400">Pack</Text>
|
||||||
<Box direction="Column" gap="100">
|
<SequenceCard
|
||||||
{images.map((image) => (
|
style={{ padding: config.space.S300 }}
|
||||||
<SequenceCard
|
variant="SurfaceVariant"
|
||||||
key={image.shortcode}
|
direction="Column"
|
||||||
style={{ padding: config.space.S300 }}
|
gap="400"
|
||||||
variant="SurfaceVariant"
|
>
|
||||||
direction="Column"
|
{metaEditing ? (
|
||||||
gap="400"
|
<ImagePackProfileEdit
|
||||||
>
|
meta={currentMeta}
|
||||||
<ImageTile
|
onCancel={handleMetaCancel}
|
||||||
image={image}
|
onSave={handleMetaSave}
|
||||||
packUsage={meta.usage}
|
/>
|
||||||
useAuthentication={useAuthentication}
|
) : (
|
||||||
|
<ImagePackProfile
|
||||||
|
meta={currentMeta}
|
||||||
|
canEdit={canEdit}
|
||||||
|
onEdit={() => setMetaEditing(true)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</SequenceCard>
|
||||||
|
<SequenceCard
|
||||||
|
style={{ padding: config.space.S300 }}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<SettingTile
|
||||||
|
title="Images Usage"
|
||||||
|
description="Select how the images are being used: as emojis, as stickers, or as both."
|
||||||
|
after={
|
||||||
|
<UsageSwitcher
|
||||||
|
usage={currentMeta.usage}
|
||||||
|
canEdit={canEdit}
|
||||||
|
onChange={handlePackUsageChange}
|
||||||
/>
|
/>
|
||||||
</SequenceCard>
|
}
|
||||||
))}
|
/>
|
||||||
</Box>
|
</SequenceCard>
|
||||||
</Box>
|
</Box>
|
||||||
|
{images.length > 0 && (
|
||||||
|
<Box direction="Column" gap="100">
|
||||||
|
<Text size="L400">Images</Text>
|
||||||
|
<Box direction="Column" gap="100">
|
||||||
|
{images.map((image) => (
|
||||||
|
<SequenceCard
|
||||||
|
key={image.shortcode}
|
||||||
|
style={{ padding: config.space.S300 }}
|
||||||
|
variant="SurfaceVariant"
|
||||||
|
direction="Column"
|
||||||
|
gap="400"
|
||||||
|
>
|
||||||
|
<ImageTile
|
||||||
|
image={image}
|
||||||
|
packUsage={currentMeta.usage}
|
||||||
|
useAuthentication={useAuthentication}
|
||||||
|
canEdit={canEdit}
|
||||||
|
/>
|
||||||
|
</SequenceCard>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,13 +39,11 @@ export function ImagePackView({ imagePack, requestClose }: ImagePackViewProps) {
|
||||||
<Box grow="Yes">
|
<Box grow="Yes">
|
||||||
<Scroll hideTrack visibility="Hover">
|
<Scroll hideTrack visibility="Hover">
|
||||||
<PageContent>
|
<PageContent>
|
||||||
<Box direction="Column" gap="700">
|
{room ? (
|
||||||
{room ? (
|
<RoomImagePack room={room} imagePack={imagePack} />
|
||||||
<RoomImagePack room={room} imagePack={imagePack} />
|
) : (
|
||||||
) : (
|
<UserImagePack imagePack={imagePack} />
|
||||||
<UserImagePack imagePack={imagePack} />
|
)}
|
||||||
)}
|
|
||||||
</Box>
|
|
||||||
</PageContent>
|
</PageContent>
|
||||||
</Scroll>
|
</Scroll>
|
||||||
</Box>
|
</Box>
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
import React, { MouseEventHandler, useState } from 'react';
|
import React, { useCallback } from 'react';
|
||||||
import { Chip, Icon, Icons, Text, PopOut, Menu, RectCords } from 'folds';
|
import { Badge, Box, Chip, Text } from 'folds';
|
||||||
import FocusTrap from 'focus-trap-react';
|
import { useUsageStr } from './UsageSwitcher';
|
||||||
import { stopPropagation } from '../../utils/keyboard';
|
|
||||||
import { UsageSelector, useUsageStr } from './UsageSelector';
|
|
||||||
import { mxcUrlToHttp } from '../../utils/matrix';
|
import { mxcUrlToHttp } from '../../utils/matrix';
|
||||||
import * as css from './style.css';
|
import * as css from './style.css';
|
||||||
import { ImageUsage, PackImageReader } from '../../plugins/custom-emoji';
|
import { ImageUsage, PackImageReader } from '../../plugins/custom-emoji';
|
||||||
|
@ -13,21 +11,15 @@ type ImageTileProps = {
|
||||||
useAuthentication: boolean;
|
useAuthentication: boolean;
|
||||||
packUsage: ImageUsage[];
|
packUsage: ImageUsage[];
|
||||||
image: PackImageReader;
|
image: PackImageReader;
|
||||||
|
canEdit?: boolean;
|
||||||
};
|
};
|
||||||
export function ImageTile({ image, packUsage, useAuthentication }: ImageTileProps) {
|
export function ImageTile({ image, packUsage, useAuthentication, canEdit }: ImageTileProps) {
|
||||||
const mx = useMatrixClient();
|
const mx = useMatrixClient();
|
||||||
const getUsageStr = useUsageStr();
|
const getUsageStr = useUsageStr();
|
||||||
const usage = image.usage ?? packUsage;
|
|
||||||
|
|
||||||
const [menuCords, setMenuCords] = useState<RectCords>();
|
const handleChange = useCallback((usg: ImageUsage[]) => {
|
||||||
|
|
||||||
const handleChange = (usg: ImageUsage[]) => {
|
|
||||||
console.log(usg);
|
console.log(usg);
|
||||||
};
|
}, []);
|
||||||
|
|
||||||
const handleSelectUsage: MouseEventHandler<HTMLButtonElement> = (event) => {
|
|
||||||
setMenuCords(event.currentTarget.getBoundingClientRect());
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SettingTile
|
<SettingTile
|
||||||
|
@ -41,41 +33,22 @@ export function ImageTile({ image, packUsage, useAuthentication }: ImageTileProp
|
||||||
title={image.shortcode}
|
title={image.shortcode}
|
||||||
description={image.body}
|
description={image.body}
|
||||||
after={
|
after={
|
||||||
<>
|
canEdit ? (
|
||||||
<Chip
|
<Box shrink="No" alignItems="Center" gap="200">
|
||||||
variant="Secondary"
|
<Chip variant="Secondary" radii="Pill" outlined>
|
||||||
radii="Pill"
|
<Text size="B300">Edit</Text>
|
||||||
after={<Icon src={Icons.ChevronBottom} size="100" />}
|
</Chip>
|
||||||
onClick={handleSelectUsage}
|
</Box>
|
||||||
>
|
) : undefined
|
||||||
<Text size="B300">{getUsageStr(usage)}</Text>
|
|
||||||
</Chip>
|
|
||||||
<PopOut
|
|
||||||
anchor={menuCords}
|
|
||||||
offset={5}
|
|
||||||
position="Bottom"
|
|
||||||
align="End"
|
|
||||||
content={
|
|
||||||
<FocusTrap
|
|
||||||
focusTrapOptions={{
|
|
||||||
initialFocus: false,
|
|
||||||
onDeactivate: () => setMenuCords(undefined),
|
|
||||||
clickOutsideDeactivates: true,
|
|
||||||
isKeyForward: (evt: KeyboardEvent) =>
|
|
||||||
evt.key === 'ArrowDown' || evt.key === 'ArrowRight',
|
|
||||||
isKeyBackward: (evt: KeyboardEvent) =>
|
|
||||||
evt.key === 'ArrowUp' || evt.key === 'ArrowLeft',
|
|
||||||
escapeDeactivates: stopPropagation,
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Menu>
|
|
||||||
<UsageSelector selected={usage} onChange={handleChange} />
|
|
||||||
</Menu>
|
|
||||||
</FocusTrap>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
</>
|
|
||||||
}
|
}
|
||||||
/>
|
>
|
||||||
|
{image.usage && getUsageStr(image.usage) !== getUsageStr(packUsage) && (
|
||||||
|
<Box>
|
||||||
|
<Badge variant="Secondary" size="400" radii="300" outlined>
|
||||||
|
<Text size="L400">{getUsageStr(image.usage)}</Text>
|
||||||
|
</Badge>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</SettingTile>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
import React, { useMemo } from 'react';
|
|
||||||
import { Box, config, MenuItem, Text } from 'folds';
|
|
||||||
import { ImageUsage } from '../../plugins/custom-emoji';
|
|
||||||
|
|
||||||
export const useUsageStr = (): ((usage: ImageUsage[]) => string) => {
|
|
||||||
const getUsageStr = (usage: ImageUsage[]): string => {
|
|
||||||
const sticker = usage.includes(ImageUsage.Sticker);
|
|
||||||
const emoticon = usage.includes(ImageUsage.Emoticon);
|
|
||||||
|
|
||||||
if (sticker && emoticon) return 'Both';
|
|
||||||
if (sticker) return 'Sticker';
|
|
||||||
if (emoticon) return 'Emoji';
|
|
||||||
return 'Both';
|
|
||||||
};
|
|
||||||
return getUsageStr;
|
|
||||||
};
|
|
||||||
|
|
||||||
type UsageSelectorProps = {
|
|
||||||
selected: ImageUsage[];
|
|
||||||
onChange: (usage: ImageUsage[]) => void;
|
|
||||||
};
|
|
||||||
export function UsageSelector({ selected, onChange }: UsageSelectorProps) {
|
|
||||||
const getUsageStr = useUsageStr();
|
|
||||||
|
|
||||||
const selectedUsageStr = getUsageStr(selected);
|
|
||||||
const isSelected = (usage: ImageUsage[]) => getUsageStr(usage) === selectedUsageStr;
|
|
||||||
|
|
||||||
const allUsages: ImageUsage[][] = useMemo(
|
|
||||||
() => [[ImageUsage.Emoticon], [ImageUsage.Sticker], [ImageUsage.Sticker, ImageUsage.Emoticon]],
|
|
||||||
[]
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Box direction="Column" gap="100" style={{ padding: config.space.S100 }}>
|
|
||||||
{allUsages.map((usage) => (
|
|
||||||
<MenuItem
|
|
||||||
key={getUsageStr(usage)}
|
|
||||||
size="300"
|
|
||||||
variant={isSelected(usage) ? 'SurfaceVariant' : 'Surface'}
|
|
||||||
aria-selected={isSelected(usage)}
|
|
||||||
radii="300"
|
|
||||||
onClick={() => onChange(usage)}
|
|
||||||
>
|
|
||||||
<Box grow="Yes">
|
|
||||||
<Text size="T300">{getUsageStr(usage)}</Text>
|
|
||||||
</Box>
|
|
||||||
</MenuItem>
|
|
||||||
))}
|
|
||||||
</Box>
|
|
||||||
);
|
|
||||||
}
|
|
115
src/app/components/image-pack-view/UsageSwitcher.tsx
Normal file
115
src/app/components/image-pack-view/UsageSwitcher.tsx
Normal file
|
@ -0,0 +1,115 @@
|
||||||
|
import React, { MouseEventHandler, useMemo, useState } from 'react';
|
||||||
|
import { Box, Button, config, Icon, Icons, Menu, MenuItem, PopOut, RectCords, Text } from 'folds';
|
||||||
|
import FocusTrap from 'focus-trap-react';
|
||||||
|
import { ImageUsage } from '../../plugins/custom-emoji';
|
||||||
|
import { stopPropagation } from '../../utils/keyboard';
|
||||||
|
|
||||||
|
export const useUsageStr = (): ((usage: ImageUsage[]) => string) => {
|
||||||
|
const getUsageStr = (usage: ImageUsage[]): string => {
|
||||||
|
const sticker = usage.includes(ImageUsage.Sticker);
|
||||||
|
const emoticon = usage.includes(ImageUsage.Emoticon);
|
||||||
|
|
||||||
|
if (sticker && emoticon) return 'Emoji & Sticker';
|
||||||
|
if (sticker) return 'Sticker';
|
||||||
|
if (emoticon) return 'Emoji';
|
||||||
|
return 'Both';
|
||||||
|
};
|
||||||
|
return getUsageStr;
|
||||||
|
};
|
||||||
|
|
||||||
|
type UsageSelectorProps = {
|
||||||
|
selected: ImageUsage[];
|
||||||
|
onChange: (usage: ImageUsage[]) => void;
|
||||||
|
};
|
||||||
|
export function UsageSelector({ selected, onChange }: UsageSelectorProps) {
|
||||||
|
const getUsageStr = useUsageStr();
|
||||||
|
|
||||||
|
const selectedUsageStr = getUsageStr(selected);
|
||||||
|
const isSelected = (usage: ImageUsage[]) => getUsageStr(usage) === selectedUsageStr;
|
||||||
|
|
||||||
|
const allUsages: ImageUsage[][] = useMemo(
|
||||||
|
() => [[ImageUsage.Emoticon], [ImageUsage.Sticker], [ImageUsage.Sticker, ImageUsage.Emoticon]],
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Box direction="Column" gap="100" style={{ padding: config.space.S100 }}>
|
||||||
|
{allUsages.map((usage) => (
|
||||||
|
<MenuItem
|
||||||
|
key={getUsageStr(usage)}
|
||||||
|
size="300"
|
||||||
|
variant={isSelected(usage) ? 'SurfaceVariant' : 'Surface'}
|
||||||
|
aria-selected={isSelected(usage)}
|
||||||
|
radii="300"
|
||||||
|
onClick={() => onChange(usage)}
|
||||||
|
>
|
||||||
|
<Box grow="Yes">
|
||||||
|
<Text size="T300">{getUsageStr(usage)}</Text>
|
||||||
|
</Box>
|
||||||
|
</MenuItem>
|
||||||
|
))}
|
||||||
|
</Box>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
type UsageSwitcherProps = {
|
||||||
|
usage: ImageUsage[];
|
||||||
|
canEdit?: boolean;
|
||||||
|
onChange: (usage: ImageUsage[]) => void;
|
||||||
|
};
|
||||||
|
export function UsageSwitcher({ usage, onChange, canEdit }: UsageSwitcherProps) {
|
||||||
|
const getUsageStr = useUsageStr();
|
||||||
|
|
||||||
|
const [menuCords, setMenuCords] = useState<RectCords>();
|
||||||
|
|
||||||
|
const handleSelectUsage: MouseEventHandler<HTMLButtonElement> = (event) => {
|
||||||
|
setMenuCords(event.currentTarget.getBoundingClientRect());
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Button
|
||||||
|
variant="Secondary"
|
||||||
|
fill="Soft"
|
||||||
|
size="300"
|
||||||
|
radii="300"
|
||||||
|
outlined
|
||||||
|
aria-disabled={!canEdit}
|
||||||
|
after={canEdit && <Icon src={Icons.ChevronBottom} size="100" />}
|
||||||
|
onClick={canEdit ? handleSelectUsage : undefined}
|
||||||
|
>
|
||||||
|
<Text size="B300">{getUsageStr(usage)}</Text>
|
||||||
|
</Button>
|
||||||
|
<PopOut
|
||||||
|
anchor={menuCords}
|
||||||
|
offset={5}
|
||||||
|
position="Bottom"
|
||||||
|
align="End"
|
||||||
|
content={
|
||||||
|
<FocusTrap
|
||||||
|
focusTrapOptions={{
|
||||||
|
initialFocus: false,
|
||||||
|
onDeactivate: () => setMenuCords(undefined),
|
||||||
|
clickOutsideDeactivates: true,
|
||||||
|
isKeyForward: (evt: KeyboardEvent) =>
|
||||||
|
evt.key === 'ArrowDown' || evt.key === 'ArrowRight',
|
||||||
|
isKeyBackward: (evt: KeyboardEvent) =>
|
||||||
|
evt.key === 'ArrowUp' || evt.key === 'ArrowLeft',
|
||||||
|
escapeDeactivates: stopPropagation,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Menu>
|
||||||
|
<UsageSelector
|
||||||
|
selected={usage}
|
||||||
|
onChange={(usg) => {
|
||||||
|
setMenuCords(undefined);
|
||||||
|
onChange(usg);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Menu>
|
||||||
|
</FocusTrap>
|
||||||
|
}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import { style } from '@vanilla-extract/css';
|
import { style } from '@vanilla-extract/css';
|
||||||
import { DefaultReset, toRem } from 'folds';
|
import { config, DefaultReset, toRem } from 'folds';
|
||||||
|
|
||||||
export const ImagePackImage = style([
|
export const ImagePackImage = style([
|
||||||
DefaultReset,
|
DefaultReset,
|
||||||
|
@ -9,3 +9,13 @@ export const ImagePackImage = style([
|
||||||
objectFit: 'contain',
|
objectFit: 'contain',
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
export const UnsavedMenu = style({
|
||||||
|
position: 'sticky',
|
||||||
|
padding: config.space.S200,
|
||||||
|
paddingLeft: config.space.S400,
|
||||||
|
top: config.space.S400,
|
||||||
|
left: config.space.S400,
|
||||||
|
right: 0,
|
||||||
|
zIndex: 1,
|
||||||
|
});
|
||||||
|
|
|
@ -38,4 +38,8 @@ export class PackMetaReader {
|
||||||
|
|
||||||
return knownUsage;
|
return knownUsage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get content(): PackMeta {
|
||||||
|
return this.meta;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,17 @@ import { EmoteRoomsContent } from './types';
|
||||||
import { StateEvent } from '../../../types/matrix/room';
|
import { StateEvent } from '../../../types/matrix/room';
|
||||||
import { getAccountData, getStateEvents } from '../../utils/room';
|
import { getAccountData, getStateEvents } from '../../utils/room';
|
||||||
import { AccountDataEvent } from '../../../types/matrix/accountData';
|
import { AccountDataEvent } from '../../../types/matrix/accountData';
|
||||||
|
import { PackMetaReader } from './PackMetaReader';
|
||||||
|
|
||||||
|
export function packMetaEqual(a: PackMetaReader, b: PackMetaReader): boolean {
|
||||||
|
return (
|
||||||
|
a.name === b.name &&
|
||||||
|
a.avatar === b.avatar &&
|
||||||
|
a.attribution === b.attribution &&
|
||||||
|
a.usage.length === b.usage.length &&
|
||||||
|
a.usage.every((u) => b.usage.includes(u))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function makeImagePacks(packEvents: MatrixEvent[]): ImagePack[] {
|
export function makeImagePacks(packEvents: MatrixEvent[]): ImagePack[] {
|
||||||
return packEvents.reduce<ImagePack[]>((imagePacks, packEvent) => {
|
return packEvents.reduce<ImagePack[]>((imagePacks, packEvent) => {
|
||||||
|
|
Loading…
Reference in a new issue