import type { ContextModalProps } from "@mantine/modals"; import { type ReactNode, useEffect, useMemo, useState } from "react"; import { useEntityQuery } from "ui/client"; import { type FileState, Media } from "ui/elements"; import { autoFormatString, datetimeStringLocal, formatNumber } from "core/utils"; import { twMerge } from "tailwind-merge"; import { IconButton } from "ui/components/buttons/IconButton"; import { TbCheck, TbCopy } from "react-icons/tb"; import { useClipboard } from "@mantine/hooks"; import { ButtonLink } from "ui/components/buttons/Button"; import { routes } from "ui/lib/routes"; import { useBkndMedia } from "ui/client/schema/media/use-bknd-media"; import { JsonViewer } from "ui"; export type MediaInfoModalProps = { file: FileState; }; export function MediaInfoModal({ context, id, innerProps: { file }, }: ContextModalProps) { const { config: { entity_name, basepath }, } = useBkndMedia(); const $q = useEntityQuery(entity_name as "media", undefined, { where: { path: file.path, }, }); const close = () => context.closeModal(id); const data = $q.data?.[0]; const origin = window.location.origin; const entity = data?.reference ? data?.reference.split(".")[0] : undefined; const entityUrl = entity ? "/data" + routes.data.entity.edit(entity, data?.entity_id!) : undefined; const mediaUrl = data?.path ? "/data" + routes.data.entity.edit(entity_name, data?.id!) : undefined; //const assetUrl = data?.path ? origin + basepath + "/file/" + data?.path : undefined; return (
{mediaUrl && ( #{String(data?.id)} )} {entityUrl && ( {data?.reference} #{data?.entity_id} )} {data?.metadata && ( )}
); } const Item = ({ title, children, value, first, copyable = true, copyValue, }: { title: string; children?: ReactNode; value?: any; first?: boolean; copyable?: boolean; copyValue?: any; }) => { const cb = useClipboard(); const is_null = !children && (value === null || typeof value === "undefined"); const can_copy = copyable && !is_null && cb.copy !== undefined; const _value = value ? typeof value === "object" && !is_null ? JSON.stringify(value) : String(value) : undefined; return (
{autoFormatString(title)}
{children ?? (
{is_null ? "null" : _value}
)} {can_copy && ( cb.copy(copyValue ? copyValue : value)} /> )}
); }; const textFormats = [/^text\/.*$/, /application\/(x\-)?(json|json|yaml|javascript|xml|rtf|sql)/]; const FilePreview = ({ file }: { file: FileState }) => { const objectUrl = typeof file.body === "string" ? file.body : URL.createObjectURL(file.body); if (file.type.startsWith("image/") || file.type.startsWith("video/")) { // @ts-ignore return ; } if (file.type === "application/pdf") { // use browser preview return (