ente/src/pages/gallery/index.tsx

67 lines
1.9 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-07 11:10:49 +00:00
import { file, getFiles } 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 { PhotoSwipeGallery } from 'react-photoswipe';
const Container = styled.div`
max-width: 1260px;
display: flex;
flex-wrap: wrap;
margin: 0 auto;
.pswp-thumbnail {
display: inline-block;
cursor: pointer;
}
`;
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[]>();
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-09-20 15:18:35 +00:00
const resp = await getFiles("0", token, "24", encryptionKey);
setLoading(false);
setData(resp);
};
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>
}
2020-09-20 15:18:35 +00:00
const getThumbnail = (item) => (
2020-11-07 11:10:49 +00:00
<PreviewCard data={item} />
2020-09-20 15:18:35 +00:00
)
return (<Container>
<PhotoSwipeGallery
items={data.map(item => ({
...item,
src: '/image.svg',
2020-10-03 14:21:56 +00:00
w: window.innerWidth,
h: window.innerHeight,
2020-09-20 15:18:35 +00:00
}))}
thumbnailContent={getThumbnail}
/>
</Container>);
}