2022-08-06 05:22:07 +00:00
|
|
|
import React, {useContext, useEffect, useState} from "react";
|
2023-07-21 15:14:09 -06:00
|
|
|
import {
|
2024-06-25 17:05:41 -06:00
|
|
|
BreadcrumbSection,
|
2023-07-21 15:14:09 -06:00
|
|
|
Button as SButton,
|
|
|
|
|
ButtonGroup,
|
|
|
|
|
Card,
|
|
|
|
|
Confirm,
|
|
|
|
|
Container,
|
2024-06-17 14:49:04 -06:00
|
|
|
Dimmer,
|
|
|
|
|
DimmerDimmable,
|
2023-08-23 20:14:23 -06:00
|
|
|
Icon as SIcon,
|
2023-07-21 15:14:09 -06:00
|
|
|
IconGroup,
|
|
|
|
|
Input,
|
|
|
|
|
Label,
|
|
|
|
|
Pagination,
|
|
|
|
|
Search,
|
|
|
|
|
Transition
|
|
|
|
|
} from 'semantic-ui-react';
|
2023-05-29 18:04:03 -06:00
|
|
|
import {Link, NavLink, useNavigate, useSearchParams} from "react-router-dom";
|
2022-01-12 09:38:21 -07:00
|
|
|
import Message from "semantic-ui-react/dist/commonjs/collections/Message";
|
2023-04-18 22:03:45 -06:00
|
|
|
import {
|
2023-08-13 10:54:43 -06:00
|
|
|
useCPUTemperature,
|
2023-04-18 22:03:45 -06:00
|
|
|
useHotspot,
|
2023-10-06 12:48:20 -06:00
|
|
|
useLoad,
|
2023-04-18 22:03:45 -06:00
|
|
|
useSearchDirectories,
|
|
|
|
|
useSearchOrder,
|
2023-07-21 15:14:09 -06:00
|
|
|
useThrottle,
|
|
|
|
|
useWROLMode
|
2023-04-18 22:03:45 -06:00
|
|
|
} from "../hooks/customHooks";
|
2024-02-19 13:45:41 -07:00
|
|
|
import {Media, SettingsContext, StatusContext, ThemeContext} from "../contexts/contexts";
|
2023-04-18 22:03:45 -06:00
|
|
|
import {
|
2024-06-25 17:05:41 -06:00
|
|
|
Breadcrumb,
|
|
|
|
|
BreadcrumbDivider,
|
2023-04-18 22:03:45 -06:00
|
|
|
Button,
|
|
|
|
|
CardIcon,
|
|
|
|
|
darkTheme,
|
|
|
|
|
Form,
|
|
|
|
|
Header,
|
|
|
|
|
Icon,
|
|
|
|
|
lightTheme,
|
2024-06-17 10:40:30 -06:00
|
|
|
Loader,
|
2023-04-18 22:03:45 -06:00
|
|
|
Menu,
|
|
|
|
|
Modal,
|
|
|
|
|
ModalActions,
|
|
|
|
|
ModalContent,
|
2023-05-29 18:04:03 -06:00
|
|
|
ModalDescription,
|
|
|
|
|
ModalHeader,
|
2023-04-18 22:03:45 -06:00
|
|
|
Popup,
|
2024-06-17 10:40:30 -06:00
|
|
|
Segment,
|
2023-04-18 22:03:45 -06:00
|
|
|
Statistic
|
|
|
|
|
} from "./Theme";
|
2023-02-15 20:58:07 -07:00
|
|
|
import {FilePreviewContext} from "./FilePreview";
|
2023-03-29 13:46:39 -06:00
|
|
|
import _ from "lodash";
|
2023-04-01 11:52:41 -06:00
|
|
|
import {killDownloads, startDownloads} from "../api";
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2024-06-12 18:44:59 -06:00
|
|
|
export const API_URI = process.env && process.env.REACT_APP_API_URI ? process.env.REACT_APP_API_URI : `https://${window.location.host}/api`;
|
2021-11-01 16:42:52 -06:00
|
|
|
export const VIDEOS_API = `${API_URI}/videos`;
|
|
|
|
|
export const ARCHIVES_API = `${API_URI}/archive`;
|
|
|
|
|
export const OTP_API = `${API_URI}/otp`;
|
2023-05-29 18:04:03 -06:00
|
|
|
export const ZIM_API = `${API_URI}/zim`;
|
2021-11-01 16:42:52 -06:00
|
|
|
export const DEFAULT_LIMIT = 20;
|
2022-10-25 15:57:44 -06:00
|
|
|
export const NAME = process.env && process.env.REACT_APP_NAME ? process.env.REACT_APP_NAME : null;
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2024-06-12 18:44:59 -06:00
|
|
|
// Other services on a WROLPi.
|
2024-08-15 19:32:01 -06:00
|
|
|
export const FILES_MEDIA_URI = `https://${window.location.hostname}:${window.location.port}/media/`;
|
2024-06-12 18:44:59 -06:00
|
|
|
export const MAP_VIEWER_URI = `https://${window.location.hostname}:8084`;
|
|
|
|
|
export const ZIM_VIEWER_URI = `https://${window.location.hostname}:8085`;
|
|
|
|
|
export const HELP_VIEWER_URI = `https://${window.location.hostname}:8086`;
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function Paginator({activePage, onPageChange, totalPages, showFirstAndLast, size = 'mini'}) {
|
2022-02-02 14:02:51 -07:00
|
|
|
const handlePageChange = (e, {activePage}) => {
|
|
|
|
|
onPageChange(activePage);
|
2022-02-02 08:24:34 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-01 11:52:41 -06:00
|
|
|
return <>
|
|
|
|
|
<Media at='mobile'>
|
|
|
|
|
<Pagination
|
|
|
|
|
activePage={activePage}
|
|
|
|
|
boundaryRange={1}
|
|
|
|
|
onPageChange={handlePageChange}
|
|
|
|
|
size={size}
|
|
|
|
|
siblingRange={2}
|
2023-11-26 20:49:36 -07:00
|
|
|
totalPages={totalPages || 1}
|
2023-04-01 11:52:41 -06:00
|
|
|
firstItem={showFirstAndLast ? undefined : null}
|
|
|
|
|
lastItem={showFirstAndLast ? undefined : null}
|
|
|
|
|
/>
|
|
|
|
|
</Media>
|
|
|
|
|
<Media greaterThanOrEqual='tablet'>
|
|
|
|
|
<Pagination
|
|
|
|
|
activePage={activePage}
|
|
|
|
|
boundaryRange={1}
|
|
|
|
|
onPageChange={handlePageChange}
|
|
|
|
|
size={size}
|
|
|
|
|
siblingRange={5}
|
2023-11-26 20:49:36 -07:00
|
|
|
totalPages={totalPages || 1}
|
2023-04-01 11:52:41 -06:00
|
|
|
firstItem={showFirstAndLast ? undefined : null}
|
|
|
|
|
lastItem={showFirstAndLast ? undefined : null}
|
|
|
|
|
/>
|
|
|
|
|
</Media>
|
|
|
|
|
</>
|
2022-02-02 08:24:34 -07:00
|
|
|
}
|
|
|
|
|
|
2022-09-28 09:38:05 -06:00
|
|
|
export function divmod(x, y) {
|
|
|
|
|
return [Math.floor(x / y), x % y];
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 13:13:21 -06:00
|
|
|
export function secondsElapsed(seconds, short = true) {
|
2022-09-28 09:38:05 -06:00
|
|
|
// Convert the provided seconds into a human-readable string of the time elapsed between the provided timestamp
|
|
|
|
|
// and now.
|
|
|
|
|
if (!seconds || seconds < 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 08:53:00 -07:00
|
|
|
// Get seconds elapsed between now and `seconds` which is a UTC epoch.
|
2024-08-20 13:13:21 -06:00
|
|
|
const localNow = (new Date()).getTime() / 1000;
|
2023-01-03 08:53:00 -07:00
|
|
|
|
2022-09-28 09:38:05 -06:00
|
|
|
let years;
|
|
|
|
|
let days;
|
|
|
|
|
let hours;
|
|
|
|
|
let minutes;
|
|
|
|
|
|
2024-08-20 13:13:21 -06:00
|
|
|
seconds = Math.abs(localNow - seconds);
|
2022-09-28 09:38:05 -06:00
|
|
|
[years, seconds] = divmod(seconds, secondsToYears);
|
|
|
|
|
[days, seconds] = divmod(seconds, secondsToDays);
|
|
|
|
|
[hours, seconds] = divmod(seconds, secondsToHours);
|
|
|
|
|
[minutes, seconds] = divmod(seconds, secondsToMinutes);
|
|
|
|
|
seconds = Math.floor(seconds);
|
|
|
|
|
|
2024-08-20 13:13:21 -06:00
|
|
|
if (short) {
|
|
|
|
|
if (years > 0 && days > 30) {
|
|
|
|
|
return `${years}y${days}d`;
|
|
|
|
|
} else if (years > 0) {
|
|
|
|
|
return `${years}y`;
|
|
|
|
|
} else if (days > 0) {
|
|
|
|
|
return `${days}d`;
|
|
|
|
|
} else if (hours > 0) {
|
|
|
|
|
return `${hours}h`;
|
|
|
|
|
} else if (minutes > 0) {
|
|
|
|
|
return `${minutes}m`;
|
|
|
|
|
}
|
|
|
|
|
return `${seconds}s`
|
|
|
|
|
} else {
|
|
|
|
|
if (years > 0 && days > 30) {
|
|
|
|
|
return `${years} years ${days} days`;
|
|
|
|
|
} else if (years > 0) {
|
|
|
|
|
return `${years} years`;
|
|
|
|
|
} else if (days > 0) {
|
|
|
|
|
return `${days} days`;
|
|
|
|
|
} else if (hours > 0) {
|
|
|
|
|
return `${hours} hours`;
|
|
|
|
|
} else if (minutes > 0) {
|
|
|
|
|
return `${minutes} minutes`;
|
|
|
|
|
}
|
|
|
|
|
return `${seconds} seconds`
|
2022-09-28 09:38:05 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function secondsToElapsedPopup(seconds) {
|
|
|
|
|
// Return a Popup which allows the user see a more detailed timestamp when hovering.
|
|
|
|
|
const elapsed = secondsElapsed(seconds);
|
|
|
|
|
if (!elapsed) {
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|
|
|
|
|
return <Popup
|
|
|
|
|
content={secondsToTimestamp(seconds)}
|
|
|
|
|
on='hover'
|
|
|
|
|
trigger={<span>{elapsed}</span>}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-23 16:04:09 -07:00
|
|
|
export function isoDatetimeToElapsedPopup(dt) {
|
2023-01-03 08:53:00 -07:00
|
|
|
let d = new Date(dt);
|
2022-12-23 16:04:09 -07:00
|
|
|
return secondsToElapsedPopup(d.getTime() / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 13:13:21 -06:00
|
|
|
export function isoDatetimeToAgoPopup(dt, short = true) {
|
|
|
|
|
const seconds = (new Date(dt)).getTime() / 1000;
|
|
|
|
|
// Return a Popup which allows the user see a more detailed timestamp when hovering.
|
|
|
|
|
const elapsed = secondsElapsed(seconds, short);
|
|
|
|
|
if (seconds === 0 || !elapsed) {
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|
|
|
|
|
const trigger = <span>{isoDatetimeToString(dt)}</span>;
|
|
|
|
|
return <Popup
|
|
|
|
|
content={<span>{elapsed} ago</span>}
|
|
|
|
|
on='hover'
|
|
|
|
|
trigger={trigger}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 10:20:06 -06:00
|
|
|
export function secondsToHMS(totalSeconds) {
|
|
|
|
|
let hours = Math.floor(totalSeconds / 3600);
|
|
|
|
|
totalSeconds -= hours * 3600;
|
|
|
|
|
let minutes = Math.floor(totalSeconds / 60);
|
|
|
|
|
let seconds = totalSeconds - (minutes * 60);
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
hours = String('00' + hours).slice(-2);
|
|
|
|
|
minutes = String('00' + minutes).slice(-2);
|
|
|
|
|
seconds = String('00' + seconds).slice(-2);
|
|
|
|
|
|
2023-10-24 10:20:06 -06:00
|
|
|
return {hours, minutes, seconds};
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2023-10-24 10:20:06 -06:00
|
|
|
export function Duration({totalSeconds}) {
|
|
|
|
|
const {hours, minutes, seconds} = secondsToHMS(totalSeconds);
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
if (hours > 0) {
|
2023-10-24 10:20:06 -06:00
|
|
|
return <div className='duration-overlay'>{hours}:{minutes}:{seconds}</div>
|
|
|
|
|
} else if (totalSeconds) {
|
|
|
|
|
return <div className='duration-overlay'>{minutes}:{seconds}</div>
|
2021-11-01 16:42:52 -06:00
|
|
|
} else {
|
|
|
|
|
return <></>
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 10:39:06 -06:00
|
|
|
export function isoDatetimeToString(dt, time = false) {
|
2022-12-23 16:04:09 -07:00
|
|
|
// Convert a datetime to a human-readable date format.
|
2023-03-24 08:58:18 -06:00
|
|
|
let d = <React.Fragment/>;
|
2023-09-13 10:39:06 -06:00
|
|
|
if (dt && time) {
|
2022-12-29 16:10:40 -07:00
|
|
|
d = new Date(dt);
|
2023-10-20 18:53:26 -06:00
|
|
|
const hours = String(d.getHours()).padStart(2, '0');
|
|
|
|
|
const minutes = String(d.getMinutes()).padStart(2, '0');
|
|
|
|
|
const seconds = String(d.getSeconds()).padStart(2, '0');
|
|
|
|
|
d = `${d.toDateString()} ${hours}:${minutes}:${seconds}`;
|
2023-09-13 10:39:06 -06:00
|
|
|
} else if (dt) {
|
|
|
|
|
d = new Date(dt);
|
|
|
|
|
d = d.toDateString();
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
2022-12-08 13:45:46 -07:00
|
|
|
return d;
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function CardLink({to, newTab = false, ...props}) {
|
|
|
|
|
const {t} = useContext(ThemeContext);
|
|
|
|
|
props = {...props, ...t};
|
|
|
|
|
|
|
|
|
|
props = newTab === true ? {...props, target: '_blank', rel: 'noopener noreferrer'} : props;
|
|
|
|
|
return <Link to={to} className="no-link-underscore card-link" {...props}>
|
|
|
|
|
{props.children}
|
|
|
|
|
</Link>
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 10:07:49 -06:00
|
|
|
export function ExternalCardLink({to, children, ...props}) {
|
2022-08-06 05:22:07 +00:00
|
|
|
const {t} = useContext(ThemeContext);
|
2023-11-02 10:07:49 -06:00
|
|
|
return <a
|
|
|
|
|
href={to}
|
|
|
|
|
target='_blank'
|
|
|
|
|
rel='noopener noreferrer'
|
|
|
|
|
className='no-link-underscore card-link'
|
|
|
|
|
{...t}
|
|
|
|
|
{...props}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
2022-08-06 05:22:07 +00:00
|
|
|
</a>
|
2022-01-29 16:50:22 -07:00
|
|
|
}
|
|
|
|
|
|
2023-02-15 20:58:07 -07:00
|
|
|
export function PreviewLink({file, children, className, ...props}) {
|
|
|
|
|
const {t} = useContext(ThemeContext);
|
2023-02-15 21:05:34 -07:00
|
|
|
const {setPreviewFile} = React.useContext(FilePreviewContext);
|
2023-02-15 20:58:07 -07:00
|
|
|
className = className ? `clickable ${className}` : `clickable `;
|
2023-02-15 21:05:34 -07:00
|
|
|
return <span className={className} onClick={() => setPreviewFile(file)} {...props} {...t}>
|
2023-02-15 20:58:07 -07:00
|
|
|
{children}
|
2023-02-15 21:05:34 -07:00
|
|
|
</span>
|
2023-02-15 20:58:07 -07:00
|
|
|
}
|
|
|
|
|
|
2023-08-26 20:28:34 -06:00
|
|
|
export function PreviewPath({path, mimetype, taggable = true, ...props}) {
|
|
|
|
|
return <PreviewLink file={{path, mimetype, taggable}} {...props}/>
|
2023-02-17 16:30:25 -07:00
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:42:52 -06:00
|
|
|
export function RequiredAsterisk() {
|
|
|
|
|
return <span style={{color: '#db2828'}}> *</span>
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 13:06:03 -06:00
|
|
|
export let defaultFileOrder = '-published_datetime';
|
2021-11-01 16:42:52 -06:00
|
|
|
export let defaultSearchOrder = 'rank';
|
|
|
|
|
|
2023-01-31 18:08:07 -07:00
|
|
|
export const frequencyOptions = [{key: null, text: '', value: null}, {
|
|
|
|
|
key: 'daily', text: 'Daily', value: 86400
|
|
|
|
|
}, {key: 'weekly', text: 'Weekly', value: 604800}, {key: 'biweekly', text: 'Biweekly', value: 1209600}, {
|
2022-08-06 05:22:07 +00:00
|
|
|
key: '30days', text: '30 Days', value: 2592000
|
|
|
|
|
}, {key: '90days', text: '90 Days', value: 7776000},];
|
|
|
|
|
|
|
|
|
|
export const rssFrequencyOptions = [{key: 'once', text: 'Once', value: 0}, {
|
|
|
|
|
key: 'hourly', text: 'Hourly', value: 3600
|
|
|
|
|
}, {key: '3hours', text: '3 hours', value: 10800}, {key: '12hours', text: '12 hours', value: 43200}, {
|
|
|
|
|
key: 'daily', text: 'Daily', value: 86400
|
|
|
|
|
}, {key: 'weekly', text: 'Weekly', value: 604800}, {key: 'biweekly', text: 'Biweekly', value: 1209600}, {
|
|
|
|
|
key: '30days', text: '30 Days', value: 2592000
|
2023-05-29 18:04:03 -06:00
|
|
|
}, {key: '90days', text: '90 Days', value: 7776000}, {key: '180days', text: '180 Days', value: 15552000}];
|
2022-05-20 15:15:31 -06:00
|
|
|
|
2021-11-01 16:42:52 -06:00
|
|
|
export function secondsToFrequency(seconds) {
|
2022-05-20 15:15:31 -06:00
|
|
|
for (let i = 0; i < Object.keys(rssFrequencyOptions).length; i++) {
|
|
|
|
|
let d = rssFrequencyOptions[i];
|
2021-11-01 16:42:52 -06:00
|
|
|
if (d.value === seconds) {
|
|
|
|
|
return d.text;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const secondsToYears = 31536000;
|
|
|
|
|
const secondsToDays = 86400;
|
|
|
|
|
const secondsToHours = 3600;
|
|
|
|
|
const secondsToMinutes = 60;
|
|
|
|
|
|
2022-09-28 09:38:05 -06:00
|
|
|
export function secondsToFullDuration(seconds) {
|
2023-11-04 15:57:13 -06:00
|
|
|
let duration = '';
|
2021-11-01 16:42:52 -06:00
|
|
|
|
2022-09-28 09:38:05 -06:00
|
|
|
let years, days, hours, minutes;
|
|
|
|
|
[years, seconds] = divmod(seconds, secondsToYears);
|
|
|
|
|
[days, seconds] = divmod(seconds, secondsToDays);
|
|
|
|
|
[hours, seconds] = divmod(seconds, secondsToHours);
|
|
|
|
|
[minutes] = divmod(seconds, secondsToMinutes);
|
|
|
|
|
if (years > 0) {
|
2023-11-04 15:57:13 -06:00
|
|
|
duration = `${years}Y`;
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
2022-09-28 09:38:05 -06:00
|
|
|
if (days > 0) {
|
2023-11-04 15:57:13 -06:00
|
|
|
duration = `${duration} ${days}D`;
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
2022-09-28 09:38:05 -06:00
|
|
|
if (hours > 0 || minutes > 0) {
|
|
|
|
|
hours = String(hours).padStart(2, '0');
|
|
|
|
|
minutes = String(minutes).padStart(2, '0');
|
2023-11-04 15:57:13 -06:00
|
|
|
duration = `${duration} ${hours}:${minutes}`;
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
2023-11-04 15:57:13 -06:00
|
|
|
if (years === 0 && days === 0 && hours === 0 && minutes === 0) {
|
|
|
|
|
// Less than a minute.
|
|
|
|
|
seconds = String(seconds).padStart(2, '0');
|
|
|
|
|
duration = `00:00:${seconds}`;
|
|
|
|
|
}
|
|
|
|
|
return duration.trim();
|
2021-11-01 16:42:52 -06:00
|
|
|
}
|
|
|
|
|
|
2022-01-25 08:51:55 -07:00
|
|
|
export function secondsToTimestamp(seconds) {
|
2023-01-03 12:35:27 -07:00
|
|
|
let d = new Date(seconds * 1000);
|
2023-01-03 08:53:00 -07:00
|
|
|
const month = String(d.getMonth() + 1).padStart(2, '0');
|
2022-09-28 09:38:05 -06:00
|
|
|
const day = String(d.getDate()).padStart(2, '0');
|
|
|
|
|
const hours = String(d.getHours()).padStart(2, '0');
|
|
|
|
|
const minutes = String(d.getMinutes()).padStart(2, '0');
|
|
|
|
|
const sec = String(d.getSeconds()).padStart(2, '0');
|
2023-01-03 08:53:00 -07:00
|
|
|
return `${d.getFullYear()}-${month}-${day} ${hours}:${minutes}:${sec}`;
|
2022-01-25 08:51:55 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-24 17:15:15 -06:00
|
|
|
export function humanFileSize(bytes, dp = 1) {
|
2022-03-08 13:26:03 -07:00
|
|
|
const thresh = 1024;
|
2021-11-01 16:42:52 -06:00
|
|
|
|
|
|
|
|
if (Math.abs(bytes) < thresh) {
|
|
|
|
|
return bytes + ' B';
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 13:26:03 -07:00
|
|
|
const units = ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
2021-11-01 16:42:52 -06:00
|
|
|
let u = -1;
|
|
|
|
|
const r = 10 ** dp;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
bytes /= thresh;
|
|
|
|
|
++u;
|
|
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bytes.toFixed(dp) + ' ' + units[u];
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 16:36:39 -06:00
|
|
|
export function humanBandwidth(bytes) {
|
2022-10-26 11:19:50 -06:00
|
|
|
// Convert bytes to MBps and return a string.
|
|
|
|
|
const thresh = 1024;
|
|
|
|
|
|
|
|
|
|
if (Math.abs(bytes) < thresh) {
|
|
|
|
|
return bytes + ' Bps';
|
2022-09-29 16:36:39 -06:00
|
|
|
}
|
2022-10-26 11:19:50 -06:00
|
|
|
|
|
|
|
|
const units = ['KBps', 'MBps', 'GBps', 'TBps', 'PBps', 'EBps', 'ZBps', 'YBps'];
|
|
|
|
|
let u = -1;
|
|
|
|
|
const r = 10;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
bytes /= thresh;
|
|
|
|
|
++u;
|
|
|
|
|
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1);
|
|
|
|
|
|
|
|
|
|
return bytes.toFixed(0) + ' ' + units[u];
|
2022-09-29 16:36:39 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-20 16:08:13 -07:00
|
|
|
export function humanNumber(num, dp = 1) {
|
2022-09-29 16:36:39 -06:00
|
|
|
// Convert large numbers to a more human-readable format.
|
2021-12-20 16:08:13 -07:00
|
|
|
// >> humanNumber(1000)
|
|
|
|
|
// 1.0k
|
|
|
|
|
// >> humanNumber(1500000)
|
|
|
|
|
// 1.5m
|
|
|
|
|
const divisor = 1000;
|
|
|
|
|
if (Math.abs(num) < divisor) {
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
const units = ['k', 'm', 'b'];
|
|
|
|
|
let i = -1;
|
|
|
|
|
const r = 10 ** dp;
|
|
|
|
|
do {
|
|
|
|
|
num /= divisor;
|
|
|
|
|
++i;
|
|
|
|
|
} while (Math.round(Math.abs(num) * r) / r >= divisor && i < units.length - 1);
|
|
|
|
|
|
|
|
|
|
return num.toFixed(dp) + units[i];
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 16:42:52 -06:00
|
|
|
export function replaceNullValues(obj, newValue) {
|
|
|
|
|
newValue = newValue === undefined ? '' : newValue;
|
|
|
|
|
let keys = Object.keys(obj);
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
|
let key = keys[i];
|
|
|
|
|
obj[key] = obj[key] === null ? newValue : obj[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function enumerate(array) {
|
|
|
|
|
let newArray = [];
|
|
|
|
|
for (let i = 0; i < array.length; i++) {
|
|
|
|
|
newArray = newArray.concat([[i, array[i]]]);
|
|
|
|
|
}
|
|
|
|
|
return newArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function arraysEqual(a, b) {
|
|
|
|
|
if (a === b) return true;
|
|
|
|
|
if (a == null || b == null) return false;
|
|
|
|
|
if (a.length !== b.length) return false;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < a.length; ++i) {
|
|
|
|
|
if (a[i] !== b[i]) return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function scrollToTop() {
|
|
|
|
|
window.scrollTo({
|
2022-08-06 05:22:07 +00:00
|
|
|
top: 0, behavior: "auto"
|
2021-11-01 16:42:52 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 18:04:03 -06:00
|
|
|
export function scrollToTopOfElement(element, smooth = true) {
|
2023-05-03 09:01:25 -06:00
|
|
|
element.scroll({
|
|
|
|
|
top: 0,
|
|
|
|
|
behavior: smooth ? 'smooth' : 'auto',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-25 10:48:08 -06:00
|
|
|
const useClearableInput = (searchStr, onChange, onClear, onSubmit, size = 'small', placeholder = 'Search...', icon = 'search', clearDisabled = null) => {
|
2023-11-02 15:24:07 -06:00
|
|
|
const [value, setValue] = useState(searchStr || '');
|
2023-11-21 13:01:52 -07:00
|
|
|
const [submitted, setSubmitted] = useState(false);
|
2022-01-03 15:13:02 -07:00
|
|
|
|
2024-02-17 13:46:28 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
// Search was changed above.
|
|
|
|
|
if (searchStr !== value) {
|
|
|
|
|
setValue(searchStr);
|
|
|
|
|
}
|
|
|
|
|
if (!searchStr) {
|
|
|
|
|
setValue('');
|
|
|
|
|
}
|
|
|
|
|
}, [searchStr, value]);
|
|
|
|
|
|
2023-11-02 10:45:35 -06:00
|
|
|
const handleChange = (e) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
setValue(e.target.value);
|
2023-11-21 13:01:52 -07:00
|
|
|
setSubmitted(false);
|
2023-11-02 10:45:35 -06:00
|
|
|
if (onChange) {
|
|
|
|
|
// Try to call the remote function, don't let its failure break this.
|
|
|
|
|
try {
|
|
|
|
|
onChange(e.target.value);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Call to ${onChange} failed`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
const handleClearSearch = (e) => {
|
|
|
|
|
e.preventDefault();
|
2022-01-03 15:13:02 -07:00
|
|
|
// Clear the input when the "clear" button is clicked, search again.
|
|
|
|
|
setValue('');
|
2023-11-21 13:01:52 -07:00
|
|
|
setSubmitted(false);
|
2023-11-02 10:45:35 -06:00
|
|
|
if (onSubmit) {
|
|
|
|
|
onSubmit('');
|
|
|
|
|
}
|
2023-05-29 18:04:03 -06:00
|
|
|
if (onClear) {
|
|
|
|
|
onClear();
|
|
|
|
|
}
|
2022-01-03 15:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const localOnSubmit = (e) => {
|
|
|
|
|
// Send the value up when submitting.
|
|
|
|
|
e.preventDefault();
|
2023-11-21 13:01:52 -07:00
|
|
|
setSubmitted(true);
|
2023-11-02 10:45:35 -06:00
|
|
|
if (onSubmit) {
|
|
|
|
|
onSubmit(value);
|
|
|
|
|
} else {
|
|
|
|
|
console.debug('No onSubmit defined');
|
|
|
|
|
}
|
2022-01-03 15:13:02 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 13:01:52 -07:00
|
|
|
const clearButton = <Button
|
|
|
|
|
icon='close'
|
|
|
|
|
size={size}
|
|
|
|
|
onClick={handleClearSearch}
|
|
|
|
|
type='button'
|
2024-05-25 10:48:08 -06:00
|
|
|
// If `clearDisabled` is provided, use it to disable the clear button. Fallback to disabling if there is no
|
|
|
|
|
// search value.
|
|
|
|
|
disabled={clearDisabled !== null ? clearDisabled : !!!value}
|
2023-11-21 13:01:52 -07:00
|
|
|
className='search-clear'
|
|
|
|
|
/>;
|
|
|
|
|
|
2023-11-21 13:10:12 -07:00
|
|
|
// Can only clear after submitting.
|
|
|
|
|
let action = submitted ? clearButton : <Button type='button' icon={icon} size='big'/>;
|
2023-11-21 13:01:52 -07:00
|
|
|
|
|
|
|
|
const input = <Input fluid
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
type='text'
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
value={value}
|
|
|
|
|
size={size}
|
|
|
|
|
className='search-input'
|
|
|
|
|
action={action}
|
|
|
|
|
/>;
|
|
|
|
|
|
|
|
|
|
return {value, submitted, clearButton, input, localOnSubmit, handleChange}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SearchInput({
|
|
|
|
|
searchStr,
|
|
|
|
|
onSubmit,
|
|
|
|
|
onChange,
|
|
|
|
|
onClear,
|
|
|
|
|
size = 'small',
|
|
|
|
|
placeholder = 'Search...',
|
|
|
|
|
icon = 'search',
|
2023-11-21 13:10:12 -07:00
|
|
|
...props
|
2023-11-21 13:01:52 -07:00
|
|
|
}) {
|
2023-11-21 13:10:12 -07:00
|
|
|
// A Semantic <Input> with a Clear button as the action.
|
2023-11-21 13:01:52 -07:00
|
|
|
let {input, localOnSubmit} = useClearableInput(searchStr, onChange, onClear, onSubmit, size, placeholder, icon);
|
|
|
|
|
|
2023-11-21 13:10:12 -07:00
|
|
|
return <Form onSubmit={localOnSubmit} {...props} className='search-container'>
|
2023-11-21 13:01:52 -07:00
|
|
|
{input}
|
|
|
|
|
</Form>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SearchResultsInput({
|
|
|
|
|
searchStr,
|
|
|
|
|
onSubmit,
|
|
|
|
|
onClear = null,
|
|
|
|
|
onChange = null,
|
|
|
|
|
size = 'small',
|
|
|
|
|
placeholder = 'Search...',
|
|
|
|
|
icon = 'search',
|
|
|
|
|
action,
|
|
|
|
|
actionIcon,
|
|
|
|
|
clearable = false,
|
2024-05-25 10:48:08 -06:00
|
|
|
clearDisabled = null,
|
2023-11-21 13:01:52 -07:00
|
|
|
autoFocus = false,
|
|
|
|
|
results = undefined,
|
|
|
|
|
handleResultSelect = null,
|
|
|
|
|
resultRenderer = undefined,
|
2023-11-21 21:15:24 -07:00
|
|
|
loading = false,
|
2023-11-21 13:01:52 -07:00
|
|
|
...props
|
|
|
|
|
}) {
|
2023-11-21 13:10:12 -07:00
|
|
|
// A Semantic <Search> input with a Clear button.
|
2023-11-21 13:01:52 -07:00
|
|
|
let {
|
|
|
|
|
value,
|
|
|
|
|
clearButton,
|
|
|
|
|
localOnSubmit,
|
|
|
|
|
handleChange,
|
2024-05-25 10:48:08 -06:00
|
|
|
} = useClearableInput(searchStr, onChange, onClear, onSubmit, size, placeholder, icon, clearDisabled);
|
2023-11-21 13:01:52 -07:00
|
|
|
|
2023-11-22 10:35:01 -07:00
|
|
|
// Show a "Loading" message rather than "No results" while results are pending.
|
|
|
|
|
const loadingResults = {'loading': {name: 'Loading', results: [{title: 'Results are pending...'}]}};
|
|
|
|
|
|
2023-11-20 11:29:14 -07:00
|
|
|
const localHandleResultSelect = (e, data) => {
|
2023-11-20 11:01:46 -07:00
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2023-11-20 11:29:14 -07:00
|
|
|
if (handleResultSelect) {
|
2023-11-21 13:01:52 -07:00
|
|
|
console.debug(`Selected result`, data);
|
2023-11-20 11:29:14 -07:00
|
|
|
handleResultSelect(data);
|
2023-11-21 13:01:52 -07:00
|
|
|
} else {
|
|
|
|
|
console.error('No handleResultSelect defined!');
|
2023-11-20 11:01:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 13:01:52 -07:00
|
|
|
return <Form onSubmit={localOnSubmit} {...props} className='search-container'>
|
|
|
|
|
<Search category
|
2023-11-21 13:10:12 -07:00
|
|
|
input={{fluid: true, icon: icon}}
|
2023-11-20 11:01:46 -07:00
|
|
|
placeholder={placeholder}
|
|
|
|
|
type='text'
|
|
|
|
|
onSearchChange={handleChange}
|
|
|
|
|
onResultSelect={localHandleResultSelect}
|
|
|
|
|
value={value}
|
|
|
|
|
size={size}
|
2023-11-22 12:34:17 -07:00
|
|
|
results={_.isEmpty(results) ? loadingResults : results}
|
2023-11-21 13:01:52 -07:00
|
|
|
resultRenderer={resultRenderer}
|
|
|
|
|
className='search-input'
|
2023-11-21 21:15:24 -07:00
|
|
|
loading={loading}
|
2022-08-06 05:22:07 +00:00
|
|
|
/>
|
2023-11-21 13:01:52 -07:00
|
|
|
{clearable === true && <div style={{marginLeft: '1em'}}>{clearButton}</div>}
|
2022-08-06 05:22:07 +00:00
|
|
|
</Form>
|
2022-01-03 15:13:02 -07:00
|
|
|
}
|
2022-01-12 09:38:21 -07:00
|
|
|
|
|
|
|
|
export function WROLModeMessage({content}) {
|
2023-07-21 15:14:09 -06:00
|
|
|
const wrolModeEnabled = useWROLMode();
|
2022-08-06 05:22:07 +00:00
|
|
|
|
2023-07-21 15:14:09 -06:00
|
|
|
if (wrolModeEnabled) {
|
2022-01-12 09:38:21 -07:00
|
|
|
return <Message icon='lock' header='WROL Mode Enabled' content={content}/>
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-01-14 16:27:16 -07:00
|
|
|
|
|
|
|
|
// Thanks https://www.npmjs.com/package/text-ellipsis
|
2022-08-06 05:22:07 +00:00
|
|
|
export function textEllipsis(str, maxLength = 100, {side = "end", ellipsis = "..."} = {}) {
|
|
|
|
|
if (str && str.length > maxLength) {
|
2022-01-25 08:51:55 -07:00
|
|
|
switch (side) {
|
|
|
|
|
case "start":
|
|
|
|
|
return ellipsis + str.slice(-(maxLength - ellipsis.length));
|
|
|
|
|
case "end":
|
|
|
|
|
default:
|
|
|
|
|
return str.slice(0, maxLength - ellipsis.length) + ellipsis;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return str;
|
2022-01-14 16:27:16 -07:00
|
|
|
}
|
2022-01-29 21:24:39 -07:00
|
|
|
|
2022-01-29 22:25:35 -07:00
|
|
|
export function TabLinks({links}) {
|
2024-04-14 13:06:03 -06:00
|
|
|
const [searchParams] = useSearchParams();
|
2023-05-29 18:04:03 -06:00
|
|
|
|
|
|
|
|
const getTo = (to) => {
|
|
|
|
|
return `${to}?${searchParams.toString()}`;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-17 16:09:24 -07:00
|
|
|
return <Menu tabular>
|
2022-08-06 05:22:07 +00:00
|
|
|
{links.map((link) => <NavLink
|
2023-05-29 18:04:03 -06:00
|
|
|
to={getTo(link.to)}
|
2022-08-06 05:22:07 +00:00
|
|
|
className='item'
|
|
|
|
|
style={{padding: '1em'}}
|
|
|
|
|
key={link.to}
|
|
|
|
|
end={link.end === true ? true : null}
|
|
|
|
|
>
|
|
|
|
|
{link.text}
|
|
|
|
|
</NavLink>)}
|
2023-02-17 16:09:24 -07:00
|
|
|
</Menu>
|
2022-01-29 21:24:39 -07:00
|
|
|
}
|
2022-01-31 09:32:06 -07:00
|
|
|
|
|
|
|
|
export function PageContainer(props) {
|
2023-01-07 18:31:39 -07:00
|
|
|
return <>
|
|
|
|
|
<Media at='mobile'>
|
|
|
|
|
<Container style={{marginTop: '1em', padding: 0}}>
|
2022-08-06 05:22:07 +00:00
|
|
|
{props.children}
|
|
|
|
|
</Container>
|
2023-01-07 18:31:39 -07:00
|
|
|
</Media>
|
|
|
|
|
<Media greaterThanOrEqual='tablet'>
|
|
|
|
|
<Container fluid style={{marginTop: '1em', padding: '1em'}}>
|
2022-08-06 05:22:07 +00:00
|
|
|
{props.children}
|
|
|
|
|
</Container>
|
2023-01-07 18:31:39 -07:00
|
|
|
</Media>
|
|
|
|
|
</>;
|
2022-01-31 09:32:06 -07:00
|
|
|
}
|
2022-01-31 09:53:17 -07:00
|
|
|
|
|
|
|
|
export function CardGroupCentered(props) {
|
2023-02-17 16:09:24 -07:00
|
|
|
return <div style={{marginTop: '1em'}}>
|
2023-01-07 18:31:39 -07:00
|
|
|
<Media at='mobile'>
|
|
|
|
|
<Card.Group centered>
|
2022-08-06 05:22:07 +00:00
|
|
|
{props.children}
|
|
|
|
|
</Card.Group>
|
2023-01-07 18:31:39 -07:00
|
|
|
</Media>
|
|
|
|
|
<Media greaterThanOrEqual='tablet'>
|
|
|
|
|
<Card.Group>
|
2022-08-06 05:22:07 +00:00
|
|
|
{props.children}
|
|
|
|
|
</Card.Group>
|
2023-01-07 18:31:39 -07:00
|
|
|
</Media>
|
2023-02-17 16:09:24 -07:00
|
|
|
</div>
|
2022-01-31 09:53:17 -07:00
|
|
|
}
|
2022-02-04 13:55:13 -07:00
|
|
|
|
2023-03-29 13:46:39 -06:00
|
|
|
export function findPosterPath(file) {
|
|
|
|
|
if (!file) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const {files, poster_path, cover_path, screenshot_path, video} = file;
|
|
|
|
|
if (poster_path) {
|
|
|
|
|
return poster_path;
|
|
|
|
|
}
|
|
|
|
|
if (cover_path) {
|
|
|
|
|
return cover_path;
|
|
|
|
|
}
|
|
|
|
|
if (screenshot_path) {
|
|
|
|
|
return screenshot_path;
|
|
|
|
|
}
|
2023-04-03 11:17:14 -06:00
|
|
|
if (file['data'] && file['data']['cover_path']) {
|
|
|
|
|
// Ebook.
|
|
|
|
|
return file['data']['cover_path'];
|
|
|
|
|
}
|
2023-03-29 13:46:39 -06:00
|
|
|
if (video && video['poster_path']) {
|
|
|
|
|
// file is a video model.
|
|
|
|
|
return video['poster_path'];
|
|
|
|
|
}
|
|
|
|
|
if (!_.isEmpty(files)) {
|
|
|
|
|
for (let i = 0; i < files.length; i++) {
|
|
|
|
|
const file = files[i];
|
|
|
|
|
if (file['mimetype'] && file['mimetype'].startsWith('image/')) {
|
|
|
|
|
// Found an image file, use that as the poster.
|
|
|
|
|
return file['path'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function CardPoster({to, file}) {
|
2022-11-08 22:42:00 -07:00
|
|
|
const {s} = useContext(ThemeContext);
|
2023-03-29 13:46:39 -06:00
|
|
|
// Used to center posters in CardIcon.
|
2022-11-08 22:42:00 -07:00
|
|
|
const style = {display: 'flex', justifyContent: 'center', ...s['style']};
|
2023-07-27 15:53:47 -06:00
|
|
|
const navigate = useNavigate();
|
2023-03-29 13:46:39 -06:00
|
|
|
|
|
|
|
|
const cardTagIcon = <div className="ui green left corner label">
|
|
|
|
|
<i aria-hidden="true" className="tag icon"></i>
|
|
|
|
|
</div>;
|
|
|
|
|
let imageLabel = !_.isEmpty(file.tags) ? cardTagIcon : null;
|
|
|
|
|
|
|
|
|
|
let posterPath = findPosterPath(file);
|
|
|
|
|
|
2023-08-04 11:32:47 -06:00
|
|
|
if (posterPath) {
|
|
|
|
|
// FileGroup has a poster (screenshot/thumbnail) file.
|
|
|
|
|
posterPath = `/media/${encodeMediaPath(posterPath)}`;
|
|
|
|
|
|
|
|
|
|
const image = <>
|
|
|
|
|
{/* Replicate <Image label/> but with maxHeight applied to image */}
|
|
|
|
|
{imageLabel}
|
2024-01-01 17:11:09 -07:00
|
|
|
<img alt='poster' src={posterPath} style={{maxHeight: '163px', maxWidth: '290px', width: 'auto'}}/>
|
2023-08-04 11:32:47 -06:00
|
|
|
</>;
|
|
|
|
|
|
|
|
|
|
if (to) {
|
|
|
|
|
// Link within this App.
|
|
|
|
|
return <Link to={to} style={style}>
|
2023-03-29 13:46:39 -06:00
|
|
|
{image}
|
2023-08-04 11:32:47 -06:00
|
|
|
</Link>
|
|
|
|
|
} else {
|
|
|
|
|
// Preview the file.
|
|
|
|
|
return <div style={style}>
|
|
|
|
|
<PreviewLink file={file}>
|
|
|
|
|
{image}
|
|
|
|
|
</PreviewLink>
|
|
|
|
|
</div>
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// FileGroup has no poster.
|
|
|
|
|
if (!to || (to.startsWith('/media/') || to.startsWith('/download/'))) {
|
|
|
|
|
// "to" is a downloadable file outside the app, preview the file.
|
|
|
|
|
return <PreviewLink file={file}>
|
|
|
|
|
<CardIcon>
|
|
|
|
|
{imageLabel}
|
|
|
|
|
<FileIcon file={file}/>
|
|
|
|
|
</CardIcon>
|
2023-03-29 13:46:39 -06:00
|
|
|
</PreviewLink>
|
2023-08-04 11:32:47 -06:00
|
|
|
} else if (!posterPath && to) {
|
|
|
|
|
// Link to the full page in this App.
|
|
|
|
|
return <Link to={to}>
|
|
|
|
|
<CardIcon onClick={() => navigate(to)}>
|
|
|
|
|
{imageLabel}
|
|
|
|
|
<FileIcon file={file}/>
|
|
|
|
|
</CardIcon>
|
|
|
|
|
</Link>
|
|
|
|
|
}
|
2022-12-09 11:22:20 -07:00
|
|
|
}
|
2022-11-08 22:42:00 -07:00
|
|
|
}
|
|
|
|
|
|
2024-06-25 22:12:13 -06:00
|
|
|
export function HelpPopup({icon = 'question', size = null, content, position = 'left center', iconSize = 'small'}) {
|
2022-05-18 19:43:25 -06:00
|
|
|
return <Popup
|
|
|
|
|
content={content}
|
2024-06-25 22:12:13 -06:00
|
|
|
size={size}
|
|
|
|
|
position={position}
|
|
|
|
|
trigger={<Icon circular link name={icon} size={iconSize}
|
2022-08-06 05:22:07 +00:00
|
|
|
style={{marginLeft: '0.25em', marginRight: '0.25em'}}
|
|
|
|
|
/>}
|
2022-05-18 19:43:25 -06:00
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 19:28:53 -06:00
|
|
|
export function HelpHeader({
|
|
|
|
|
icon,
|
|
|
|
|
headerSize = 'h2',
|
|
|
|
|
iconSize,
|
|
|
|
|
headerContent,
|
|
|
|
|
popupContent,
|
|
|
|
|
popupPosition = 'top center'
|
|
|
|
|
}) {
|
2023-02-17 16:09:24 -07:00
|
|
|
return <div className='inline-header'>
|
2024-06-25 22:12:13 -06:00
|
|
|
<Header as={headerSize}>{headerContent}</Header>
|
2022-08-06 05:22:07 +00:00
|
|
|
<span>
|
2024-06-25 22:12:13 -06:00
|
|
|
<HelpPopup content={popupContent} size={iconSize} icon={icon} position={popupPosition}/>
|
|
|
|
|
</span>
|
2023-02-17 16:09:24 -07:00
|
|
|
</div>
|
2022-05-18 19:43:25 -06:00
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function HotspotToggle() {
|
2022-02-04 13:55:13 -07:00
|
|
|
let {on, setHotspot} = useHotspot();
|
2022-05-18 19:43:25 -06:00
|
|
|
const disabled = on === null;
|
2023-02-17 16:09:24 -07:00
|
|
|
return <div style={{margin: '0.5em'}}>
|
2022-08-06 05:22:07 +00:00
|
|
|
<Toggle
|
|
|
|
|
label='WiFi Hotspot'
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
checked={on === true}
|
2022-10-15 19:27:20 -06:00
|
|
|
onChange={checked => setHotspot(checked)}
|
2022-08-06 05:22:07 +00:00
|
|
|
/>
|
|
|
|
|
{disabled && <HelpPopup content='Hotspot is not supported on this server'/>}
|
2023-02-17 16:09:24 -07:00
|
|
|
</div>;
|
2022-02-04 13:55:13 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function ThrottleToggle() {
|
2022-02-07 13:37:58 -07:00
|
|
|
let {on, setThrottle} = useThrottle();
|
2022-05-18 19:43:25 -06:00
|
|
|
const disabled = on === null;
|
2023-02-17 16:09:24 -07:00
|
|
|
return <div style={{margin: '0.5em'}}>
|
2022-08-06 05:22:07 +00:00
|
|
|
<Toggle
|
|
|
|
|
label='CPU Power-save'
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
checked={on === true}
|
2022-10-15 19:27:20 -06:00
|
|
|
onChange={checked => setThrottle(checked)}
|
2022-08-06 05:22:07 +00:00
|
|
|
/>
|
|
|
|
|
{disabled && <HelpPopup content='CPU Power-save is not supported on this server'/>}
|
2023-02-17 16:09:24 -07:00
|
|
|
</div>;
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function Toggle({label, checked, disabled, onChange, icon}) {
|
|
|
|
|
// Custom toggle because Semantic UI does not handle inverted labels correctly.
|
2022-09-29 09:12:53 -06:00
|
|
|
const {t, inverted} = useContext(ThemeContext);
|
2022-08-06 05:22:07 +00:00
|
|
|
|
|
|
|
|
let style = {marginLeft: '1em'};
|
|
|
|
|
if (t && t.style) {
|
|
|
|
|
style = {...t.style, ...style};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disabled = disabled === true ? 'disabled' : '';
|
|
|
|
|
|
|
|
|
|
let inputClassName = `${disabled} ${inverted}`;
|
|
|
|
|
let sliderClassName = `${disabled} ${inverted} slider`;
|
|
|
|
|
if (inverted) {
|
|
|
|
|
style['color'] = '#dddddd';
|
|
|
|
|
}
|
|
|
|
|
if (disabled && inverted) {
|
|
|
|
|
style['color'] = '#888888';
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-31 18:08:07 -07:00
|
|
|
let onMouseUp = () => {
|
|
|
|
|
};
|
2022-08-06 05:22:07 +00:00
|
|
|
if (onChange) {
|
|
|
|
|
onMouseUp = (e) => {
|
|
|
|
|
if (disabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (onChange) {
|
|
|
|
|
onChange(!checked);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (icon) {
|
|
|
|
|
icon = <Icon name={icon}/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <>
|
|
|
|
|
<div className='toggle' onMouseUp={onMouseUp}>
|
2023-01-31 18:08:07 -07:00
|
|
|
<input type="checkbox" className={inputClassName} checked={checked} onChange={onMouseUp}
|
|
|
|
|
data-testid='toggle'/>
|
2022-08-06 05:22:07 +00:00
|
|
|
<span className={sliderClassName}></span>
|
|
|
|
|
</div>
|
2023-01-31 18:08:07 -07:00
|
|
|
<span style={style} data-testid='toggle-label'>
|
2022-08-06 05:22:07 +00:00
|
|
|
{icon}
|
|
|
|
|
{label}
|
|
|
|
|
</span>
|
|
|
|
|
</>
|
2022-02-07 13:37:58 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 13:55:13 -07:00
|
|
|
export function DisableDownloadsToggle() {
|
2023-04-02 14:55:02 -06:00
|
|
|
const [pending, setPending] = React.useState(false);
|
2023-04-01 11:52:41 -06:00
|
|
|
const {status, fetchStatus} = React.useContext(StatusContext);
|
2023-04-02 14:55:02 -06:00
|
|
|
|
|
|
|
|
const {downloads} = status ? status : {downloads: null};
|
2023-07-21 15:14:09 -06:00
|
|
|
const wrolModeEnabled = useWROLMode();
|
2023-04-01 11:52:41 -06:00
|
|
|
|
|
|
|
|
const setDownloads = async (enable) => {
|
2023-04-02 14:55:02 -06:00
|
|
|
setPending(true);
|
|
|
|
|
if (enable) {
|
|
|
|
|
await startDownloads();
|
|
|
|
|
} else {
|
|
|
|
|
await killDownloads();
|
|
|
|
|
}
|
|
|
|
|
await fetchStatus();
|
|
|
|
|
setPending(false);
|
2023-04-01 11:52:41 -06:00
|
|
|
}
|
|
|
|
|
|
2023-04-02 14:55:02 -06:00
|
|
|
const on = downloads && downloads['disabled'] === false && downloads['stopped'] === false;
|
2022-08-06 05:22:07 +00:00
|
|
|
return <Form>
|
2023-01-31 18:08:07 -07:00
|
|
|
<Toggle
|
2023-04-02 14:55:02 -06:00
|
|
|
label={on === true ? 'Downloading Enabled' : 'Downloading Disabled'}
|
2023-07-21 15:14:09 -06:00
|
|
|
disabled={wrolModeEnabled || pending || downloads === null}
|
2023-01-31 18:08:07 -07:00
|
|
|
checked={on === true}
|
|
|
|
|
onChange={setDownloads}
|
2022-08-06 05:22:07 +00:00
|
|
|
/>
|
|
|
|
|
</Form>;
|
2022-02-04 13:55:13 -07:00
|
|
|
}
|
2022-02-16 21:57:27 -07:00
|
|
|
|
|
|
|
|
export function emptyToNull(obj) {
|
|
|
|
|
const keys = Object.keys(obj);
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
|
const key = keys[i];
|
|
|
|
|
if (obj[key] === '') {
|
|
|
|
|
obj[key] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
2022-03-08 13:26:03 -07:00
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function mimetypeColor(mimetype) {
|
2023-01-31 18:08:07 -07:00
|
|
|
if (!mimetype) {
|
|
|
|
|
return 'grey';
|
|
|
|
|
}
|
2022-08-06 05:22:07 +00:00
|
|
|
try {
|
|
|
|
|
if (mimetype === 'application/pdf') {
|
|
|
|
|
return 'red'
|
2023-01-31 18:08:07 -07:00
|
|
|
} else if (mimetype.startsWith('video/')) {
|
2022-08-06 05:22:07 +00:00
|
|
|
return 'blue'
|
2023-01-31 18:08:07 -07:00
|
|
|
} else if (mimetype.startsWith('image/')) {
|
2022-08-06 05:22:07 +00:00
|
|
|
return 'pink'
|
2023-01-31 18:08:07 -07:00
|
|
|
} else if (mimetype.startsWith('text/html')) {
|
2022-08-06 05:22:07 +00:00
|
|
|
return 'green'
|
2023-08-17 17:53:29 -06:00
|
|
|
} else if (mimetype.startsWith('application/epub')) {
|
2022-12-09 11:22:20 -07:00
|
|
|
return 'yellow'
|
2023-02-22 12:41:41 -07:00
|
|
|
} else if (mimetype.startsWith('audio/')) {
|
|
|
|
|
return 'violet'
|
2023-08-17 17:53:29 -06:00
|
|
|
} else if (isZipMimetype(mimetype)) {
|
|
|
|
|
return 'purple'
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
console.error('Unable to choose mimetype color');
|
|
|
|
|
}
|
|
|
|
|
return 'grey'
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 21:20:23 -07:00
|
|
|
export function isZipMimetype(mimetype) {
|
2023-02-15 21:35:29 -07:00
|
|
|
return mimetype.startsWith('application/zip') || mimetype.startsWith('application/zlib') || mimetype.startsWith('application/x-7z-compressed') || mimetype.startsWith('application/x-bzip2') || mimetype.startsWith('application/x-xz') || mimetype.startsWith('application/gzip') || mimetype.startsWith('application/x-rar');
|
2023-02-15 21:20:23 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-18 22:03:45 -06:00
|
|
|
export function mimetypeIconName(mimetype, lowerPath = '') {
|
2022-08-06 05:22:07 +00:00
|
|
|
if (mimetype) {
|
2023-02-16 08:39:34 -07:00
|
|
|
if (mimetype.startsWith('text/html') || mimetype.startsWith('application/json') || mimetype.startsWith('text/yaml') || mimetype.startsWith('text/xml')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'file code';
|
2022-08-06 05:22:07 +00:00
|
|
|
} else if (mimetype.startsWith('application/pdf')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'file pdf';
|
2022-08-06 05:22:07 +00:00
|
|
|
} else if (mimetype.startsWith('text/plain')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'file text';
|
2022-08-06 05:22:07 +00:00
|
|
|
} else if (mimetype.startsWith('image/')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'image';
|
2022-08-06 05:22:07 +00:00
|
|
|
} else if (mimetype.startsWith('video/')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'film';
|
2023-02-15 21:20:23 -07:00
|
|
|
} else if (mimetype.startsWith('message/rfc822')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'mail';
|
2023-02-15 21:20:23 -07:00
|
|
|
} else if (isZipMimetype(mimetype)) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'file archive';
|
|
|
|
|
} else if (mimetype.startsWith('application/x-iso9660-image') || mimetype.startsWith('application/x-raw-disk-image') || mimetype.startsWith('application/x-cd-image')) {
|
|
|
|
|
return 'dot circle';
|
2023-02-16 08:39:34 -07:00
|
|
|
} else if (mimetype.startsWith('application/epub') || mimetype.startsWith('application/x-mobipocket-ebook') || mimetype.startsWith('application/vnd.amazon.mobi8-ebook')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'book';
|
2023-01-31 18:08:07 -07:00
|
|
|
} else if (mimetype.startsWith('text/vtt') || mimetype.startsWith('text/srt')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'closed captioning';
|
2023-02-21 15:49:11 -07:00
|
|
|
} else if (mimetype.startsWith('application/x-openscad') || mimetype.startsWith('model/stl')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'cube';
|
2023-09-20 12:17:07 -06:00
|
|
|
} else if (mimetype.startsWith('application/x-dosexec') || mimetype.startsWith('application/x-msi') || mimetype.startsWith('application/vnd.microsoft.portable-executable')) {
|
|
|
|
|
return 'windows';
|
2023-02-22 11:48:25 -07:00
|
|
|
} else if (mimetype.startsWith('audio/')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'file audio';
|
|
|
|
|
} else if (mimetype.startsWith('application/vnd.openxmlformats-officedocument.wordprocessingml.document')) {
|
|
|
|
|
return 'file word';
|
|
|
|
|
} else if (mimetype.startsWith('application/x-x509-ca-cert')) {
|
|
|
|
|
return 'certificate';
|
2023-02-16 08:39:34 -07:00
|
|
|
} else if (mimetype.startsWith('application/octet-stream')) {
|
2023-02-22 12:41:41 -07:00
|
|
|
if (lowerPath.endsWith('.mp3')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'file audio';
|
2023-02-22 12:41:41 -07:00
|
|
|
} else if (lowerPath.endsWith('.stl')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'cube';
|
2023-02-16 08:39:34 -07:00
|
|
|
} else if (lowerPath.endsWith('.blend')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'cube';
|
2023-02-16 08:39:34 -07:00
|
|
|
} else if (lowerPath.endsWith('.dmg')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'apple';
|
2023-02-16 08:39:34 -07:00
|
|
|
} else if (lowerPath.endsWith('.azw3')) {
|
2023-04-18 22:03:45 -06:00
|
|
|
return 'book';
|
2023-09-20 12:17:07 -06:00
|
|
|
} else if (lowerPath.endsWith('.exe')) {
|
|
|
|
|
return 'windows';
|
2023-02-16 08:39:34 -07:00
|
|
|
}
|
2023-05-29 18:04:03 -06:00
|
|
|
} else if (mimetype.startsWith('application/vnd.openxmlformats-officedocument.spreadsheetml.') || mimetype.startsWith('application/vnd.ms-excel') || mimetype.startsWith('application/vnd.oasis.opendocument.spreadsheet')) {
|
|
|
|
|
return 'file excel'
|
|
|
|
|
} else if (mimetype.startsWith('application/vnd.openxmlformats-officedocument.wordprocessingml.') || mimetype.startsWith('application/msword') || mimetype.startsWith('application/vnd.oasis.opendocument.text')) {
|
|
|
|
|
return 'file word'
|
|
|
|
|
} else if (mimetype.startsWith('application/vnd.openxmlformats-officedocument.presentationml.') || mimetype.startsWith('application/vnd.ms-powerpoint') || mimetype.startsWith('application/vnd.oasis.opendocument.presentation')) {
|
|
|
|
|
return 'file powerpoint'
|
2023-09-15 12:02:33 -06:00
|
|
|
} else if (mimetype.startsWith('font/') || mimetype.startsWith('application/font-sfnt') || mimetype.startsWith('application/vnd.ms-fontobject')) {
|
|
|
|
|
return 'font'
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-04-18 22:03:45 -06:00
|
|
|
if (lowerPath.endsWith('.pem')) {
|
|
|
|
|
return 'certificate';
|
|
|
|
|
}
|
|
|
|
|
return 'file';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function FileIcon({file, disabled = true, size = 'huge', ...props}) {
|
|
|
|
|
// Default to a grey file icon.
|
|
|
|
|
const {mimetype, path, primary_path} = file;
|
|
|
|
|
// `file` may be a file_group or a file.
|
|
|
|
|
const lowerPath = primary_path ? primary_path.toLocaleString() : path.toLowerCase();
|
|
|
|
|
props['name'] = mimetypeIconName(mimetype, lowerPath);
|
|
|
|
|
props['color'] = mimetypeColor(mimetype);
|
2022-08-06 05:22:07 +00:00
|
|
|
return <Icon disabled={disabled} size={size} {...props}/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function LoadStatistic({label, value, cores, ...props}) {
|
|
|
|
|
const quarter = cores / 4;
|
|
|
|
|
if (cores && value >= (quarter * 3)) {
|
2022-09-29 14:58:16 -06:00
|
|
|
props['color'] = 'red';
|
2022-08-06 05:22:07 +00:00
|
|
|
} else if (cores && value >= (quarter * 2)) {
|
2022-09-29 14:58:16 -06:00
|
|
|
props['color'] = 'orange';
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
2022-09-28 14:56:06 -06:00
|
|
|
return <Statistic
|
|
|
|
|
label={label}
|
|
|
|
|
value={value ? parseFloat(value).toFixed(1) : '?'}
|
|
|
|
|
{...props}/>;
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function DarkModeToggle() {
|
2022-10-27 17:35:36 -06:00
|
|
|
const {savedTheme, cycleSavedTheme} = useContext(ThemeContext);
|
|
|
|
|
let iconName = 'lightbulb outline';
|
|
|
|
|
if (savedTheme === darkTheme) {
|
|
|
|
|
iconName = 'moon';
|
|
|
|
|
} else if (savedTheme === lightTheme) {
|
|
|
|
|
iconName = 'sun';
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 11:16:45 -06:00
|
|
|
return <Icon
|
2022-10-27 17:35:36 -06:00
|
|
|
name={iconName}
|
|
|
|
|
onClick={cycleSavedTheme}
|
|
|
|
|
style={{cursor: 'pointer'}}
|
|
|
|
|
/>
|
2022-08-06 05:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-22 14:27:18 -06:00
|
|
|
|
|
|
|
|
export function UnsupportedModal(header, message, icon) {
|
|
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
|
const onOpen = () => setOpen(true);
|
|
|
|
|
const onClose = () => setOpen(false);
|
|
|
|
|
|
|
|
|
|
const modal = <Modal basic closeIcon
|
|
|
|
|
onOpen={onOpen}
|
|
|
|
|
onClose={onClose}
|
|
|
|
|
open={open}
|
|
|
|
|
>
|
|
|
|
|
<Header>
|
|
|
|
|
<Icon name={icon || 'exclamation triangle'}/>
|
|
|
|
|
{header || 'Unsupported'}
|
|
|
|
|
</Header>
|
2023-04-18 22:03:45 -06:00
|
|
|
<ModalContent>
|
|
|
|
|
<ModalDescription>
|
2022-10-22 14:27:18 -06:00
|
|
|
{message}
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalDescription>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
<ModalActions>
|
2022-10-22 14:27:18 -06:00
|
|
|
<Button basic inverted onClick={onClose}>Ok</Button>
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalActions>
|
2022-10-22 14:27:18 -06:00
|
|
|
</Modal>;
|
|
|
|
|
|
|
|
|
|
return {modal, doClose: onClose, doOpen: onOpen};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-19 09:18:53 -06:00
|
|
|
export function HotspotStatusIcon() {
|
2024-07-31 12:41:53 -06:00
|
|
|
const {on, inUse, setHotspot, dockerized, hotspotSsid} = useHotspot();
|
|
|
|
|
const {modal: unsupportedModal, doOpen: openUnsupportedModal} =
|
|
|
|
|
UnsupportedModal('Unsupported', 'You cannot toggle the hotspot on this machine.');
|
|
|
|
|
const [disableHotspotOpen, setDisableHotspotOpen] = React.useState(false);
|
|
|
|
|
const [inUseOpen, setInUseOpen] = React.useState(false);
|
2022-10-22 14:27:18 -06:00
|
|
|
|
2024-07-31 12:41:53 -06:00
|
|
|
const handleConfirmDisable = (e) => {
|
2023-09-24 20:10:09 -06:00
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault()
|
2022-10-19 09:18:53 -06:00
|
|
|
}
|
2024-07-31 12:41:53 -06:00
|
|
|
setDisableHotspotOpen(false);
|
2023-09-24 20:10:09 -06:00
|
|
|
setHotspot(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 12:41:53 -06:00
|
|
|
const handleConfirmInUse = (e) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault()
|
|
|
|
|
}
|
|
|
|
|
setInUseOpen(false);
|
|
|
|
|
setHotspot(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inUse === true) {
|
|
|
|
|
const content = hotspotSsid ? `Wifi device is in use for ${hotspotSsid}. Disconnect and enable hotspot?`
|
|
|
|
|
: 'Wifi device is in use. Disconnect and enable hotspot?'
|
|
|
|
|
return <>
|
|
|
|
|
<Confirm open={inUseOpen}
|
|
|
|
|
onCancel={() => setInUseOpen(false)}
|
|
|
|
|
onClose={() => setInUseOpen(false)}
|
|
|
|
|
onConfirm={handleConfirmInUse}
|
|
|
|
|
header='Wifi is in-use'
|
|
|
|
|
content={content}
|
|
|
|
|
confirmButton='Enable Hotspot'
|
|
|
|
|
/>
|
|
|
|
|
<a href='#' onClick={() => setInUseOpen(true)}>
|
|
|
|
|
<IconGroup size='large'>
|
|
|
|
|
<Icon name='wifi' disabled/>
|
|
|
|
|
<Icon corner name='exclamation'/>
|
|
|
|
|
</IconGroup>
|
|
|
|
|
</a>
|
|
|
|
|
</>
|
|
|
|
|
} else if (dockerized === false && on === true) {
|
2023-09-24 20:10:09 -06:00
|
|
|
return <>
|
|
|
|
|
<Confirm
|
2024-07-31 12:41:53 -06:00
|
|
|
open={disableHotspotOpen}
|
|
|
|
|
onCancel={() => setDisableHotspotOpen(false)}
|
|
|
|
|
onClose={() => setDisableHotspotOpen(false)}
|
|
|
|
|
onConfirm={handleConfirmDisable}
|
2023-09-24 20:10:09 -06:00
|
|
|
header='Disable the hotspot'
|
|
|
|
|
content='You will be disconnected when using the hotspot. Are you sure?'
|
|
|
|
|
confirmButton='Disable'
|
|
|
|
|
/>
|
2024-07-31 12:41:53 -06:00
|
|
|
<a href='#' onClick={() => setDisableHotspotOpen(true)}>
|
2023-09-24 20:10:09 -06:00
|
|
|
<IconGroup size='large'>
|
|
|
|
|
<Icon name='wifi' disabled={on !== true}/>
|
|
|
|
|
{on === false && <Icon corner name='x'/>}
|
|
|
|
|
{on === null && <Icon corner name='question'/>}
|
|
|
|
|
</IconGroup>
|
|
|
|
|
</a>
|
|
|
|
|
</>
|
|
|
|
|
} else if (dockerized === false && on === false) {
|
|
|
|
|
return <a href='#' onClick={() => setHotspot(true)}>
|
|
|
|
|
<IconGroup size='large'>
|
|
|
|
|
<Icon name='wifi' disabled/>
|
|
|
|
|
<Icon corner name='x'/>
|
|
|
|
|
</IconGroup>
|
|
|
|
|
</a>
|
2022-10-19 09:18:53 -06:00
|
|
|
}
|
|
|
|
|
|
2023-09-24 20:10:09 -06:00
|
|
|
// Hotspot is not available, or, status has not yet been fetched.
|
2022-10-22 14:27:18 -06:00
|
|
|
return <>
|
2024-07-31 12:41:53 -06:00
|
|
|
<a href='#' onClick={openUnsupportedModal}>
|
2022-10-22 14:27:18 -06:00
|
|
|
<IconGroup size='large'>
|
2023-09-24 20:10:09 -06:00
|
|
|
<Icon name='wifi' disabled/>
|
|
|
|
|
<Icon corner name='question'/>
|
2022-10-22 14:27:18 -06:00
|
|
|
</IconGroup>
|
|
|
|
|
</a>
|
2024-07-31 12:41:53 -06:00
|
|
|
{unsupportedModal}
|
2022-10-22 14:27:18 -06:00
|
|
|
</>
|
2022-10-19 09:18:53 -06:00
|
|
|
}
|
|
|
|
|
|
2023-10-06 12:48:20 -06:00
|
|
|
export function CPUTemperatureIcon({size = 'large', fallback = null}) {
|
2023-08-13 10:54:43 -06:00
|
|
|
// Returns an Icon only if temperature is too high.
|
2023-10-06 12:48:20 -06:00
|
|
|
const {temperature, highTemperature, criticalTemperature} = useCPUTemperature();
|
2023-08-13 10:54:43 -06:00
|
|
|
|
2023-10-06 12:48:20 -06:00
|
|
|
if (temperature === null || temperature < highTemperature) {
|
2023-08-13 10:54:43 -06:00
|
|
|
// Temperature is not a problem.
|
2023-10-06 12:48:20 -06:00
|
|
|
return fallback;
|
2023-08-13 10:54:43 -06:00
|
|
|
}
|
|
|
|
|
|
2023-10-06 12:48:20 -06:00
|
|
|
let icon = temperature >= criticalTemperature ?
|
|
|
|
|
<Icon data-testid='cpuTemperatureIcon' name='thermometer' size={size} color='red'/>
|
|
|
|
|
: <Icon data-testid='cpuTemperatureIcon' name='thermometer half' size={size} color='yellow'/>;
|
|
|
|
|
icon = <Link to='/admin/status'>{icon}</Link>;
|
2023-08-13 10:54:43 -06:00
|
|
|
|
|
|
|
|
return <Popup content={`CPU: ${temperature}°C`} trigger={icon}/>
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 12:48:20 -06:00
|
|
|
export function SystemLoadIcon({size = 'large', fallback = null}) {
|
|
|
|
|
const {minute_1, mediumLoad, highLoad} = useLoad();
|
|
|
|
|
|
|
|
|
|
if (!mediumLoad && !highLoad) {
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only return the icon if the load is not low.
|
|
|
|
|
const color = highLoad ? 'red' : 'yellow';
|
|
|
|
|
let icon = <Link to='/admin/status'>
|
|
|
|
|
<Icon name='tachometer alternate' size={size} color={color}/>
|
|
|
|
|
</Link>;
|
|
|
|
|
return <Popup content={`Load: ${minute_1}`} trigger={icon}/>
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
export function useTitle(title) {
|
|
|
|
|
const documentDefined = typeof document !== 'undefined';
|
|
|
|
|
const originalTitle = React.useRef(documentDefined ? document.title : null);
|
2022-10-25 15:57:44 -06:00
|
|
|
const name = NAME ? `${NAME} WROLPi` : `WROLPi`;
|
2022-08-06 05:22:07 +00:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!documentDefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:57:44 -06:00
|
|
|
const newTitle = `${title} - ${name}`
|
2022-08-06 05:22:07 +00:00
|
|
|
if (title && document.title !== newTitle) {
|
|
|
|
|
document.title = newTitle;
|
|
|
|
|
}
|
|
|
|
|
return () => {
|
|
|
|
|
document.title = originalTitle.current;
|
|
|
|
|
}
|
2022-10-25 15:57:44 -06:00
|
|
|
}, [title]);
|
2022-03-08 13:26:03 -07:00
|
|
|
}
|
2022-11-09 08:33:55 -07:00
|
|
|
|
2023-04-18 22:03:45 -06:00
|
|
|
export function DirectorySearch({onSelect, value, ...props}) {
|
|
|
|
|
const {
|
|
|
|
|
directoryName,
|
|
|
|
|
setDirectoryName,
|
|
|
|
|
directories,
|
|
|
|
|
channelDirectories,
|
|
|
|
|
domainDirectories,
|
2024-10-02 21:59:33 -06:00
|
|
|
isDir,
|
2023-04-18 22:03:45 -06:00
|
|
|
loading,
|
|
|
|
|
} = useSearchDirectories(value);
|
|
|
|
|
const [results, setResults] = useState();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-10-02 21:59:33 -06:00
|
|
|
if (directories && directories.length >= 0) {
|
|
|
|
|
const newDirectory = isDir ? {} : {
|
|
|
|
|
newDirectory: {
|
|
|
|
|
name: 'New Directory',
|
|
|
|
|
results: [{title: directoryName}],
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
const newResults = {
|
|
|
|
|
...newDirectory,
|
2023-04-18 22:03:45 -06:00
|
|
|
directories: {
|
|
|
|
|
name: 'Directories',
|
|
|
|
|
results: directories.map(i => {
|
|
|
|
|
return {title: i['path']}
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
channel_directories: {
|
|
|
|
|
name: 'Channels',
|
|
|
|
|
results: channelDirectories.map(i => {
|
|
|
|
|
return {title: i['path'], description: i['name']};
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
domain_directories: {
|
|
|
|
|
name: 'Domains',
|
|
|
|
|
results: domainDirectories.map(i => {
|
|
|
|
|
return {title: i['path'], description: i['domain']};
|
|
|
|
|
}),
|
2024-10-02 21:59:33 -06:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
setResults(newResults);
|
2023-04-18 22:03:45 -06:00
|
|
|
}
|
|
|
|
|
}, [
|
|
|
|
|
JSON.stringify(directories),
|
|
|
|
|
JSON.stringify(channelDirectories),
|
|
|
|
|
JSON.stringify(domainDirectories),
|
|
|
|
|
loading,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const handleSearchChange = (e, data) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
setDirectoryName(data.value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleResultSelect = (e, data) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
2024-10-02 21:59:33 -06:00
|
|
|
setDirectoryName(data.result.title);
|
2023-04-18 22:03:45 -06:00
|
|
|
// title is the relative path.
|
|
|
|
|
if (onSelect) {
|
|
|
|
|
onSelect(data.result.title);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-02 21:59:33 -06:00
|
|
|
return <Search category
|
|
|
|
|
placeholder='Search directory names...'
|
|
|
|
|
onSearchChange={handleSearchChange}
|
|
|
|
|
onResultSelect={handleResultSelect}
|
|
|
|
|
loading={loading}
|
|
|
|
|
value={directoryName}
|
|
|
|
|
results={results}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
2022-11-09 08:33:55 -07:00
|
|
|
}
|
2022-11-30 19:31:18 -07:00
|
|
|
|
2024-05-31 23:53:30 -06:00
|
|
|
export const BackButton = ({...props}) => {
|
2022-11-30 19:31:18 -07:00
|
|
|
const navigate = useNavigate();
|
2024-05-31 23:53:30 -06:00
|
|
|
return <Button icon='arrow left' content='Back' onClick={() => navigate(-1)} {...props}/>;
|
2022-11-30 19:31:18 -07:00
|
|
|
}
|
2022-11-30 20:13:54 -07:00
|
|
|
|
2024-07-30 19:28:53 -06:00
|
|
|
export const semanticUIColorMap = {
|
|
|
|
|
red: '#db2828',
|
|
|
|
|
orange: '#f2711c',
|
|
|
|
|
yellow: '#fbbd08',
|
|
|
|
|
olive: '#b5cc18',
|
|
|
|
|
green: '#21ba45',
|
|
|
|
|
teal: '#00b5ad',
|
|
|
|
|
blue: '#2185d0',
|
|
|
|
|
violet: '#6435c9',
|
|
|
|
|
purple: '#a333c8',
|
|
|
|
|
pink: '#e03997',
|
|
|
|
|
brown: '#a5673f',
|
|
|
|
|
grey: '#767676',
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-30 20:13:54 -07:00
|
|
|
export const ColorToSemanticHexColor = (color) => {
|
2024-07-30 19:28:53 -06:00
|
|
|
return semanticUIColorMap[color] || null;
|
2022-11-30 20:13:54 -07:00
|
|
|
}
|
2022-12-09 11:22:20 -07:00
|
|
|
|
|
|
|
|
export const filterToMimetypes = (filter) => {
|
2023-11-02 15:31:13 -06:00
|
|
|
const zipMimetypes = ['application/zip', 'application/zlib', 'application/x-bzip2', 'application/x-xz', 'application/x-bzip', 'application/x-bzip2', 'application/gzip', 'application/vnd.rar', 'application/x-tar', 'application/x-7z-compressed'];
|
|
|
|
|
const softwareMimetypes = [...zipMimetypes, 'application/x-iso9660-image', 'application/x-executable', 'application/x-dosexec'];
|
|
|
|
|
|
2022-12-09 11:22:20 -07:00
|
|
|
if (filter === 'video') {
|
|
|
|
|
return ['video'];
|
|
|
|
|
} else if (filter === 'archive') {
|
|
|
|
|
return ['text/html'];
|
|
|
|
|
} else if (filter === 'pdf') {
|
|
|
|
|
return ['application/pdf'];
|
2023-02-22 12:41:41 -07:00
|
|
|
} else if (filter === 'audio') {
|
|
|
|
|
return ['audio'];
|
2022-12-09 11:22:20 -07:00
|
|
|
} else if (filter === 'ebook') {
|
|
|
|
|
return ['application/epub+zip', 'application/x-mobipocket-ebook'];
|
|
|
|
|
} else if (filter === 'image') {
|
|
|
|
|
return ['image'];
|
|
|
|
|
} else if (filter === 'zip') {
|
2023-11-02 15:31:13 -06:00
|
|
|
return zipMimetypes;
|
2023-04-09 14:13:36 -06:00
|
|
|
} else if (filter === 'model') {
|
|
|
|
|
return ['application/x-openscad', 'model/stl', 'application/sla', 'model/obj'];
|
2023-10-29 21:26:35 -06:00
|
|
|
} else if (filter === 'software') {
|
2023-11-02 15:31:13 -06:00
|
|
|
return softwareMimetypes;
|
2022-12-09 11:22:20 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-12-15 13:50:58 -07:00
|
|
|
|
|
|
|
|
export const toLocaleString = (num, locale = 'en-US') => {
|
|
|
|
|
return num.toLocaleString(locale);
|
|
|
|
|
}
|
2022-12-19 10:58:45 -07:00
|
|
|
|
2023-03-24 08:58:18 -06:00
|
|
|
function luma(color) {
|
|
|
|
|
let rgb = (typeof color === 'string') ? hexToRGBArray(color) : color;
|
|
|
|
|
return (0.2126 * rgb[0]) + (0.7152 * rgb[1]) + (0.0722 * rgb[2]); // SMPTE C, Rec. 709 weightings
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function hexToRGBArray(color) {
|
|
|
|
|
if (color.length === 7) {
|
|
|
|
|
color = color.slice(1);
|
|
|
|
|
}
|
|
|
|
|
if (color.length !== 6) {
|
|
|
|
|
console.error('Invalid hex color: ' + color);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let rgb = [];
|
|
|
|
|
for (let i = 0; i <= 2; i++) rgb[i] = parseInt(color.substr(i * 2, 2), 16);
|
|
|
|
|
return rgb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function contrastingColor(color) {
|
|
|
|
|
return (luma(color) >= 120) ? '#000000' : '#dddddd';
|
|
|
|
|
}
|
2023-03-29 13:46:39 -06:00
|
|
|
|
|
|
|
|
export const encodeMediaPath = (path) => {
|
|
|
|
|
// Replace % first to avoid replacing the other replacements.
|
|
|
|
|
path = path.replaceAll('%', '%25');
|
|
|
|
|
|
2023-08-20 16:06:16 -06:00
|
|
|
path = path.replaceAll('&', '%26');
|
2023-08-20 18:50:30 -06:00
|
|
|
path = path.replaceAll('=', '%3d');
|
2023-03-29 13:46:39 -06:00
|
|
|
path = path.replaceAll('#', '%23');
|
|
|
|
|
path = path.replaceAll(' ', '%20');
|
|
|
|
|
return path
|
|
|
|
|
}
|
2023-04-09 14:13:36 -06:00
|
|
|
|
|
|
|
|
export function SortButton({sorts = []}) {
|
|
|
|
|
const {sort, setSort} = useSearchOrder();
|
|
|
|
|
|
|
|
|
|
if (!sorts || (sorts && sorts.length === 0)) {
|
|
|
|
|
console.error('No sorts have been defined!');
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 16:09:09 -06:00
|
|
|
const [localSort, setLocalSort] = useState(sort ? sort.replaceAll(/^-/g, '') : null);
|
|
|
|
|
const [desc, setDesc] = useState(sort ? sort.startsWith('-') : true);
|
2023-04-09 14:13:36 -06:00
|
|
|
const [open, setOpen] = useState(false);
|
2023-04-12 16:09:09 -06:00
|
|
|
|
|
|
|
|
// Remove the - from the front of the query sort, it will be added when toggling direction.
|
|
|
|
|
const sortKey = localSort ? localSort.replaceAll(/^-/g, '') : sorts[0]['value'];
|
|
|
|
|
const selectedSort = sorts.find(i => i['value'] === sortKey);
|
2023-04-09 14:13:36 -06:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (localSort) {
|
|
|
|
|
const newSort = desc ? `-${localSort}` : localSort;
|
|
|
|
|
console.debug(`Setting new sort: ${newSort}`)
|
|
|
|
|
setSort(newSort);
|
|
|
|
|
}
|
|
|
|
|
}, [localSort, desc]);
|
|
|
|
|
|
|
|
|
|
const handleSortButton = (o) => {
|
|
|
|
|
setLocalSort(o);
|
|
|
|
|
setOpen(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toggleDesc = () => {
|
|
|
|
|
setDesc(!desc);
|
2023-04-12 16:09:09 -06:00
|
|
|
if (!localSort) {
|
|
|
|
|
// No sort in URL, use the first.
|
|
|
|
|
setLocalSort(sorts[0]['value']);
|
|
|
|
|
}
|
2023-04-09 14:13:36 -06:00
|
|
|
setOpen(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let sortFields;
|
|
|
|
|
if (sorts && sorts.length) {
|
|
|
|
|
sortFields = sorts.map((i) => {
|
2023-04-18 22:03:45 -06:00
|
|
|
return <Button key={i['value']} onClick={() => handleSortButton(i['value'])}>{i['text']}</Button>
|
2023-04-09 14:13:36 -06:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <>
|
|
|
|
|
<Modal closeIcon
|
|
|
|
|
open={open}
|
|
|
|
|
onClose={() => setOpen(false)}
|
|
|
|
|
>
|
2023-04-18 22:03:45 -06:00
|
|
|
<ModalHeader>Sort By</ModalHeader>
|
|
|
|
|
<ModalContent>
|
2023-04-09 14:13:36 -06:00
|
|
|
{sortFields}
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalContent>
|
2023-04-09 14:13:36 -06:00
|
|
|
</Modal>
|
|
|
|
|
<ButtonGroup icon>
|
|
|
|
|
<Button icon={desc ? 'sort down' : 'sort up'} onClick={() => toggleDesc()}/>
|
2023-10-24 10:20:06 -06:00
|
|
|
<Button content={selectedSort['short'] || selectedSort['text']} onClick={() => setOpen(true)}/>
|
2023-04-09 14:13:36 -06:00
|
|
|
</ButtonGroup>
|
|
|
|
|
</>
|
2023-05-29 18:04:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TagIcon() {
|
|
|
|
|
return <Label circular color='green' style={{padding: '0.5em', marginRight: '0.5em'}}>
|
|
|
|
|
<Icon name='tag' style={{margin: 0}}/>
|
|
|
|
|
</Label>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function normalizeEstimate(estimate) {
|
2024-01-01 15:16:03 -07:00
|
|
|
if (Number.isInteger(estimate)) {
|
|
|
|
|
return estimate > 999 ? '>999' : estimate.toString();
|
|
|
|
|
}
|
|
|
|
|
return '?';
|
2023-07-21 15:14:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useAPIButton(
|
|
|
|
|
color = 'violet',
|
|
|
|
|
size = 'medium',
|
|
|
|
|
floated,
|
|
|
|
|
onClick,
|
|
|
|
|
disabled,
|
|
|
|
|
confirmContent,
|
|
|
|
|
confirmButton,
|
2024-10-05 14:06:13 -06:00
|
|
|
confirmHeader,
|
2023-07-21 15:14:09 -06:00
|
|
|
themed = true,
|
|
|
|
|
obeyWROLMode = false,
|
|
|
|
|
icon = null,
|
|
|
|
|
props
|
|
|
|
|
) {
|
|
|
|
|
props = props || {};
|
|
|
|
|
const ref = React.useRef();
|
|
|
|
|
|
|
|
|
|
const [confirmOpen, setConfirmOpen] = React.useState(false);
|
|
|
|
|
const [pending, setPending] = React.useState(false);
|
|
|
|
|
const [animation, setAnimation] = React.useState('jiggle');
|
|
|
|
|
const [animationVisible, setAnimationVisible] = React.useState(true);
|
|
|
|
|
const [showSuccess, setShowSuccess] = React.useState(false);
|
|
|
|
|
const [showFailure, setShowFailure] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
const wrolModeEnabled = useWROLMode();
|
|
|
|
|
|
|
|
|
|
// Disable when API call is pending, or button is disabled.
|
|
|
|
|
disabled = pending || disabled;
|
|
|
|
|
// Disable when WROL Mode is enabled, otherwise normal disabled.
|
|
|
|
|
disabled = obeyWROLMode ? wrolModeEnabled || disabled : disabled;
|
|
|
|
|
|
|
|
|
|
const reset = () => {
|
|
|
|
|
setShowSuccess(false);
|
|
|
|
|
setShowFailure(false);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setSuccess = () => {
|
|
|
|
|
setShowSuccess(true);
|
|
|
|
|
setAnimation('pulse');
|
|
|
|
|
setAnimationVisible(!animationVisible);
|
|
|
|
|
setTimeout(reset, 2000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const setFailure = () => {
|
|
|
|
|
setShowFailure(true);
|
|
|
|
|
setAnimation('shake');
|
|
|
|
|
setAnimationVisible(!animationVisible);
|
|
|
|
|
setTimeout(reset, 2000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handleAPICall = async () => {
|
|
|
|
|
// Handle when user clicks button, or clicks confirm.
|
|
|
|
|
setPending(true);
|
|
|
|
|
try {
|
|
|
|
|
await onClick();
|
|
|
|
|
setSuccess();
|
|
|
|
|
} catch (e) {
|
2023-09-13 10:39:06 -06:00
|
|
|
console.error(e);
|
2023-07-21 15:14:09 -06:00
|
|
|
setFailure();
|
|
|
|
|
}
|
|
|
|
|
setPending(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const localOnClick = async (e) => {
|
|
|
|
|
if (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (confirmContent) {
|
|
|
|
|
// Clicking button opens confirm.
|
|
|
|
|
setConfirmOpen(true);
|
|
|
|
|
} else if (onClick) {
|
|
|
|
|
// No <Confirm/> send the API request.
|
|
|
|
|
await handleAPICall();
|
|
|
|
|
} else {
|
|
|
|
|
throw Error('No onClick defined!');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const localOnConfirm = async () => {
|
|
|
|
|
// User clicked the "OK" button in the <Confirm/>.
|
|
|
|
|
setConfirmOpen(false);
|
|
|
|
|
|
|
|
|
|
if (onClick) {
|
|
|
|
|
await handleAPICall();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create button with or without theme. Pass all props to the <Button/> (except props.children).
|
|
|
|
|
const buttonArgs = {color, onClick: localOnClick, disabled, loading: pending, size, floated, ...props};
|
|
|
|
|
|
2023-07-21 21:23:15 -06:00
|
|
|
let buttonContent = props.children || null;
|
2023-07-21 15:14:09 -06:00
|
|
|
if (icon) {
|
|
|
|
|
// Send Icon as Button properties.
|
|
|
|
|
buttonContent = null;
|
|
|
|
|
buttonArgs['icon'] = showSuccess ? 'check' : showFailure ? 'close' : icon;
|
|
|
|
|
} else if (showSuccess || showFailure) {
|
|
|
|
|
// Show ✔ or ✖ overtop the contents after API call has completed.
|
|
|
|
|
buttonContent = <>
|
|
|
|
|
<Icon style={{position: 'absolute'}} name={showSuccess ? 'check' : 'close'}/>
|
|
|
|
|
{/* Keep contents to avoid resizing button */}
|
|
|
|
|
<div style={{opacity: 0}}>{buttonContent}</div>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let button = themed ?
|
|
|
|
|
<Button ref={ref} {...buttonArgs}>{buttonContent}</Button>
|
|
|
|
|
: <SButton ref={ref} {...buttonArgs}>{buttonContent}</SButton>;
|
|
|
|
|
// Wrap button in <Transition/> to show success or failure animations.
|
|
|
|
|
button = <Transition animation={animation} duration={500} visible={animationVisible}>
|
|
|
|
|
{button}
|
|
|
|
|
</Transition>;
|
|
|
|
|
|
|
|
|
|
if (confirmContent) {
|
|
|
|
|
// Wrap button with <Confirm/>
|
|
|
|
|
button = <>
|
|
|
|
|
{button}
|
|
|
|
|
<Confirm open={confirmOpen}
|
|
|
|
|
content={confirmContent}
|
|
|
|
|
onClose={() => setConfirmOpen(false)}
|
|
|
|
|
onCancel={() => setConfirmOpen(false)}
|
|
|
|
|
onConfirm={localOnConfirm}
|
2023-08-17 20:07:02 -06:00
|
|
|
confirmButton={confirmButton}
|
2024-10-05 14:06:13 -06:00
|
|
|
header={confirmHeader}
|
2023-07-21 15:14:09 -06:00
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {button, ref}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function APIButton({
|
|
|
|
|
color,
|
|
|
|
|
size,
|
|
|
|
|
floated,
|
|
|
|
|
onClick,
|
|
|
|
|
disabled,
|
|
|
|
|
confirmContent,
|
|
|
|
|
confirmButton,
|
2024-10-05 14:06:13 -06:00
|
|
|
confirmHeader,
|
2023-07-21 15:14:09 -06:00
|
|
|
themed,
|
|
|
|
|
obeyWROLMode,
|
|
|
|
|
icon,
|
|
|
|
|
...props
|
|
|
|
|
}) {
|
|
|
|
|
const {button} = useAPIButton(
|
|
|
|
|
color,
|
|
|
|
|
size,
|
|
|
|
|
floated,
|
|
|
|
|
onClick,
|
|
|
|
|
disabled,
|
|
|
|
|
confirmContent,
|
|
|
|
|
confirmButton,
|
2024-10-05 14:06:13 -06:00
|
|
|
confirmHeader,
|
2023-07-21 15:14:09 -06:00
|
|
|
themed,
|
|
|
|
|
obeyWROLMode,
|
|
|
|
|
icon,
|
|
|
|
|
props
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return button;
|
2023-08-23 20:14:23 -06:00
|
|
|
}
|
|
|
|
|
|
2023-09-07 20:13:36 -06:00
|
|
|
export function InfoMessage({children, size = null}) {
|
|
|
|
|
return <Message info icon size={size}>
|
2023-08-23 20:14:23 -06:00
|
|
|
<SIcon name='question'/>
|
|
|
|
|
<Message.Content>
|
|
|
|
|
{children}
|
|
|
|
|
</Message.Content>
|
|
|
|
|
</Message>
|
2023-09-07 20:13:36 -06:00
|
|
|
}
|
|
|
|
|
|
2024-04-17 11:25:26 -06:00
|
|
|
export function HandPointMessage({children, size = null}) {
|
|
|
|
|
return <Message info icon size={size}>
|
|
|
|
|
<SIcon name='hand point right'/>
|
|
|
|
|
<Message.Content>
|
|
|
|
|
{children}
|
|
|
|
|
</Message.Content>
|
|
|
|
|
</Message>
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 21:45:06 -06:00
|
|
|
export function WarningMessage({children, size = null, icon = 'exclamation'}) {
|
2024-06-25 17:05:41 -06:00
|
|
|
// Use color='yellow' because "warning" does not work.
|
|
|
|
|
return <Message color='yellow' icon size={size}>
|
2024-08-15 20:03:22 -06:00
|
|
|
<SIcon name={icon}/>
|
2023-09-07 20:13:36 -06:00
|
|
|
<Message.Content>
|
|
|
|
|
{children}
|
|
|
|
|
</Message.Content>
|
|
|
|
|
</Message>
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 21:45:06 -06:00
|
|
|
export function ErrorMessage({children, size = null, icon = 'exclamation'}) {
|
2023-09-07 20:13:36 -06:00
|
|
|
return <Message error icon size={size}>
|
2024-08-15 20:03:22 -06:00
|
|
|
<SIcon name={icon}/>
|
2023-09-07 20:13:36 -06:00
|
|
|
<Message.Content>{children}</Message.Content>
|
|
|
|
|
</Message>
|
|
|
|
|
}
|
2023-11-21 13:01:52 -07:00
|
|
|
|
|
|
|
|
function levenshteinDistance(a, b) {
|
|
|
|
|
const matrix = [];
|
|
|
|
|
|
|
|
|
|
// Initialize the matrix
|
|
|
|
|
for (let i = 0; i <= b.length; i++) {
|
|
|
|
|
matrix[i] = [i];
|
|
|
|
|
}
|
|
|
|
|
for (let j = 0; j <= a.length; j++) {
|
|
|
|
|
matrix[0][j] = j;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate the matrix
|
|
|
|
|
for (let i = 1; i <= b.length; i++) {
|
|
|
|
|
for (let j = 1; j <= a.length; j++) {
|
|
|
|
|
if (b.charAt(i - 1) === a.charAt(j - 1)) {
|
|
|
|
|
matrix[i][j] = matrix[i - 1][j - 1];
|
|
|
|
|
} else {
|
|
|
|
|
matrix[i][j] = Math.min(matrix[i - 1][j - 1], matrix[i][j - 1], matrix[i - 1][j]) + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matrix[b.length][a.length];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fuzzyMatch(a, b, threshold = 3) {
|
|
|
|
|
return levenshteinDistance(a, b) <= threshold;
|
|
|
|
|
}
|
2024-02-19 13:45:41 -07:00
|
|
|
|
|
|
|
|
export function useIsIgnoredDirectory(directory) {
|
|
|
|
|
const {settings} = React.useContext(SettingsContext);
|
|
|
|
|
|
2024-02-20 09:49:53 -07:00
|
|
|
if (!settings || _.isEmpty(settings)) {
|
|
|
|
|
// Settings have not yet been fetched.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-19 13:45:41 -07:00
|
|
|
let ignoredDirectories = settings['ignored_directories'];
|
|
|
|
|
if (directory.endsWith('/')) {
|
|
|
|
|
ignoredDirectories = ignoredDirectories.map(i => `${i}/`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ignoredDirectories.indexOf(directory) >= 0;
|
|
|
|
|
}
|
2024-02-20 13:47:12 -07:00
|
|
|
|
|
|
|
|
export function getParentDirectory(filePath) {
|
|
|
|
|
// Remove trailing slashes for consistency
|
|
|
|
|
const normalizedPath = filePath.endsWith('/') ? filePath.slice(0, -1) : filePath;
|
|
|
|
|
|
|
|
|
|
// Find the last occurrence of "/" and extract the substring up to it
|
|
|
|
|
const parentDirectory = normalizedPath.substring(0, normalizedPath.lastIndexOf('/'));
|
|
|
|
|
|
|
|
|
|
return parentDirectory;
|
|
|
|
|
}
|
2024-02-27 18:54:14 -07:00
|
|
|
|
|
|
|
|
export function MultilineText({text, ...props}) {
|
|
|
|
|
return <div {...props}>
|
|
|
|
|
{text.split('\n').map((line, index, array) =>
|
2024-03-06 17:41:04 -07:00
|
|
|
index === array.length - 1 ? line : <p key={index}>{line}</p>
|
2024-02-27 18:54:14 -07:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
}
|
2024-03-06 17:41:04 -07:00
|
|
|
|
|
|
|
|
export const monthNames = [
|
|
|
|
|
'January',
|
|
|
|
|
'February',
|
|
|
|
|
'March',
|
|
|
|
|
'April',
|
|
|
|
|
'May',
|
|
|
|
|
'June',
|
|
|
|
|
'July',
|
|
|
|
|
'August',
|
|
|
|
|
'September',
|
|
|
|
|
'October',
|
|
|
|
|
'November',
|
|
|
|
|
'December',
|
|
|
|
|
];
|
2024-04-16 10:34:05 -06:00
|
|
|
|
2024-06-17 14:49:04 -06:00
|
|
|
export function IframeViewer({title, src, fallback, style, timeout = 5000}) {
|
|
|
|
|
// This function checks that an iframe can be fetched before displaying it. Otherwise, it will display the fallback
|
|
|
|
|
// element which should help the maintainer to fix the issue.
|
2024-06-17 10:40:30 -06:00
|
|
|
const [contentAvailable, setContentAvailable] = useState(false);
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
|
|
|
|
const {s} = React.useContext(ThemeContext);
|
|
|
|
|
fallback = fallback || <pre {...s}>Frame could not load.</pre>;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const controller = new AbortController(); // To manage fetch timeout
|
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
|
controller.abort(); // Abort the fetch after 5 seconds
|
|
|
|
|
}, timeout);
|
|
|
|
|
|
|
|
|
|
fetch(src, {signal: controller.signal})
|
|
|
|
|
.then(response => {
|
2024-06-17 14:49:04 -06:00
|
|
|
// Only display content if it can be fetched.
|
|
|
|
|
setContentAvailable(response.ok);
|
2024-06-17 10:40:30 -06:00
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
setContentAvailable(false); // Handle fetch errors (including aborts)
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setLoading(false); // Update loading state regardless of result
|
|
|
|
|
clearTimeout(timeoutId); // Clear the timeout
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return () => clearTimeout(timeoutId); // Cleanup timeout on unmount
|
|
|
|
|
}, [src]);
|
|
|
|
|
|
2024-06-17 14:49:04 -06:00
|
|
|
// Mobile needs less height, otherwise it hides the content below the screen.
|
|
|
|
|
let mobileStyle = {
|
|
|
|
|
position: 'fixed',
|
|
|
|
|
height: '80%',
|
|
|
|
|
width: '100%',
|
|
|
|
|
border: 'none',
|
|
|
|
|
padding: 0,
|
|
|
|
|
backgroundColor: '#FFFFFF',
|
|
|
|
|
};
|
|
|
|
|
// Allow provided `style` to overwrite.
|
|
|
|
|
mobileStyle = style ? {...mobileStyle, ...style} : mobileStyle;
|
|
|
|
|
let tabletStyle = {...mobileStyle, height: '93%'};
|
|
|
|
|
tabletStyle = style ? {...tabletStyle, ...style} : tabletStyle;
|
|
|
|
|
|
2024-06-17 10:40:30 -06:00
|
|
|
const iframeMedia = <>
|
2024-04-16 10:34:05 -06:00
|
|
|
<Media at='mobile'>
|
2024-06-17 14:49:04 -06:00
|
|
|
<iframe title={title} src={src} style={mobileStyle}/>
|
2024-04-16 10:34:05 -06:00
|
|
|
</Media>
|
|
|
|
|
<Media greaterThan='mobile'>
|
2024-06-17 14:49:04 -06:00
|
|
|
<iframe title={title} src={src} style={tabletStyle}/>
|
2024-04-16 10:34:05 -06:00
|
|
|
</Media>
|
2024-06-17 10:40:30 -06:00
|
|
|
</>;
|
|
|
|
|
|
|
|
|
|
const dimmer = <DimmerDimmable as={Segment} dimmed={true}><Dimmer active><Loader/></Dimmer></DimmerDimmable>;
|
|
|
|
|
return <>
|
|
|
|
|
{loading ? dimmer
|
|
|
|
|
: contentAvailable ? iframeMedia
|
|
|
|
|
: fallback
|
|
|
|
|
}
|
2024-04-16 10:34:05 -06:00
|
|
|
</>
|
2024-05-31 23:53:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function roundDigits(value, decimals = 2) {
|
|
|
|
|
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
|
|
|
|
|
}
|
2024-06-25 17:05:41 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
export function Breadcrumbs({crumbs, size = undefined}) {
|
|
|
|
|
function getSection(crumb) {
|
|
|
|
|
const {text, link, icon} = crumb;
|
|
|
|
|
let contents = text;
|
|
|
|
|
if (link) {
|
|
|
|
|
contents = <Link to={link}>
|
|
|
|
|
{icon && <Icon name={icon}/>}
|
|
|
|
|
{text}
|
|
|
|
|
</Link>;
|
|
|
|
|
}
|
|
|
|
|
return <BreadcrumbSection>{contents}</BreadcrumbSection>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <Breadcrumb size={size}>
|
|
|
|
|
{crumbs.map((crumb, index) => (
|
|
|
|
|
<React.Fragment key={index}>
|
|
|
|
|
{getSection(crumb)}
|
|
|
|
|
{index < crumbs.length - 1 && <BreadcrumbDivider icon='right chevron'/>}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
))}
|
|
|
|
|
</Breadcrumb>
|
|
|
|
|
}
|
2024-06-25 22:12:13 -06:00
|
|
|
|
|
|
|
|
const validUrlRegex = /^(http|https):\/\/[^ "]+$/;
|
|
|
|
|
|
|
|
|
|
export function validURL(url) {
|
|
|
|
|
return !(url && !validUrlRegex.test(url));
|
2024-10-05 14:06:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useLocalStorage(key, initialValue, decode = JSON.parse, encode = JSON.stringify) {
|
|
|
|
|
// Use localstorage to store some JSON encode-able.
|
|
|
|
|
|
|
|
|
|
// Initialize state with the value from localStorage or initial value
|
|
|
|
|
const [value, setValue] = useState(() => {
|
|
|
|
|
try {
|
|
|
|
|
const item = window.localStorage.getItem(key);
|
|
|
|
|
// Parse the stored item (integer, bool, etc.). Use the initial value if empty.
|
|
|
|
|
return item ? decode(item) : initialValue;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error reading localStorage:', error);
|
|
|
|
|
return initialValue;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Save to localStorage when the value changes
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
try {
|
|
|
|
|
window.localStorage.setItem(key, encode(value));
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error setting localStorage:', error);
|
|
|
|
|
}
|
|
|
|
|
}, [key, value, encode]);
|
|
|
|
|
|
|
|
|
|
// Return the stored value and a function to update it
|
|
|
|
|
return [value, setValue];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useLocalStorageInt(key, initialValue) {
|
|
|
|
|
// Use localStorage to store an integer.
|
|
|
|
|
const [storedValue, setStoredValue] = useLocalStorage(key, initialValue, parseInt, (num) => num.toString());
|
|
|
|
|
return [storedValue, setStoredValue];
|
|
|
|
|
}
|