Skip to content

Commit

Permalink
Introduce Media Gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Nov 27, 2024
1 parent 6707432 commit aa390da
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
25 changes: 22 additions & 3 deletions web/src/api/media.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import { useHttp } from './core';
import { useQuery } from '@tanstack/react-query';

type MediaResponse = {
id: number;
description: string;
url: string;
};

export const useMedia = (id: string) =>
useHttp<MediaResponse>('/api/media/' + id);
export const useMedia = (id: number) =>
// useHttp<MediaResponse>('/api/media/' + id);
{
return useQuery({
queryKey: ['media', id],
queryFn: () => {
return {
1: {
id: 1,
description: 'test',
url: '/test.webp',
},
2: {
id: 2,
description: 'test2',
url: '/test.stl',
},
}[id];
},
});
};
4 changes: 2 additions & 2 deletions web/src/components/media/MediaGallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { MediaPreview } from './MediaPreview';

export const MediaGallery: FC<{ media_ids: number[] }> = ({ media_ids }) => {
return (
<div className="p-4">
<div className="flex items-stretch gap-2">
<div className="p-4 w-full">
<div className="flex flex-row items-stretch overflow-x-auto w-full gap-2">
{media_ids.map((media_id) => (
<MediaPreview media_id={media_id} />
))}
Expand Down
35 changes: 32 additions & 3 deletions web/src/components/media/MediaPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { FC } from 'react';
import { match } from 'ts-pattern';

import { useMedia } from '../../api/media';
import { StlPreview } from '../stl_preview/StlPreview';

export const MediaPreview: FC<{ media_id: number }> = ({ media_id }) => {
const { data: media } = useMedia(media_id);

const fileType = media?.url.split('.').pop();

return (
<div className="aspect-video bg-neutral-100 max-w-md w-full border border-neutral-200 rounded-md">
{match(fileType)
.with('webp', () => <ImagePreview media_id={media_id} />)
.with('stl', () => <StlPreview stlUrl={media?.url} />)
.otherwise(() => (
<div className="p-3">
<span>Unknown file type</span>
<span>{fileType}</span>
</div>
))}
</div>
);
};

export const ImagePreview: FC<{ media_id: number }> = ({ media_id }) => {
const { data: media } = useMedia(media_id);

return (
<div className="aspect-video bg-neutral-100">
MediaPreview
{media_id}
<div className="w-full h-full">
<img
src={media?.url}
alt={media?.description}
className="w-full h-full object-contain"
/>
</div>
);
};
2 changes: 1 addition & 1 deletion web/src/components/stl_preview/StlPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export const StlPreview = ({ stlUrl }: { stlUrl?: string }) => {
};

return (
<div style={{ width: '100%', height: '500px' }}>
<div style={{ width: '100%', height: '100%' }}>
<Canvas
shadows
camera={{
Expand Down

0 comments on commit aa390da

Please sign in to comment.