Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Somewhat better version
Browse files Browse the repository at this point in the history
  • Loading branch information
x-t committed Dec 10, 2020
1 parent 242bf91 commit 8ec8877
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 208 deletions.
1 change: 0 additions & 1 deletion .eslintcache

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.env.development.local
.env.test.local
.env.production.local
.eslintcache

npm-debug.log*
yarn-debug.log*
Expand Down
98 changes: 0 additions & 98 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"browser-image-compression": "^1.0.13",
"react": "^17.0.1",
"react-colorful": "^4.4.3",
"react-cookie": "^4.0.3",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"typescript": "^4.1.2",
Expand Down
29 changes: 13 additions & 16 deletions src/css/App.css
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
.App {
text-align: center;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

.App-header {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding-top: 10px;
}

.SettingsBtn {
position: absolute;
right: 0;
bottom: 0;
margin-bottom: 20px;
}

.SettingsBtn button {
width: 25px;
padding: 3px;
border-radius: 5px;
outline: none;
border: none;
background-color: transparent;
color: var(--background-color);
fill: currentColor;
filter: invert(1) grayscale(1);
}
File renamed without changes.
12 changes: 12 additions & 0 deletions src/hooks/useAmountRendered.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useRef } from "react";

/**
* Outputs the amount of times rendered inside console
* @param scope Define component scope
*/
const useAmountRendered = (scope: string) => {
const renders = useRef(0);
console.log(`${scope} renders: ${renders.current++}`)
}

export default useAmountRendered;
36 changes: 36 additions & 0 deletions src/hooks/useBackground.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import useStickyState from "./useStickyState";

export interface Background {
values: {
opacity: number;
color: string;
data: string;
};

set: {
opacity: React.Dispatch<any>;
color: React.Dispatch<any>;
data: React.Dispatch<any>;
}
}

const useBackground = (): Background => {
const [bgOpacity, setBgOpacity] = useStickyState(40, 'bgOpacity');
const [bgColor, setBgColor] = useStickyState("#ffffff", 'bgColor');
const [bgData, setBgData] = useStickyState("img/default.jpg", 'bgData');

return {
values: {
opacity: bgOpacity,
color: bgColor,
data: bgData
},
set: {
opacity: setBgOpacity,
color: setBgColor,
data: setBgData
}
};
}

export default useBackground;
26 changes: 26 additions & 0 deletions src/hooks/useStickyState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { SetStateAction, useEffect, useState } from "react";

/* Original JS code:
https://www.joshwcomeau.com/react/persisting-react-state-in-localstorage/ */

/**
* Returns a stateful value, based on localStorage, and a function to update it.
* @param defaultValue Default value for the value
* @param key Key used to get/set inside localStorage
*/
const useStickyState = <T extends unknown>(defaultValue: T, key: string): [T, React.Dispatch<SetStateAction<T>>] => {
const [value, setValue] = useState(() => {
const stickyValue = localStorage.getItem(key);
return stickyValue !== null
? JSON.parse(stickyValue)
: defaultValue;
});

useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);

return [value, setValue];
}

export default useStickyState;
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import './css/index.css';
import App from './ui/App';

ReactDOM.render(
Expand Down
9 changes: 9 additions & 0 deletions src/lib/blobToBase64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const blobToBase64 = async (compressedFile: Blob): Promise<string> => {
return new Promise((resolve) => {
const reader = new FileReader();
reader.readAsDataURL(compressedFile);
reader.onloadend = () => resolve(reader.result as string)
});
}

export default blobToBase64;
22 changes: 22 additions & 0 deletions src/lib/compressImage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import imageCompression from "browser-image-compression";
import blobToBase64 from "./blobToBase64";

const compressImage = async (event: React.ChangeEvent<HTMLInputElement>) => {
const imageFile = event.target.files![0];

const options = {
maxSizeMB: 1,
maxWidthOrHeight: 1920,
useWebWorker: true
}

try {
const compressedFile = await imageCompression(imageFile, options);
const compressedFileB64 = await blobToBase64(compressedFile);
return compressedFileB64;
} catch (error) {
console.error(error);
}
}

export default compressImage;
1 change: 1 addition & 0 deletions src/svg/wrench.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8ec8877

Please sign in to comment.