Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Image gallery #5

Merged
merged 5 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"clsx": "^2.1.1",
"cmdk": "^1.0.4",
"eslint-plugin-tailwindcss": "^3.15.1",
"embla-carousel": "^8.5.1",
"embla-carousel-auto-height": "^8.5.1",
"embla-carousel-react": "^8.5.1",
"eslint-plugin-unused-imports": "^4.0.1",
"fuse.js": "^7.0.0",
"globals": "^15.8.0",
Expand Down
51 changes: 47 additions & 4 deletions web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions web/src/components/media/MediaCarousel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import '../../embla.css';

import AutoHeight from 'embla-carousel-auto-height';
import useEmblaCarousel from 'embla-carousel-react';
import { Dispatch, FC, SetStateAction, useEffect } from 'react';
import { FaArrowLeft, FaArrowRight } from 'react-icons/fa6';

import { MediaMetaData } from './MediaMetaData';
import { BareMediaPreview } from './MediaPreview';

export const MediaCarousel: FC<{
ids: number[];
mediaId: number;
setMediaId: Dispatch<SetStateAction<number | undefined>>;
}> = ({ ids, mediaId, setMediaId }) => {
const [emblaReference, emblaApi] = useEmblaCarousel(
{
loop: true,
},
[AutoHeight()]
);

emblaApi?.on('select', () => {
const selected = emblaApi.selectedScrollSnap();

if (selected !== undefined) {
setMediaId(ids[selected]);
}
});

useEffect(() => {
if (mediaId !== undefined) {
const index = ids.indexOf(mediaId);

// TODO: If this useEffect wasn't triggered by emblaApi.on('select'),
// then we should set the scrollTo(index, true) boolean to true to not animate the scroll
emblaApi?.scrollTo(index);
}
});

useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'ArrowLeft') {
emblaApi?.scrollPrev();
} else if (event.key === 'ArrowRight') {
emblaApi?.scrollNext();
}
};

document.addEventListener('keydown', handleKeyDown);

return () => {
document.removeEventListener('keydown', handleKeyDown);
};
});

return (
<div className="flex flex-col md:flex-row">
<div ref={emblaReference} className="embla overflow-hidden">
<div className="embla__container">
{ids.map((id) => (
<div
key={id}
className="embla__slide select-none rounded-lg"
draggable={false}
>
<BareMediaPreview media_id={id} />
</div>
))}
</div>
</div>
<div className="min-w-64 p-4">
<code>
<MediaMetaData mediaId={mediaId} />
</code>
<div>
<button
className="gap-x-2 rounded-lg bg-gray-200 p-2 px-2 text-black disabled:opacity-50"
onClick={() => {
emblaApi?.scrollPrev();
}}
>
<FaArrowLeft />
</button>
<button
className="gap-x-2 rounded-lg bg-gray-200 p-2 px-2 text-black disabled:opacity-50"
onClick={() => {
emblaApi?.scrollNext();
}}
>
<FaArrowRight />
</button>
</div>
</div>
</div>
);
};
57 changes: 45 additions & 12 deletions web/src/components/media/MediaGallery.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
import { FC } from 'react';
'use client';

import * as Dialog from '@radix-ui/react-dialog';
import { FC, useState } from 'react';

import { useMedia } from '@/api/media';

import { MediaCarousel } from './MediaCarousel';
import { MediaPreview } from './MediaPreview';

export const MediaGallery: FC<{
media_ids: number[];
}> = ({ media_ids }) => {
const [mediaId, setMediaId] = useState<number | undefined>();

return (
<div className="card flex items-stretch">
{media_ids.length > 0 ? (
<div className="grid w-full grid-flow-row grid-cols-1 gap-2 md:grid-cols-2">
{media_ids.map((media_id) => (
<MediaPreview media_id={media_id} key={media_id} />
))}
</div>
) : (
<div className="flex h-full w-full items-center justify-center text-center">
No media
</div>
)}
<Dialog.Root modal={true}>
{media_ids.length > 0 ? (
<div className="grid w-full grid-flow-row grid-cols-1 gap-2 md:grid-cols-2">
{media_ids.map((media_id) => (
<Dialog.Trigger
key={media_id}
onClick={() => {
setMediaId(media_id);
}}
>
<MediaPreview
media_id={media_id}
key={media_id}
/>
</Dialog.Trigger>
))}
</div>
) : (
<div className="flex h-full w-full items-center justify-center text-center">
No media
</div>
)}
<Dialog.Portal>
<Dialog.Overlay className="fixed inset-0 z-10 bg-black bg-opacity-50" />
<Dialog.Content className="fixed inset-0 z-10 mx-auto my-8 h-min max-w-6xl rounded-lg bg-white shadow-lg">
<Dialog.DialogTitle className="overflow-hidden p-2 text-center text-xl font-medium">
{useMedia(mediaId).data?.description}
</Dialog.DialogTitle>
<MediaCarousel
ids={media_ids}
mediaId={mediaId!}
setMediaId={setMediaId}
/>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
</div>
);
};
30 changes: 30 additions & 0 deletions web/src/components/media/MediaMetaData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import TimeAgo from 'javascript-time-ago';
import en from 'javascript-time-ago/locale/en';
import { FC } from 'react';

import { useMedia } from '@/api/media';

import { BaseInput } from '../input/BaseInput';

TimeAgo.addDefaultLocale(en);
const timeAgo = new TimeAgo('en-US');

export const MediaMetaData: FC<{
mediaId: number;
}> = ({ mediaId }) => {
const media = useMedia(mediaId).data;

return (
<>
<BaseInput
label="Type"
value={media?.kind}
disabled
/>
<span>Created: {timeAgo.format(new Date(media?.created_at||''))}</span>
{media?.updated_at !== media?.created_at && (
<span>Updated: {timeAgo.format(new Date(media?.updated_at||''))}</span>
)}
</>
);
};
88 changes: 60 additions & 28 deletions web/src/components/media/MediaPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,64 @@ import { ErrorBoundary } from '@/components/ErrorBoundary';

import { Button } from '../ui/Button';

export const BareMediaPreview: FC<{
media_id?: number;
}> = ({ media_id }) => {
const { data: instanceSettings } = useInstanceSettings();
const { data: media } = useMedia(media_id);

const mediaUrl = (() => {
const link = media?.url;

if (link?.includes(':')) {
return link;
}

if (!instanceSettings) {
return;
}

return (
instanceSettings.modules.storage.endpoint_url +
'/' +
instanceSettings.modules.storage.bucket +
'/' +
link
);
})();

return (
<div className="relative bg-neutral-100 w-full aspect-video rounded-md">
{match(media?.kind)
.with(
'webp',
'image/webp',
'png',
'image/png',
'svg',
'image/svg+xml',
'jpeg',
'jpg',
'image/jpeg',
'image/gif',
() => <ImagePreview media_id={media_id} url={mediaUrl} />
)
.with('mp4', 'video/mp4', () => (
<VideoPreview media_id={media_id} url={mediaUrl} />
))
.with('stl', 'model/stl', () => (
<StlPreview media_id={media_id} url={mediaUrl} />
))
.otherwise(() => (
<div className="p-3 border-orange-500 border-2 rounded-md bg-orange-100 h-full">
<span>Unknown file type</span>
<span>{media?.kind}</span>
</div>
))}
</div>
);
};

export const MediaPreview: FC<{
variant?: 'small' | 'default';
media_id?: number;
Expand Down Expand Up @@ -109,7 +167,7 @@ export const MediaPreview: FC<{
return (
<div
className={clsx(
'relative aspect-video w-full max-w-md rounded-md bg-neutral-100',
'relative aspect-video bg-neutral-100 max-w-md w-full rounded-md h-fit',
status == 'new-media' &&
isPending &&
'border-2 border-blue-400',
Expand All @@ -120,33 +178,7 @@ export const MediaPreview: FC<{
status == 'removed-media' && 'border-2 border-red-400'
)}
>
{match(fileType)
.with(
'webp',
'image/webp',
'png',
'image/png',
'svg',
'image/svg+xml',
'jpeg',
'jpg',
'image/jpeg',
'image/gif',
'gif',
() => <ImagePreview media_id={media_id} url={mediaUrl} />
)
.with('mp4', 'video/mp4', () => (
<VideoPreview media_id={media_id} url={mediaUrl} />
))
.with('stl', 'model/stl', () => (
<StlPreview media_id={media_id} url={mediaUrl} />
))
.otherwise(() => (
<div className="h-full rounded-md border-2 border-orange-500 bg-orange-100 p-3">
<span>Unknown file type</span>
<span>{fileType}</span>
</div>
))}
<BareMediaPreview media_id={media_id} />
{isPending && (
<div className="mt-1 flex w-full items-center justify-center gap-2 border-t-2 border-t-inherit">
Uploading... <FiLoader className="animate-spin" />
Expand Down
Loading
Loading