diff --git a/packages/frontend/src/routes/Piece.tsx b/packages/frontend/src/routes/Piece.tsx
index 29d8c12..2a6baf8 100644
--- a/packages/frontend/src/routes/Piece.tsx
+++ b/packages/frontend/src/routes/Piece.tsx
@@ -1,8 +1,8 @@
import type { Attachment, Piece } from "backend/database";
-import { AttachmentId, PieceId } from "common";
+import { PieceId } from "common";
import { ACCEPTED_EXTENSIONS } from "common/MediaType";
import { ELYSIA_FORM_DATA } from "elysia";
-import { FormEventHandler, useId, useReducer, useRef, useState } from "react";
+import { FormEventHandler, MouseEvent, useCallback, useId, useReducer, useRef, useState } from "react";
import { Link, useParams } from "react-router-dom";
import { client } from "../client";
import { FileReducer } from "../FileReducer";
@@ -125,11 +125,36 @@ namespace Attachments {
}
function Attachments(props: Attachments.Props) {
+ return (
+
+
+
+ | Nazwa pliku |
+ Typ |
+ Dodano |
+ Zmodyfikowano |
+ Pobierz |
+
+
+
+ {props.attachments.map((attachment) => )}
+
+
+ );
+}
- const download = (attachmentId: AttachmentId) => async () => {
+namespace AttachmentRow {
+ export interface Props {
+ readonly attachment: Attachment;
+ }
+}
+
+function AttachmentRow(props: AttachmentRow.Props) {
+
+ const download = useCallback(async () => {
const { error, data: _data } = await client
- .piece({ pieceId: props.pieceId })
- .attachment({ attachmentId })
+ .piece({ pieceId: props.attachment.pieceId })
+ .attachment({ attachmentId: props.attachment.attachmentId })
.get();
if (error !== null) {
@@ -144,52 +169,64 @@ function Attachments(props: Attachments.Props) {
a.download = data.filename;
a.click();
URL.revokeObjectURL(url);
- };
+ }, [props.attachment.attachmentId, props.attachment.pieceId]);
+
+ const open = useCallback(async () => {
+ const { error, data: _data } = await client
+ .piece({ pieceId: props.attachment.pieceId })
+ .attachment({ attachmentId: props.attachment.attachmentId })
+ .get();
+
+ if (error !== null) {
+ console.error(error.value);
+ return;
+ }
+
+ const data = _data as unknown as typeof _data[ELYSIA_FORM_DATA];
+ const url = URL.createObjectURL(data.data);
+ window.open(url, "_target");
+ URL.revokeObjectURL(url);
+ }, [props.attachment.attachmentId, props.attachment.pieceId, props.attachment.mediaType]);
+
+ const onOpen = useCallback((event: MouseEvent) => {
+ if (props.attachment.mediaType !== "application/pdf") {
+ return;
+ }
+
+ event.preventDefault();
+ open();
+ }, [props.attachment.mediaType, open]);
return (
-
-
-
- | Nazwa pliku |
- Typ |
- Dodano |
- Zmodyfikowano |
- Pobierz |
-
-
-
- {props.attachments.map((attachment) => (
-
- |
- {attachment.mediaType === "application/vnd.recordare.musicxml"
- || attachment.mediaType === "application/vnd.recordare.musicxml+xml" ? (
-
- {attachment.filename}
-
- ) : (
- attachment.filename
- )}
- |
- {attachment.mediaType} |
-
- {attachment.createdAt}
- {attachment.createdBy !== null && <> przez {attachment.createdBy}>}
- |
-
- {attachment.modifiedAt === null && attachment.modifiedBy === null ? "\u2014"
- : attachment.modifiedAt !== null && attachment.modifiedBy === null ? attachment.modifiedAt
- : attachment.modifiedAt === null ? `przez ${attachment.createdBy}`
- : <>{attachment.createdAt} przez {attachment.createdBy}>}
- |
-
-
- |
-
- ))}
-
-
+
+ |
+ {props.attachment.mediaType === "application/vnd.recordare.musicxml"
+ || props.attachment.mediaType === "application/vnd.recordare.musicxml+xml"
+ || props.attachment.mediaType === "application/pdf" ? (
+
+ {props.attachment.filename}
+
+ ) : (
+ props.attachment.filename
+ )}
+ |
+ {props.attachment.mediaType} |
+
+ {props.attachment.createdAt}
+ {props.attachment.createdBy !== null && <> przez {props.attachment.createdBy}>}
+ |
+
+ {props.attachment.modifiedAt === null && props.attachment.modifiedBy === null ? "\u2014"
+ : props.attachment.modifiedAt !== null && props.attachment.modifiedBy === null ? props.attachment.modifiedAt
+ : props.attachment.modifiedAt === null ? `przez ${props.attachment.createdBy}`
+ : <>{props.attachment.createdAt} przez {props.attachment.createdBy}>}
+ |
+
+
+ |
+
);
}