Skip to content

Commit

Permalink
Merge pull request #1 from kebien6020/feature/persist-open-state
Browse files Browse the repository at this point in the history
Feature - Persist open state
  • Loading branch information
kebien6020 authored Aug 29, 2020
2 parents 2fe003b + 82be720 commit 282efd3
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 31 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ You will find a new icon near "Up next" which will toggle the translation box.
match into the translation box.
- Works for live chat and chat replay.
- Follows your configured light/dark theme.
- Will remember whether you had the translation box open or closed last time you
used it.

## License

Expand All @@ -43,6 +45,9 @@ This proyect is licensed under the [WTFPL](http://www.wtfpl.net/). See the

## Changelog

### 1.0.2
- Persist translation box open or closed state

### 1.0.1
- Follow light/dark theme of Youtube.

Expand Down
2 changes: 1 addition & 1 deletion dist/live-translate.meta.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ==UserScript==
// @name Live Translate
// @version 1.0.1
// @version 1.0.2
// @author u/BakuhatsuK
// @description Get streaming translation comments easily. Based on extension made by u/konokalahola
// @supportURL https://github.com/kebien6020/live-translate-userscript
Expand Down
4 changes: 2 additions & 2 deletions dist/live-translate.user.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "live-translate-userscript",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"main": "webpack.config.js",
"dependencies": {
Expand Down
55 changes: 30 additions & 25 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Messages from './Messages';
import { ThemeProvider, createMuiTheme } from '@material-ui/core';
import useYoutubeDarkMode from '../hooks/useYoutubeDarkMode';
import baseTheme from '../theme';
import { SettingsProvider, useSetting } from '../context/Settings';

const Providers = ({ children }) => {
const ytIsDark = useYoutubeDarkMode();
Expand All @@ -28,16 +29,18 @@ const Providers = ({ children }) => {

return (
<ThemeProvider theme={theme}>
{children}
<SettingsProvider>
{children}
</SettingsProvider>
</ThemeProvider>
);
};

const App = () => {
const Main = () => {
const [isWatchPage, setIsWatchPage] = useState(false);
const [messages, setMessages] = useState([]);
const [containers, setContainers] = useState(null);
const [showMessages, setShowMessages] = useState(false);
const [showMessages, setShowMessages] = useSetting('showMessages', false);
const toggleMessages = () => setShowMessages(prev => !prev);

// Detect when we arrive at the watch page
Expand Down Expand Up @@ -121,28 +124,30 @@ const App = () => {
}
}, [messages]);

return (
<Providers>
{containers && <>
{ReactDOM.createPortal(
<Buttons
onToggleMessages={toggleMessages}
messagesAreShown={showMessages}
/>,
containers.buttonContainer,
)}
{ReactDOM.createPortal(
<Messages
show={showMessages}
messages={messages}
forwardRef={messagesRef}
bottomOfChatRef={bottomOfChatRef}
/>,
containers.msgContainer,
)}
</>}
</Providers>
);
return (containers && <>
{ReactDOM.createPortal(
<Buttons
onToggleMessages={toggleMessages}
messagesAreShown={showMessages}
/>,
containers.buttonContainer,
)}
{ReactDOM.createPortal(
<Messages
show={showMessages}
messages={messages}
forwardRef={messagesRef}
bottomOfChatRef={bottomOfChatRef}
/>,
containers.msgContainer,
)}
</>);
};

const App = () => (
<Providers>
<Main />
</Providers>
);

export default App;
52 changes: 52 additions & 0 deletions src/context/Settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useState, useContext } from 'react';

const SettingsContext = React.createContext({});

const LOCAL_STORAGE_KEY = 'live_translate_settings';

export const SettingsProvider = ({ children }) => {
const [settings, setSettings] = useState(() => {
// Restore previous settings
const value = window.localStorage.getItem(LOCAL_STORAGE_KEY);

try {
return JSON.parse(value);
} catch (err) {
return {};
}
});

const setAndSaveSetting = (name, value) => {
const newSettings = { ...settings, [name]: value };
setSettings(newSettings);

window.localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify(newSettings),
);
};

const context = {
settings,
setAndSaveSetting,
};

return (
<SettingsContext.Provider value={context}>
{children}
</SettingsContext.Provider>
);
};

export const useSetting = (name, def) => {
const { settings, setAndSaveSetting } = useContext(SettingsContext);
const value = (settings && name in settings) ? settings[name] : def;
const setSetting = (newVal) => {
// Handle function expecting old value
if (typeof newVal === 'function')
newVal = newVal(value);

setAndSaveSetting(name, newVal);
};
return [value, setSetting];
};
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ module.exports = {
include: 'https://*.youtube.com/*',
'run-at': 'document-start',
supportURL: 'https://github.com/kebien6020/live-translate-userscript',
downloadURL: 'https://github.com/kebien6020/live-translate-userscript/raw/master/dist/live-translate.user.js',
updateURL: 'https://github.com/kebien6020/live-translate-userscript/raw/master/dist/live-translate.user.js',
downloadURL: dev ? undefined : 'https://github.com/kebien6020/live-translate-userscript/raw/master/dist/live-translate.user.js',
updateURL: dev ? undefined : 'https://github.com/kebien6020/live-translate-userscript/raw/master/dist/live-translate.user.js',
},
proxyScript: {
enable: () => dev,
Expand Down

0 comments on commit 282efd3

Please sign in to comment.