ente/src/pages/gallery/index.tsx

189 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-09-20 15:18:35 +00:00
import React, { useEffect, useState } from 'react';
import { useRouter } from 'next/router';
import Spinner from 'react-bootstrap/Spinner';
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
2020-11-24 04:40:02 +00:00
import { file, getFile, getFiles, getPreview } from 'services/fileService';
import { getData, LS_KEYS } from 'utils/storage/localStorage';
import PreviewCard from './components/PreviewCard';
import { getActualKey } from 'utils/common/key';
2020-09-20 15:18:35 +00:00
import styled from 'styled-components';
import { PhotoSwipe } from 'react-photoswipe';
import { Options } from 'photoswipe';
2020-11-20 17:24:21 +00:00
import AutoSizer from 'react-virtualized-auto-sizer';
import { FixedSizeList as List } from 'react-window';
2020-09-20 15:18:35 +00:00
const Container = styled.div`
2020-11-20 17:24:21 +00:00
display: block;
flex: 1;
width: 100%;
2020-09-20 15:18:35 +00:00
flex-wrap: wrap;
margin: 0 auto;
.pswp-thumbnail {
display: inline-block;
cursor: pointer;
}
`;
2020-11-20 17:24:21 +00:00
const ListItem = styled.div`
display: flex;
justify-content: center;
`;
2020-11-07 15:43:00 +00:00
const PAGE_SIZE = 12;
const COLUMNS = 3;
export default function Gallery() {
const router = useRouter();
const [loading, setLoading] = useState(false);
2020-11-07 11:10:49 +00:00
const [data, setData] = useState<file[]>();
const [open, setOpen] = useState(false);
const [options, setOptions] = useState<Options>({
history: false,
});
2020-11-24 04:40:02 +00:00
const fetching: { [k: number]: boolean } = {};
2020-09-20 15:18:35 +00:00
useEffect(() => {
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
const token = getData(LS_KEYS.USER).token;
if (!key) {
router.push("/");
}
const main = async () => {
setLoading(true);
const encryptionKey = await getActualKey();
2020-11-20 11:22:13 +00:00
const resp = await getFiles("0", token, "100", encryptionKey);
setLoading(false);
setData(resp.map(item => ({
...item,
w: window.innerWidth,
h: window.innerHeight,
})));
};
main();
}, []);
if (!data || loading) {
2020-09-20 15:18:35 +00:00
return <div className="text-center">
2020-11-07 11:10:49 +00:00
<Spinner animation="border" variant="primary" />;
2020-09-20 15:18:35 +00:00
</div>
}
const updateUrl = (index: number) => (url: string) => {
2020-11-24 04:40:02 +00:00
data[index] = {
...data[index],
msrc: url,
w: window.innerWidth,
h: window.innerHeight,
}
if (!data[index].src) {
data[index].src = url;
}
setData(data);
}
const updateSrcUrl = (index: number, url: string) => {
data[index] = {
...data[index],
src: url,
2020-11-24 04:40:02 +00:00
w: window.innerWidth,
h: window.innerHeight,
}
setData(data);
}
const handleClose = () => {
setOpen(false);
}
const onThumbnailClick = (index: number) => () => {
setOptions({
...options,
index,
});
setOpen(true);
}
const getThumbnail = (data: file[], index: number) => (
<PreviewCard
2020-11-24 04:40:02 +00:00
key={`tile-${index}`}
data={data[index]}
updateUrl={updateUrl(index)}
onClick={onThumbnailClick(index)}
/>
2020-09-20 15:18:35 +00:00
)
2020-11-24 04:40:02 +00:00
const getSlideData = async (instance: any, index: number, item: file) => {
const token = getData(LS_KEYS.USER).token;
if (!item.msrc) {
const url = await getPreview(token, item);
updateUrl(index)(url);
item.msrc = url;
if (!item.src) {
item.src = url;
}
item.w = window.innerWidth;
item.h = window.innerHeight;
if (instance.framework.getScrollY() > 0) {
instance.invalidateCurrItems();
instance.updateSize(true);
}
}
if ((!item.src || item.src === item.msrc) && !fetching[index]) {
fetching[index] = true;
const url = await getFile(token, item);
updateSrcUrl(index, url);
item.src = url;
item.w = window.innerWidth;
item.h = window.innerHeight;
if (instance.framework.getScrollY() > 0) {
instance.invalidateCurrItems();
instance.updateSize(true);
}
}
}
2020-09-20 15:18:35 +00:00
return (<Container>
2020-11-20 17:24:21 +00:00
<AutoSizer>
2020-11-23 06:20:00 +00:00
{({ height, width }) => {
let columns;
if (width >= 1000) {
columns = 5;
} else if (width < 1000 && width >= 450) {
columns = 3;
} else if (width < 450 && width >= 300) {
columns = 2;
} else {
columns = 1;
}
return (
<List
itemSize={200}
height={height}
width={width}
itemCount={data.length / columns}
>
{({ index, style }) => {
const arr = [];
for (let i = 0; i < columns; i++) {
arr.push(index * columns + i);
}
return (<ListItem style={style}>
{arr.map(i => getThumbnail(data, i))}
</ListItem>);
}}
</List>
)
}}
2020-11-20 17:24:21 +00:00
</AutoSizer>
<PhotoSwipe
isOpen={open}
items={data}
options={options}
onClose={handleClose}
2020-11-24 04:40:02 +00:00
gettingData={getSlideData}
/>
2020-09-20 15:18:35 +00:00
</Container>);
}