initial commit
This commit is contained in:
commit
9b18db6e8b
24
.gitignore
vendored
Normal file
24
.gitignore
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
/.pnp
|
||||
.pnp.js
|
||||
|
||||
# testing
|
||||
/coverage
|
||||
|
||||
# production
|
||||
/build
|
||||
|
||||
# misc
|
||||
.DS_Store
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
create-react-app-README.md
|
||||
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
22
README.md
Normal file
22
README.md
Normal file
@ -0,0 +1,22 @@
|
||||
# reddit-slideshow
|
||||
Site that displays a slideshow with images/gifs obtained from reddit. By default images will be fetched from reddit frontpage.
|
||||
|
||||
Currently hosted at https://redditslideshow.netlify.com/
|
||||
|
||||
## Usage
|
||||
You can go to the next/previous slide by pressing `A` or `D`, or by using the arrow keys.
|
||||
|
||||
You can add, for example, `/r/pics` to the url and data will be fetched from reddit.com/r/pics. Try the following URLs:
|
||||
|
||||
- https://redditslideshow.netlify.com/r/pics
|
||||
- https://redditslideshow.netlify.com/r/art
|
||||
- https://redditslideshow.netlify.com/r/aww
|
||||
|
||||
## Todo
|
||||
- Detect / handle invalid URLs.
|
||||
- Better error handling.
|
||||
- NSFW filter (some logic already implemented).
|
||||
|
||||
---
|
||||
|
||||
Made with React using `create-react-app`, based on http://redditp.com/
|
12608
package-lock.json
generated
Normal file
12608
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
32
package.json
Normal file
32
package.json
Normal file
@ -0,0 +1,32 @@
|
||||
{
|
||||
"name": "reddit-slideshow",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"axios": "^0.19.0",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-scripts": "3.0.1"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "react-scripts start",
|
||||
"build": "react-scripts build",
|
||||
"test": "react-scripts test",
|
||||
"eject": "react-scripts eject"
|
||||
},
|
||||
"eslintConfig": {
|
||||
"extends": "react-app"
|
||||
},
|
||||
"browserslist": {
|
||||
"production": [
|
||||
">0.2%",
|
||||
"not dead",
|
||||
"not op_mini all"
|
||||
],
|
||||
"development": [
|
||||
"last 1 chrome version",
|
||||
"last 1 firefox version",
|
||||
"last 1 safari version"
|
||||
]
|
||||
}
|
||||
}
|
1
public/_redirects
Normal file
1
public/_redirects
Normal file
@ -0,0 +1 @@
|
||||
/* /index.html 200
|
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
38
public/index.html
Normal file
38
public/index.html
Normal file
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="theme-color" content="#000000" />
|
||||
<!--
|
||||
manifest.json provides metadata used when your web app is installed on a
|
||||
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
|
||||
-->
|
||||
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
|
||||
<!--
|
||||
Notice the use of %PUBLIC_URL% in the tags above.
|
||||
It will be replaced with the URL of the `public` folder during the build.
|
||||
Only files inside the `public` folder can be referenced from the HTML.
|
||||
|
||||
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
|
||||
work correctly both with client-side routing and a non-root public URL.
|
||||
Learn how to configure a non-root public URL by running `npm run build`.
|
||||
-->
|
||||
<title>Reddit slideshow</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>You need to enable JavaScript to run this app.</noscript>
|
||||
<div id="root"></div>
|
||||
<!--
|
||||
This HTML file is a template.
|
||||
If you open it directly in the browser, you will see an empty page.
|
||||
|
||||
You can add webfonts, meta tags, or analytics to this file.
|
||||
The build step will place the bundled scripts into the <body> tag.
|
||||
|
||||
To begin the development, run `npm start` or `yarn start`.
|
||||
To create a production bundle, use `npm run build` or `yarn build`.
|
||||
-->
|
||||
</body>
|
||||
</html>
|
15
public/manifest.json
Normal file
15
public/manifest.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"short_name": "Reddit Slideshow",
|
||||
"name": "Reddit Slideshow",
|
||||
"icons": [
|
||||
{
|
||||
"src": "favicon.ico",
|
||||
"sizes": "64x64 32x32 24x24 16x16",
|
||||
"type": "image/x-icon"
|
||||
}
|
||||
],
|
||||
"start_url": ".",
|
||||
"display": "standalone",
|
||||
"theme_color": "#000000",
|
||||
"background_color": "#ffffff"
|
||||
}
|
4
src/assets/githubLogo.svg
Normal file
4
src/assets/githubLogo.svg
Normal file
@ -0,0 +1,4 @@
|
||||
<svg role="img" viewBox="0 0 24 24" width="24" height="24" xmlns="http://www.w3.org/2000/svg">
|
||||
<title>GitHub icon</title>
|
||||
<path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/>
|
||||
</svg>
|
After Width: | Height: | Size: 858 B |
1
src/assets/leftArrow-24px.svg
Normal file
1
src/assets/leftArrow-24px.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path opacity=".87" fill="none" d="M24 0v24H0V0h24z"/><path d="M14 7l-5 5 5 5V7z"/></svg>
|
After Width: | Height: | Size: 172 B |
1
src/assets/nextSlide-24px.svg
Normal file
1
src/assets/nextSlide-24px.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path opacity=".87" fill="none" d="M24 24H0V0h24v24z"/><path d="M6.49 20.13l1.77 1.77 9.9-9.9-9.9-9.9-1.77 1.77L14.62 12l-8.13 8.13z"/></svg>
|
After Width: | Height: | Size: 224 B |
4
src/assets/prevSlide-24px.svg
Normal file
4
src/assets/prevSlide-24px.svg
Normal file
@ -0,0 +1,4 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
|
||||
<path opacity=".87" fill="none" d="M0 0h24v24H0V0z"/>
|
||||
<path d="M17.51 3.87L15.73 2.1 5.84 12l9.9 9.9 1.77-1.77L9.38 12l8.13-8.13z"/>
|
||||
</svg>
|
After Width: | Height: | Size: 228 B |
1
src/assets/rightArrow-24px.svg
Normal file
1
src/assets/rightArrow-24px.svg
Normal file
@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path d="M10 17l5-5-5-5v10z"/></svg>
|
After Width: | Height: | Size: 158 B |
203
src/components/Layout/Layout.css
Normal file
203
src/components/Layout/Layout.css
Normal file
@ -0,0 +1,203 @@
|
||||
.ui {
|
||||
z-index: 3;
|
||||
transition: opacity 1s;
|
||||
}
|
||||
|
||||
.ui.hideUI {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.ui.showUI {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.layout {
|
||||
position:relative;
|
||||
height: 100%;
|
||||
z-index: 0;
|
||||
}
|
||||
|
||||
.hideCursor {
|
||||
cursor: none;
|
||||
}
|
||||
|
||||
header {
|
||||
position:absolute;
|
||||
text-align: justify;
|
||||
display: flex;
|
||||
transition: left 1s;
|
||||
background-color: rgba(0,0,0,0.5);
|
||||
z-index: 3;
|
||||
}
|
||||
|
||||
header.showTitle {
|
||||
left:0px;
|
||||
}
|
||||
|
||||
header.hideTitle {
|
||||
left:-451px;
|
||||
}
|
||||
|
||||
.toggleTitleButton {
|
||||
height: 46px;
|
||||
width: 46px;
|
||||
fill: #ddd;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 20px;
|
||||
font-family: Arial;
|
||||
color: white;
|
||||
width: 450px;
|
||||
height: auto;
|
||||
padding: 10px 10px;
|
||||
|
||||
text-align: justify;
|
||||
transition: width 1s;
|
||||
}
|
||||
|
||||
.toggleButton {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.navButton {
|
||||
position: absolute;
|
||||
cursor: pointer;
|
||||
top: 50%;
|
||||
transform: translate(0, -50%);
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
fill: #eee;
|
||||
background-color: rgba(0,0,0,0.5);
|
||||
z-index: 3;
|
||||
}
|
||||
|
||||
.prevButton {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.nextButton {
|
||||
right: 0;
|
||||
}
|
||||
|
||||
.info {
|
||||
color: #ddd;
|
||||
z-index: 3;
|
||||
position: absolute;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
bottom: 0;
|
||||
background-color: rgba(0,0,0,0.5);
|
||||
transition: left 1s;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.info.showInfo {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
.info.hideInfo {
|
||||
/* left: -281px; with nsfw checkbox */
|
||||
left: -201px;
|
||||
}
|
||||
|
||||
|
||||
.infoButtons {
|
||||
text-align: center;
|
||||
width: 200px;
|
||||
/* width: 280px; with nsfw checkbox */
|
||||
}
|
||||
|
||||
.infoRow {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.infoButtons a {
|
||||
color: #ddd;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.infoButtons a:hover {
|
||||
text-decoration: underline #bbb dotted;
|
||||
}
|
||||
|
||||
.infoElement {
|
||||
display: inline;
|
||||
padding: 3px 8px;
|
||||
}
|
||||
|
||||
.infoElement label {
|
||||
padding-left: 2px;
|
||||
}
|
||||
|
||||
.githubLogo {
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
height: 40px;
|
||||
width: 40px;
|
||||
padding: 5px;
|
||||
fill: #ddd;
|
||||
background-color: rgba(0,0,0,0.5);
|
||||
}
|
||||
|
||||
.githubLogo:hover{
|
||||
fill: white;
|
||||
}
|
||||
|
||||
.toggleInfoButton{
|
||||
height: 35px;
|
||||
width: 35px;
|
||||
fill: #ccc;
|
||||
}
|
||||
|
||||
@media (max-width: 500px) {
|
||||
.title {
|
||||
font-size: 10pt;
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
header.hideTitle {
|
||||
left: -301px;
|
||||
}
|
||||
|
||||
.infoButtons {
|
||||
flex-direction: column;
|
||||
width: 100px;
|
||||
}
|
||||
|
||||
.infoRow {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.info.hideInfo {
|
||||
left: -101px;
|
||||
}
|
||||
|
||||
.navButton {
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
}
|
||||
|
||||
.toggleInfoButton {
|
||||
align-self: flex-start;
|
||||
}
|
||||
|
||||
.githubLogo {
|
||||
height: 35px;
|
||||
width: 35px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 350px) {
|
||||
.title {
|
||||
width: 200px;
|
||||
}
|
||||
|
||||
header.hideTitle {
|
||||
left: -201px;
|
||||
}
|
||||
}
|
87
src/components/Layout/Layout.js
Normal file
87
src/components/Layout/Layout.js
Normal file
@ -0,0 +1,87 @@
|
||||
import React from 'react';
|
||||
|
||||
import Slide from '../Slide/Slide';
|
||||
import './Layout.css';
|
||||
import { ReactComponent as PrevSlideSVG } from '../../assets/prevSlide-24px.svg';
|
||||
import { ReactComponent as NextSlideSVG } from '../../assets/nextSlide-24px.svg';
|
||||
import { ReactComponent as LeftArrowSVG } from '../../assets/leftArrow-24px.svg';
|
||||
import { ReactComponent as RightArrowSVG } from '../../assets/rightArrow-24px.svg';
|
||||
import { ReactComponent as GithubLogo } from '../../assets/githubLogo.svg';
|
||||
|
||||
const layout = (props) => {
|
||||
const link = props.post ? props.post.link : null;
|
||||
const id = props.post ? props.post.id : null;
|
||||
const title = props.post ? props.post.title : null;
|
||||
const urlToComments = props.post ? props.post.urlToComments : null;
|
||||
const prevLink = props.prev ? props.prev.link : null;
|
||||
const prevId = props.prev ? props.prev.id : null;
|
||||
// nsfw: (true/false)
|
||||
// console.log('link', link);
|
||||
// console.log('post', props.post);
|
||||
|
||||
const headerClasses = [ props.showTitle ? 'showTitle' : 'hideTitle'];
|
||||
|
||||
const infoClasses = ['info'];
|
||||
infoClasses.push( props.showInfo ? 'showInfo' : 'hideInfo' );
|
||||
|
||||
const uiClasses = ['ui'];
|
||||
uiClasses.push( props.hideUI ? 'hideUI' : 'showUI' );
|
||||
|
||||
const slideClasses = ['slide'];
|
||||
if ( props.hideUI ) {
|
||||
slideClasses.push('hideCursor');
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div className="layout" onTouchStart={props.touchStart} onTouchEnd={props.touchEnd}>
|
||||
<div className={ uiClasses.join(' ') }>
|
||||
<header className={headerClasses.join(' ')}>
|
||||
<h1 className="title">
|
||||
{title}
|
||||
</h1>
|
||||
{ props.showTitle
|
||||
? <LeftArrowSVG className="toggleTitleButton toggleButton" onClick={props.titleClick}/>
|
||||
: <RightArrowSVG className="toggleTitleButton toggleButton" onClick={props.titleClick}/> }
|
||||
</header>
|
||||
|
||||
<NextSlideSVG className="navButton nextButton" onClick={ props.nextHandler }/>
|
||||
<PrevSlideSVG className="navButton prevButton" onClick={ props.prevHandler }/>
|
||||
|
||||
<div className={infoClasses.join(' ')}>
|
||||
<div className="infoButtons">
|
||||
<div className="infoRow">
|
||||
<a target="_blank" className="infoElement" href= { urlToComments } rel="noopener noreferrer">Comments</a>
|
||||
<a target="_blank" className="infoElement" href= { link } rel="noopener noreferrer">Direct link</a>
|
||||
{/*<div className="infoElement">
|
||||
<input type="checkbox" id="showNSFW" name="showNSFW" onChange={props.nsfwCheckboxHandler} checked={props.nsfwChecked}/><label htmlFor="showNSFW">NSFW</label>
|
||||
</div>*/}
|
||||
</div>
|
||||
<div className="infoRow">
|
||||
<div className="infoElement">
|
||||
<input type="checkbox" id="auto" name="auto" onChange={props.autoCheckboxHandler} checked={props.autoPlay}/>
|
||||
<label htmlFor="auto">Auto</label>
|
||||
</div>
|
||||
<div className="infoElement">
|
||||
<input type="checkbox" id="hideUI" name="hideUI" onChange={props.hideUICheckboxHandler} checked={props.hideUIChecked}/>
|
||||
<label htmlFor="hideUI">Hide UI</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{ props.showInfo
|
||||
? <LeftArrowSVG className="toggleInfoButton toggleButton" onClick={props.infoClick}/>
|
||||
: <RightArrowSVG className="toggleInfoButton toggleButton" onClick={props.infoClick}/> }
|
||||
</div>
|
||||
|
||||
<a href="https://github.com/ismaelpadilla/reddit-slideshow" target="_blank" rel="noopener noreferrer">
|
||||
<GithubLogo className="githubLogo"/>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
{ link ? <Slide url={ link } classes={ slideClasses.concat(['current']) } key={ id }/> : null }
|
||||
{ props.prev !== props.post ? <Slide url={ prevLink } classes={ slideClasses.concat(['prev']) } key={ prevId } /> : null }
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default layout;
|
47
src/components/Slide/Slide.css
Normal file
47
src/components/Slide/Slide.css
Normal file
@ -0,0 +1,47 @@
|
||||
.slide {
|
||||
position: absolute;
|
||||
top:0;
|
||||
height:100%;
|
||||
width:100%;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
background-size: contain;
|
||||
}
|
||||
|
||||
.current {
|
||||
opacity: 1;
|
||||
animation: fadeIn 1s;
|
||||
z-index: -1;
|
||||
}
|
||||
|
||||
.prev {
|
||||
opacity: 0;
|
||||
animation: fadeOut 1s;
|
||||
z-index: -2;
|
||||
}
|
||||
|
||||
.video {
|
||||
position: fixed;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
min-width: 100%;
|
||||
min-height: 100%;
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeOut {
|
||||
from {
|
||||
opacity: 1;
|
||||
}
|
||||
to {
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
49
src/components/Slide/Slide.js
Normal file
49
src/components/Slide/Slide.js
Normal file
@ -0,0 +1,49 @@
|
||||
import React from 'react';
|
||||
|
||||
import './Slide.css'
|
||||
|
||||
const slide = (props) => {
|
||||
|
||||
// get file extension
|
||||
const extPattern = /\.[0-9a-z]+$/i;
|
||||
const match = props.url.match(extPattern);
|
||||
const fileExt = match ? match[0] : null;
|
||||
if (props.classes.includes('current')) {
|
||||
console.log("Attempting to show", props.url);
|
||||
}
|
||||
// console.log (props.classes);
|
||||
// console.log('extension', fileExt);
|
||||
|
||||
// console.log('domain', domain);
|
||||
|
||||
// imgur urls are case sensitive!
|
||||
// const afterDomain = props.url.match(/[.0-9a-zA-Z]+$/)[0];
|
||||
// console.log('afterdomain', afterDomain);
|
||||
|
||||
if (fileExt) {
|
||||
// Imgur videos can be linked by using .mp4 extension instead of .gifv
|
||||
if ( fileExt === ".gifv" ) {
|
||||
return (
|
||||
<video autoPlay muted loop className= { props.classes.join(' ') + ' video' }>
|
||||
<source src={ props.url.replace(".gifv", ".mp4") } type="video/mp4"/>
|
||||
</video>
|
||||
);
|
||||
} else if ( fileExt === ".mp4" ) {
|
||||
return (
|
||||
<video autoPlay muted loop className= { props.classes.join(' ') + ' video' }>
|
||||
<source src={ props.url.replace('.mp4', '.webm') } type="video/webm"/>
|
||||
<source src={ props.url } type="video/mp4"/>
|
||||
</video>
|
||||
);
|
||||
}
|
||||
// Regular picture
|
||||
const mystyle= { backgroundImage:`url(${props.url})` };
|
||||
return <div style={ mystyle } className= { props.classes.join(' ') } />;
|
||||
}
|
||||
|
||||
return(
|
||||
<div>Something went wrong.</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default slide;
|
13
src/containers/App.css
Normal file
13
src/containers/App.css
Normal file
@ -0,0 +1,13 @@
|
||||
.App {
|
||||
position: relative;
|
||||
background-color: black;
|
||||
height: 100vh;
|
||||
max-height: 100%;
|
||||
width: 100vw;
|
||||
}
|
||||
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
392
src/containers/App.js
Normal file
392
src/containers/App.js
Normal file
@ -0,0 +1,392 @@
|
||||
import React, { Component } from 'react';
|
||||
import axios from 'axios';
|
||||
|
||||
import Layout from '../components/Layout/Layout';
|
||||
import './App.css';
|
||||
|
||||
class App extends Component {
|
||||
hideUI = false;
|
||||
state = {
|
||||
// request: '/r/pics.json',
|
||||
request: window.location.pathname + '.json',
|
||||
posts: [],
|
||||
currentPost: -1,
|
||||
prevPost: 0,
|
||||
after: '', // to be sent as a part of a request, see reddit api docs
|
||||
awaitingResponse: false,
|
||||
showTitle: true, // show image title at top left
|
||||
showInfo: true, // show info and buttons at bottom right
|
||||
auto: false, // autoplay
|
||||
hideUI: this.hideUI,
|
||||
hideUIChecked: this.hideUI,
|
||||
showNSWF: true,
|
||||
touchStartX: 0 // used for swipe gestures in mobile
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* This makes sure we access the right reddit url based on how the user
|
||||
* accesses our website.
|
||||
*/
|
||||
componentDidMount = () => {
|
||||
// Get initial posts via http, set state
|
||||
if ( !this.state.awaitingResponse ) {
|
||||
this.setState( { awaitingResponse: true } );
|
||||
axios.get( this.state.request )
|
||||
.then(
|
||||
response => {
|
||||
// posts are in response.data.data.children
|
||||
const after = response.data.data.after;
|
||||
this.getPosts(response.data.data.children).then ( (posts) =>
|
||||
{
|
||||
// console.log('after', after);
|
||||
// console.log('reduced posts', posts);
|
||||
this.setState( {
|
||||
posts: [...posts],
|
||||
after: after,
|
||||
currentPost: 0,
|
||||
awaitingResponse: false
|
||||
// interval: setInterval(this.nextSlideHandler, 2000)
|
||||
});
|
||||
if (this.state.auto) {
|
||||
this.setState({ interval: setInterval(this.nextSlideHandler, 5000) });
|
||||
}
|
||||
// console.log("initialized state", this.state);
|
||||
}).catch(function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
}
|
||||
document.addEventListener('mousemove', this.mouseMoveHandler);
|
||||
document.addEventListener('keydown', this.keyDownHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent unnecessary rerenders.
|
||||
*/
|
||||
shouldComponentUpdate = (nextProps, nextState) => {
|
||||
/**
|
||||
* Do not rerender on touch start
|
||||
* the touch start event modifies the state but it doesn't have an impact
|
||||
* on the elements being shown.
|
||||
*/
|
||||
if (this.state.touchStartX !== nextState.touchStartX){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle key presses.
|
||||
* A or arrow left to go left, D or arrow right to go right.
|
||||
*/
|
||||
keyDownHandler = (event) => {
|
||||
if (event.key === "ArrowLeft" || event.code === "KeyA") {
|
||||
this.prevSlideHandler();
|
||||
} else if (event.key === "ArrowRight" || event.code === "KeyD") {
|
||||
this.nextSlideHandler();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get posts from a server response
|
||||
*
|
||||
* post {
|
||||
* title:
|
||||
* id:
|
||||
* url:
|
||||
* urlToComments:
|
||||
* nsfw: (true/false)
|
||||
* }
|
||||
*/
|
||||
getPosts = (children) => {
|
||||
// Filter and map at the same time
|
||||
const posts = children.reduce( this.filterPosts, [] );
|
||||
// Update the posts, get urls to gfycat mp4s (see updatePosts fore more info).
|
||||
const updatedPosts = this.updatePosts(posts);
|
||||
// console.log('updated posts', updatedPosts);
|
||||
return updatedPosts;
|
||||
};
|
||||
|
||||
/**
|
||||
* Map an array of reddit posts so as to only keep relevant information,
|
||||
* and filter self posts, posts with unsuppored exentensions, etc.
|
||||
*/
|
||||
filterPosts = (filtered, child) => {
|
||||
// is_self is true for self posts
|
||||
if (!child.data.is_self) {
|
||||
// Will return either a filter url or null if url is invalid
|
||||
const filteredUrl = this.filterUrl(child.data.url);
|
||||
if (filteredUrl) {
|
||||
filtered.push({
|
||||
title: child.data.title,
|
||||
id: child.data.id,
|
||||
nsfw: child.data.over_18,
|
||||
urlToComments: 'https://www.reddit.com' + child.data.permalink,
|
||||
link: filteredUrl
|
||||
});
|
||||
}
|
||||
}
|
||||
// console.log("returning filterPosts");
|
||||
return filtered;
|
||||
};
|
||||
|
||||
/**
|
||||
* Only keep valid urls and extensions.
|
||||
* Returns a valid url or null if url parameter isn't valid.
|
||||
*/
|
||||
filterUrl = (url) => {
|
||||
const domain = url.match(/:\/\/(.+)\//)[1];
|
||||
const extPattern = /\.[0-9a-z]+$/i;
|
||||
const match = url.match(extPattern);
|
||||
const fileExt = match ? match[0] : null;
|
||||
const supportedExtensions = [".jpg", ".jpeg", ".png", ".bmp",".gif", ".gifv"];
|
||||
if ( domain === "gfycat.com" || supportedExtensions.includes(fileExt) ) {
|
||||
return url;
|
||||
}
|
||||
if ( domain === "imgur.com" ) {
|
||||
return url + ".jpg";
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* This is mainly to make sure we get the right gfycat url.
|
||||
* This is because gfycats urls on reddit will have the form
|
||||
* "https://gfycat.com/somewords" in lowercase, so we must get the .mp4
|
||||
* link, which is case sensitive (i.e. "https://giant.gfycat.com/SomeWords.mp4").
|
||||
* We do this by calling the gfycat API, see:
|
||||
* https://developers.gfycat.com/api/#getting-gfycats
|
||||
*/
|
||||
updatePosts = async (posts) => {
|
||||
// console.log(posts);
|
||||
// posts.forEach( async (post) => {
|
||||
let counter = 0;
|
||||
for (const post of posts) {
|
||||
// console.log(post1);
|
||||
const domain = post.link.match(/:\/\/(.+)\//)[1];
|
||||
|
||||
// In order to play gfycat videos we must get the mp4 url from the video id
|
||||
if (domain === 'gfycat.com') {
|
||||
const gfyId = post.link.match(/([.0-9a-zA-Z]+)(-|$)/)[1];
|
||||
/*
|
||||
* gfyId regex clarification: sometimes gfys have dashes ('-') in their url.
|
||||
* These are not part of the gfy ID.
|
||||
*/
|
||||
|
||||
// Wait until some images are correct so we can render something
|
||||
if ( counter < 3) {
|
||||
const response = await axios.get( "https://api.gfycat.com/v1/gfycats/" + gfyId );
|
||||
post.link = response.data.gfyItem.mp4Url;
|
||||
} else {
|
||||
axios.get( "https://api.gfycat.com/v1/gfycats/" + gfyId ).then( (response) =>
|
||||
{
|
||||
post.link = response.data.gfyItem.mp4Url;
|
||||
}).catch(function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
};
|
||||
return posts;
|
||||
};
|
||||
|
||||
/**
|
||||
* Go to previous slide.
|
||||
*/
|
||||
prevSlideHandler = () => {
|
||||
if ( this.state.currentPost > 0 ) {
|
||||
this.setState( (prevState, props) => {
|
||||
return {
|
||||
prevPost: prevState.currentPost,
|
||||
currentPost: prevState.currentPost -1
|
||||
};
|
||||
});
|
||||
}
|
||||
console.log("Prev");
|
||||
};
|
||||
|
||||
/**
|
||||
* Go to next slide.
|
||||
*/
|
||||
nextSlideHandler = () => {
|
||||
if ( this.state.currentPost < this.state.posts.length -1 ) {
|
||||
this.setState( (prevState, props) => {
|
||||
return {
|
||||
prevPost: prevState.currentPost,
|
||||
currentPost: prevState.currentPost +1
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
// Load more posts when close to reaching the end
|
||||
if ( this.state.currentPost > this.state.posts.length -4 ) {
|
||||
if ( !this.state.awaitingResponse ) {
|
||||
this.setState( { awaitingResponse: true } );
|
||||
console.log('fetching aditional posts');
|
||||
axios.get(this.state.request + '?after=' + this.state.after)
|
||||
.then(
|
||||
response => {
|
||||
// console.log(response); // posts are in response.data.data.children
|
||||
const after = response.data.data.after;
|
||||
this.getPosts(response.data.data.children).then ( (posts) =>
|
||||
{
|
||||
// console.log('after', after);
|
||||
// console.log('reduced posts', posts);
|
||||
this.setState( (state, props) => {
|
||||
return {
|
||||
posts: [...state.posts, ...posts],
|
||||
after: after,
|
||||
awaitingResponse: false
|
||||
}
|
||||
});
|
||||
}).catch(function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.log(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
console.log("Next");
|
||||
};
|
||||
|
||||
/**
|
||||
* Hide/show title at the top right.
|
||||
*/
|
||||
toggleTitleHandler = () => {
|
||||
this.setState( (state,props) => {
|
||||
return {
|
||||
showTitle: ! state.showTitle
|
||||
}
|
||||
} );
|
||||
};
|
||||
|
||||
/**
|
||||
* Hide/show info panel at the bottom left.
|
||||
*/
|
||||
toggleInfoHandler = () => {
|
||||
console.log('toggle info');
|
||||
this.setState( (state,props) => {
|
||||
return {
|
||||
showInfo: ! state.showInfo
|
||||
}
|
||||
} );
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle autoplay checkbox.
|
||||
*/
|
||||
autoCheckboxHandler = (event) => {
|
||||
if (event.target.checked) {
|
||||
this.setState({
|
||||
auto: true,
|
||||
interval: setInterval(this.nextSlideHandler, 5000)
|
||||
});
|
||||
} else {
|
||||
this.setState({ auto: false });
|
||||
clearInterval( this.state.interval );
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle hideUI checkbox.
|
||||
*/
|
||||
hideUICheckboxHandler = (event) => {
|
||||
if (event.target.checked) {
|
||||
this.setState({
|
||||
hideUI: true,
|
||||
hideUIChecked: true
|
||||
});
|
||||
} else {
|
||||
this.setState({
|
||||
hideUI: false,
|
||||
hideUIChecked: false
|
||||
});
|
||||
clearInterval( this.state.interval );
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle nsfw checkbox.
|
||||
*/
|
||||
nsfwCheckboxHandler = (event) => {
|
||||
this.setState({ showNSWF: event.target.checked });
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle mouse movement.
|
||||
* If UI is hidden, this should show the UI, then hide it again after some time
|
||||
*/
|
||||
mouseMoveHandler = () => {
|
||||
if (this.state.hideUI) {
|
||||
this.setState({ hideUI:false });
|
||||
setTimeout( this.hideUI, 3000);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hide UI, this method should be called from the timer created in
|
||||
* mouseMoveHandler only.
|
||||
* It's necessary to check if the user has unchecked the hideUI checkbox
|
||||
* after moving the mouse so as not to hide the UI in that case.
|
||||
*/
|
||||
hideUI = () => {
|
||||
if(this.state.hideUIChecked) {
|
||||
this.setState({ hideUI: true });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* On touch start, save position on state.
|
||||
* This will be later compared to touch end position for swipe gestures.
|
||||
*/
|
||||
touchStartHandler = (event) => {
|
||||
this.setState({ touchStartX: event.targetTouches[0].clientX });
|
||||
}
|
||||
|
||||
/**
|
||||
* On touch end, compare touch position to touch start (saved in state).
|
||||
* Change slides if both points are far enough.
|
||||
*/
|
||||
touchEndHandler = (event) => {
|
||||
if ( event.changedTouches[0].clientX < this.state.touchStartX - 50 ) {
|
||||
this.nextSlideHandler();
|
||||
} else if ( event.changedTouches[0].clientX > this.state.touchStartX + 50) {
|
||||
this.prevSlideHandler();
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div className="App">
|
||||
<Layout className="App"
|
||||
post={ this.state.posts.length ? (this.state.posts[ this.state.currentPost ]) : null }
|
||||
prev={ this.state.posts.length ? (this.state.posts[ this.state.prevPost ]) : null }
|
||||
prevHandler={ this.prevSlideHandler }
|
||||
nextHandler={ this.nextSlideHandler }
|
||||
showTitle={ this.state.showTitle }
|
||||
showInfo={ this.state.showInfo }
|
||||
titleClick={ this.toggleTitleHandler }
|
||||
infoClick={ this.toggleInfoHandler }
|
||||
autoPlay={ this.state.auto }
|
||||
autoCheckboxHandler={ this.autoCheckboxHandler }
|
||||
hideUI={ this.state.hideUI }
|
||||
hideUIChecked={ this.state.hideUIChecked }
|
||||
hideUICheckboxHandler={ this.hideUICheckboxHandler }
|
||||
nsfwCheckboxHandler={ this.nsfwCheckboxHandler }
|
||||
nsfwChecked={ this.state.showNSWF }
|
||||
touchStart={ this.touchStartHandler }
|
||||
touchEnd={ this.touchEndHandler }
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
export default App;
|
9
src/containers/App.test.js
Normal file
9
src/containers/App.test.js
Normal file
@ -0,0 +1,9 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import App from './App';
|
||||
|
||||
it('renders without crashing', () => {
|
||||
const div = document.createElement('div');
|
||||
ReactDOM.render(<App />, div);
|
||||
ReactDOM.unmountComponentAtNode(div);
|
||||
});
|
13
src/index.css
Normal file
13
src/index.css
Normal file
@ -0,0 +1,13 @@
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
|
||||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
|
||||
sans-serif;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
|
||||
code {
|
||||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
|
||||
monospace;
|
||||
}
|
18
src/index.js
Normal file
18
src/index.js
Normal file
@ -0,0 +1,18 @@
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import * as serviceWorker from './serviceWorker';
|
||||
import axios from 'axios';
|
||||
|
||||
import App from './containers/App';
|
||||
import './index.css';
|
||||
|
||||
axios.defaults.baseURL = "https://www.reddit.com";
|
||||
// axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
|
||||
// axios.defaults.headers.common['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept';
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById('root'));
|
||||
|
||||
// If you want your app to work offline and load faster, you can change
|
||||
// unregister() to register() below. Note this comes with some pitfalls.
|
||||
// Learn more about service workers: https://bit.ly/CRA-PWA
|
||||
serviceWorker.unregister();
|
135
src/serviceWorker.js
Normal file
135
src/serviceWorker.js
Normal file
@ -0,0 +1,135 @@
|
||||
// This optional code is used to register a service worker.
|
||||
// register() is not called by default.
|
||||
|
||||
// This lets the app load faster on subsequent visits in production, and gives
|
||||
// it offline capabilities. However, it also means that developers (and users)
|
||||
// will only see deployed updates on subsequent visits to a page, after all the
|
||||
// existing tabs open on the page have been closed, since previously cached
|
||||
// resources are updated in the background.
|
||||
|
||||
// To learn more about the benefits of this model and instructions on how to
|
||||
// opt-in, read https://bit.ly/CRA-PWA
|
||||
|
||||
const isLocalhost = Boolean(
|
||||
window.location.hostname === 'localhost' ||
|
||||
// [::1] is the IPv6 localhost address.
|
||||
window.location.hostname === '[::1]' ||
|
||||
// 127.0.0.1/8 is considered localhost for IPv4.
|
||||
window.location.hostname.match(
|
||||
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
||||
)
|
||||
);
|
||||
|
||||
export function register(config) {
|
||||
if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
||||
// The URL constructor is available in all browsers that support SW.
|
||||
const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
|
||||
if (publicUrl.origin !== window.location.origin) {
|
||||
// Our service worker won't work if PUBLIC_URL is on a different origin
|
||||
// from what our page is served on. This might happen if a CDN is used to
|
||||
// serve assets; see https://github.com/facebook/create-react-app/issues/2374
|
||||
return;
|
||||
}
|
||||
|
||||
window.addEventListener('load', () => {
|
||||
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
||||
|
||||
if (isLocalhost) {
|
||||
// This is running on localhost. Let's check if a service worker still exists or not.
|
||||
checkValidServiceWorker(swUrl, config);
|
||||
|
||||
// Add some additional logging to localhost, pointing developers to the
|
||||
// service worker/PWA documentation.
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log(
|
||||
'This web app is being served cache-first by a service ' +
|
||||
'worker. To learn more, visit https://bit.ly/CRA-PWA'
|
||||
);
|
||||
});
|
||||
} else {
|
||||
// Is not localhost. Just register service worker
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function registerValidSW(swUrl, config) {
|
||||
navigator.serviceWorker
|
||||
.register(swUrl)
|
||||
.then(registration => {
|
||||
registration.onupdatefound = () => {
|
||||
const installingWorker = registration.installing;
|
||||
if (installingWorker == null) {
|
||||
return;
|
||||
}
|
||||
installingWorker.onstatechange = () => {
|
||||
if (installingWorker.state === 'installed') {
|
||||
if (navigator.serviceWorker.controller) {
|
||||
// At this point, the updated precached content has been fetched,
|
||||
// but the previous service worker will still serve the older
|
||||
// content until all client tabs are closed.
|
||||
console.log(
|
||||
'New content is available and will be used when all ' +
|
||||
'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
|
||||
);
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onUpdate) {
|
||||
config.onUpdate(registration);
|
||||
}
|
||||
} else {
|
||||
// At this point, everything has been precached.
|
||||
// It's the perfect time to display a
|
||||
// "Content is cached for offline use." message.
|
||||
console.log('Content is cached for offline use.');
|
||||
|
||||
// Execute callback
|
||||
if (config && config.onSuccess) {
|
||||
config.onSuccess(registration);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error during service worker registration:', error);
|
||||
});
|
||||
}
|
||||
|
||||
function checkValidServiceWorker(swUrl, config) {
|
||||
// Check if the service worker can be found. If it can't reload the page.
|
||||
fetch(swUrl)
|
||||
.then(response => {
|
||||
// Ensure service worker exists, and that we really are getting a JS file.
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (
|
||||
response.status === 404 ||
|
||||
(contentType != null && contentType.indexOf('javascript') === -1)
|
||||
) {
|
||||
// No service worker found. Probably a different app. Reload the page.
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister().then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
// Service worker found. Proceed as normal.
|
||||
registerValidSW(swUrl, config);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
console.log(
|
||||
'No internet connection found. App is running in offline mode.'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export function unregister() {
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.ready.then(registration => {
|
||||
registration.unregister();
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user