Creating global FilePreview.

This commit is contained in:
Roland 2023-02-15 20:58:07 -07:00
parent a2e1cfb0eb
commit a82917fdf7
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
11 changed files with 137 additions and 150 deletions

View file

@ -133,3 +133,46 @@ td.file-path {
top: 20px;
right: 20px;
}
textarea.otp {
width: 100%;
color: #333;
}
.inline-header h1 {
display: inline-block;
}
.inline-header h2 {
display: inline-block;
}
.inline-header h3 {
display: inline-block;
}
.inline-header h4 {
display: inline-block;
}
.inline-header h5 {
display: inline-block;
}
.inline-header span {
margin-left: 1em;
}
.card-icon {
min-height: 163px;
text-align: center;
padding-top: 53px;
}
.card-icon.inverted {
background-color: #222222;
}
.clickable {
cursor: pointer;
}

View file

@ -18,6 +18,7 @@ import {Dashboard} from "./Dashboard";
import {Donate} from "./components/Donate";
import {useEventsInterval} from "./Events";
import {SemanticToastContainer} from "react-semantic-toasts-2";
import {FilePreviewWrapper} from "./components/FilePreview";
function PageNotFound() {
const {t} = useContext(ThemeContext);
@ -78,13 +79,16 @@ export default function App() {
useEventsInterval();
return <ThemeWrapper>
{/* Context and style to handle switching between mobile/computer. */}
<style>{mediaStyles}</style>
<SemanticToastContainer position='top-right'/>
<MediaContextProvider>
<StatusContext.Provider value={status}>
<RouterProvider router={router}/>
</StatusContext.Provider>
</MediaContextProvider>
<FilePreviewWrapper>
{/* Context and style to handle switching between mobile/computer. */}
<style>{mediaStyles}</style>
{/* Toasts can be on any page. */}
<SemanticToastContainer position='top-right'/>
<MediaContextProvider>
<StatusContext.Provider value={status}>
<RouterProvider router={router}/>
</StatusContext.Provider>
</MediaContextProvider>
</FilePreviewWrapper>
</ThemeWrapper>
}

View file

@ -1,7 +1,6 @@
import React, {useContext} from 'react';
import {Divider, SegmentGroup, StatisticLabel, StatisticValue} from "semantic-ui-react";
import {Link, Route, Routes} from "react-router-dom";
import "../static/wrolpi.css";
import {decryptOTP, encryptOTP} from "../api";
import {humanFileSize, mimetypeColor, PageContainer, toLocaleString, useTitle} from "./Common";
import {ThemeContext} from "../contexts/contexts";

View file

@ -23,7 +23,7 @@ import {
HelpHeader,
isoDatetimeToString,
mimetypeColor,
PageContainer,
PageContainer, PreviewLink,
SearchInput,
TabLinks,
textEllipsis,

View file

@ -5,6 +5,7 @@ import Message from "semantic-ui-react/dist/commonjs/collections/Message";
import {useDirectories, useDownloaders, useHotspot, useSettings, useThrottle} from "../hooks/customHooks";
import {Media, StatusContext, ThemeContext} from "../contexts/contexts";
import {Button, darkTheme, Form, Header, Icon, lightTheme, Menu, Popup, Statistic} from "./Theme";
import {FilePreviewContext} from "./FilePreview";
export const API_URI = process.env && process.env.REACT_APP_API_URI ? process.env.REACT_APP_API_URI : `${window.location.protocol}//${window.location.host}/api`;
export const VIDEOS_API = `${API_URI}/videos`;
@ -141,6 +142,15 @@ export function ExternalCardLink({to, ...props}) {
</a>
}
export function PreviewLink({file, children, className, ...props}) {
const {t} = useContext(ThemeContext);
const {setPreviewPath} = React.useContext(FilePreviewContext);
className = className ? `clickable ${className}` : `clickable `;
return <div className={className} onClick={() => setPreviewPath(file)} {...props} {...t}>
{children}
</div>
}
export function RequiredAsterisk() {
return <span style={{color: '#db2828'}}> *</span>
}
@ -436,14 +446,16 @@ export function CardPosterLink({to, poster_url, imageLabel, external = false}) {
style={{maxHeight: '163px', width: 'auto'}}
/>;
if (external === true) {
return <a href={to} target='_blank' style={style}>
return <a href={to} target='_blank' rel='noreferrer' style={style}>
{image}
</a>
} else {
} else if (to) {
// Link using React Router.
return <Link to={to} style={style}>
{image}
</Link>
} else {
return <div style={style}>{image}</div>;
}
}

View file

@ -1,18 +1,5 @@
import {Button, Icon, Placeholder} from "./Theme";
import {
Button as SButton,
Checkbox,
Confirm,
Image,
Modal,
ModalActions,
ModalContent,
PlaceholderLine,
TableCell,
TableFooter,
TableHeaderCell,
TableRow
} from "semantic-ui-react";
import {Checkbox, Confirm, PlaceholderLine, TableCell, TableFooter, TableHeaderCell, TableRow} from "semantic-ui-react";
import {FileIcon, humanFileSize} from "./Common";
import React from "react";
import {deleteFile} from "../api";
@ -20,7 +7,7 @@ import _ from 'lodash';
import {SortableTable} from "./SortableTable";
import {useBrowseFiles} from "../hooks/customHooks";
import {DirectoryRefreshButton, FilesRefreshButton} from "./Files";
import {useFilePreview} from "./FilePreview";
import {FilePreviewContext} from "./FilePreview";
function depthIndentation(path) {
// Repeated spaces for every folder a path is in.
@ -113,7 +100,7 @@ export function FileBrowser() {
key: 'path', text: 'Path', sortBy: i => i['path'].toLowerCase()
}, {key: 'size', text: 'Size', sortBy: 'size'},];
const {modal, setPath} = useFilePreview();
const {setPreviewPath} = React.useContext(FilePreviewContext);
const onSelect = (path) => {
if (path.endsWith('/') && selectedPath === path) {
@ -198,13 +185,12 @@ export function FileBrowser() {
key={i['key']}
path={i}
onFolderClick={onFolderClick}
onFileClick={(i) => setPath(i)}
onFileClick={(i) => setPreviewPath(i)}
sortData={sortData}
selectedPath={selectedPath}
onSelect={onSelect}
/>}
footer={footer}
/>
{modal}
</>
}

View file

@ -1,4 +1,4 @@
import React, {useEffect} from "react";
import React from "react";
import _ from "lodash";
import {Button as SButton, Image, Modal, ModalActions, ModalContent} from "semantic-ui-react";
@ -11,7 +11,7 @@ function getDownloadPathURL(path) {
}
function getEpubViewerURL(path) {
return `/epub.html?url=${getDownloadPathURL(path)}`;
return `/epub.html?url=download/${encodeURIComponent(path['path'])}`;
}
function getIframeModal(path) {
@ -62,51 +62,64 @@ function getVideoModal(path) {
</ModalContent>
}
export function useFilePreview() {
const [path, setPath] = React.useState(null);
const [modal, setModal] = React.useState(null);
export const FilePreviewContext = React.createContext({
previewPath: null, setPreviewPath: null, previewModal: null, setPreviewModal: null,
});
export function FilePreviewWrapper({children}) {
const [previewPath, setPreviewPath] = React.useState(null);
const [previewModal, setPreviewModal] = React.useState(null);
const value = {
previewPath, setPreviewPath, previewModal, setPreviewModal,
};
function setModalContent(content, url, downloadURL) {
setModal(<Modal closeIcon
size='fullscreen'
open={true}
onClose={() => setModal(null)}
setPreviewModal(<Modal closeIcon
size='fullscreen'
open={true}
onClose={() => setPreviewModal(null)}
>
{content}
<ModalActions>
{downloadURL &&
<SButton color='yellow' onClick={() => window.open(downloadURL)} floated='left'>Download</SButton>}
<SButton color='blue' onClick={() => window.open(url)}>Open</SButton>
<SButton onClick={() => setPath(null)}>Close</SButton>
<SButton onClick={() => setPreviewPath(null)}>Close</SButton>
</ModalActions>
</Modal>);
}
useEffect(() => {
setModal(null);
if (path && !_.isEmpty(path)) {
const {mimetype, size} = path;
console.debug(`useFilePreview path=${path['path']} mimetype=${mimetype}`);
const url = getMediaPathURL(path);
const downloadURL = getDownloadPathURL(path);
React.useEffect(() => {
setPreviewModal(null);
if (previewPath && !_.isEmpty(previewPath)) {
const {mimetype, size} = previewPath;
console.debug(`useFilePreview path=${previewPath['path']} mimetype=${mimetype}`);
const url = getMediaPathURL(previewPath);
const downloadURL = getDownloadPathURL(previewPath);
if (mimetype.startsWith('text/') && size >= 10000) {
// Large text files should be downloaded.
window.open(downloadURL);
} else if (mimetype.startsWith('text/') || mimetype.startsWith('application/json')) {
setModalContent(getIframeModal(path), url, downloadURL);
setModalContent(getIframeModal(previewPath), url, downloadURL);
} else if (mimetype.startsWith('video/')) {
setModalContent(getVideoModal(path), url, downloadURL);
setModalContent(getVideoModal(previewPath), url, downloadURL);
} else if (mimetype.startsWith('application/epub')) {
const viewerURL = getEpubViewerURL(path);
setModalContent(getEpubModal(path), viewerURL, downloadURL);
const viewerURL = getEpubViewerURL(previewPath);
console.dir(previewPath);
console.log(viewerURL);
setModalContent(getEpubModal(previewPath), viewerURL, downloadURL);
} else if (mimetype.startsWith('image/')) {
setModalContent(getImageModal(path), url);
setModalContent(getImageModal(previewPath), url);
} else {
// No special handler for this file type, just open it.
window.open(downloadURL);
}
}
}, [path]);
}, [previewPath]);
return {modal, setPath};
return <FilePreviewContext.Provider value={value}>
{children}
{previewModal}
</FilePreviewContext.Provider>
}

View file

@ -1,15 +1,12 @@
import React, {useContext} from "react";
import {
Button as SButton,
CardContent,
CardDescription,
CardHeader,
CardMeta,
Container,
Dropdown,
Header as SHeader,
Image,
Modal,
PlaceholderLine,
TableCell,
TableHeaderCell,
@ -26,6 +23,7 @@ import {
mimetypeColor,
PageContainer,
Paginator,
PreviewLink,
textEllipsis,
useTitle
} from "./Common";
@ -42,6 +40,7 @@ import _ from 'lodash';
import {FileBrowser} from "./FileBrowser";
import {refreshDirectoryFiles, refreshFiles} from "../api";
import {useSubscribeEventName} from "../Events";
import {FilePreviewContext} from "./FilePreview";
export function FilesPage() {
@ -58,51 +57,19 @@ function EbookCard({file}) {
const isEpub = file['mimetype'].startsWith('application/epub');
const viewerUrl = isEpub ? `/epub.html?url=${downloadUrl}` : null;
let cover = <CardIcon><FileIcon file={file}/></CardIcon>;
let cover = <FileIcon file={file}/>;
if (ebook && ebook.cover_path) {
const coverSrc = `/media/${encodeURIComponent(ebook.cover_path)}`;
cover = <CardPosterLink to={viewerUrl || downloadUrl} poster_url={coverSrc} external={true}/>;
cover = <CardPosterLink poster_url={coverSrc}/>;
}
suffix = suffix ? _.trimStart(suffix, '.').toUpperCase() : null;
const downloadButton = <ExternalCardLink to={downloadUrl}>
<SButton
icon='download'
content='Download'
labelPosition='left'/>
</ExternalCardLink>;
const detailsButton = <Button
icon='file alternate'
content='Details'
labelPosition='left'
disabled={!ebook}
onClick={() => setDetailsOpen(true)}
/>;
const [detailsOpen, setDetailsOpen] = React.useState(false);
const detailsModal = <Modal closeIcon
open={detailsOpen}
onClose={() => setDetailsOpen(false)}
trigger={detailsButton}
>
<Modal.Header><SHeader as='h1'>{ebook.title || file.title}</SHeader></Modal.Header>
<Modal.Content>
<SHeader as='h3'>Author: {ebook.creator}</SHeader>
<p>{humanFileSize(ebook.size)}</p>
<SHeader as='h5'>eBook Path</SHeader>
<pre>{ebook ? ebook.ebook_path : file.path}</pre>
<SHeader as='h5'>Cover Path</SHeader>
<pre>{ebook && ebook.cover_path ? ebook.cover_path : 'N/A'}</pre>
</Modal.Content>
<Modal.Actions>
{downloadButton}
</Modal.Actions>
</Modal>;
const color = mimetypeColor(file.mimetype);
return <Card color={color}>
{cover}
<PreviewLink file={file}>
{cover}
</PreviewLink>
<CardContent {...s}>
<CardHeader>
<Container textAlign='left'>
@ -113,7 +80,6 @@ function EbookCard({file}) {
</CardHeader>
<CardMeta>
<pre {...s}>{suffix}</pre>
{detailsModal}
</CardMeta>
</CardContent>
</Card>
@ -122,21 +88,23 @@ function EbookCard({file}) {
function ImageCard({file}) {
const {s} = useContext(ThemeContext);
const {setPreviewPath} = React.useContext(FilePreviewContext);
const url = `/media/${encodeURIComponent(file.path)}`;
let poster = <CardIcon><FileIcon file={file}/></CardIcon>;
let poster = <FileIcon file={file}/>;
if (file.size && file.size < 50000000) {
// Image is less than 5mb, use it.
poster = <Image wrapped
src={url}
style={{position: 'relative', width: '100%'}}
/>;
onClick={() => setPreviewPath(file)}
/>
}
return <Card color={mimetypeColor(file.mimetype)}>
<ExternalCardLink to={url}>
<PreviewLink file={file}>
{poster}
</ExternalCardLink>
</PreviewLink>
<CardContent {...s}>
<CardHeader>
<ExternalCardLink to={url} className='no-link-underscore card-link'>
@ -170,19 +138,19 @@ function FileCard({file}) {
return <EbookCard key={file['path']} file={file}/>;
}
const url = `/download/${encodeURIComponent(file.path)}`;
const downloadUrl = `/download/${encodeURIComponent(file.path)}`;
const color = mimetypeColor(file.mimetype);
const size = file.size !== null && file.size !== undefined ? humanFileSize(file.size) : null;
return <Card color={color}>
<ExternalCardLink to={url}>
<PreviewLink file={file}>
<CardIcon>
<FileIcon file={file}/>
</CardIcon>
</ExternalCardLink>
</PreviewLink>
<CardContent {...s}>
<CardHeader>
<ExternalCardLink to={url}>
<ExternalCardLink to={downloadUrl}>
{cardTitleWrapper(file.title || file.stem || file.path)}
</ExternalCardLink>
</CardHeader>

View file

@ -1,5 +1,4 @@
import React, {useContext, useState} from 'react';
import "../static/wrolpi.css";
import {
deleteInventory,
deleteItems,

View file

@ -78,11 +78,6 @@ export function ThemeWrapper({children, ...props}) {
}
}
const setSystemTheme = () => {
console.debug('setSystemTheme');
saveTheme(systemTheme);
}
const saveTheme = (value) => {
setSavedTheme(value);
localStorage.setItem(themeSessionKey, value);
@ -141,7 +136,6 @@ export function ThemeWrapper({children, ...props}) {
savedTheme,
setDarkTheme,
setLightTheme,
setSystemTheme,
cycleSavedTheme,
};
@ -288,9 +282,16 @@ export function TextArea(props) {
return <TEXTAREA {...props}/>
}
export function CardIcon(props) {
export function CardIcon({onClick, ...props}) {
const {inverted} = useContext(ThemeContext);
return <center className={`card-icon ${inverted}`} {...props}/>
const cardIcon = <center className={`card-icon ${inverted}`} {...props}/>;
if (onClick) {
return <div onClick={onClick} className='clickable'>
{cardIcon}
</div>
} else {
return cardIcon;
}
}
export function Card({color, ...props}) {

View file

@ -1,38 +0,0 @@
textarea.otp {
width: 100%;
color: #333;
}
.inline-header h1 {
display: inline-block;
}
.inline-header h2 {
display: inline-block;
}
.inline-header h3 {
display: inline-block;
}
.inline-header h4 {
display: inline-block;
}
.inline-header h5 {
display: inline-block;
}
.inline-header span {
margin-left: 1em;
}
.card-icon {
min-height: 163px;
text-align: center;
padding-top: 53px;
}
.card-icon.inverted {
background-color: #222222;
}