Skip to content

Commit

Permalink
stop trying to use TW favorites & fix libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyGamer13 committed Sep 24, 2023
1 parent 6c7f4bc commit 503a01e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 97 deletions.
73 changes: 19 additions & 54 deletions src/components/library/library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class LibraryComponent extends React.Component {
'handleMouseLeave',
'handlePlayingEnd',
'handleSelect',
'handleFavorite',
'handleTagClick',
'setFilteredDataRef',
'loadLibraryData',
Expand All @@ -69,7 +68,6 @@ class LibraryComponent extends React.Component {
'createFilteredData',
'getFilteredData'
]);
const favorites = this.readFavoritesFromStorage();
this.state = {
playingItem: null,
filterQuery: '',
Expand Down Expand Up @@ -172,44 +170,17 @@ class LibraryComponent extends React.Component {
this.scrollToTop();
}

if (this.state.favorites !== prevState.favorites) {
try {
localStorage.setItem(this.getFavoriteStorageKey(), JSON.stringify(this.state.favorites));
} catch (error) {
// ignore
}
if (prevProps.data !== this.props.data) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({
data: this.props.data
});
}
}
handleSelect (id) {
this.handleClose();
this.props.onItemSelected(this.getFilteredData()[id]);
}
readFavoritesFromStorage () {
let data;
try {
data = JSON.parse(localStorage.getItem(this.getFavoriteStorageKey()));
} catch (error) {
// ignore
}
if (!Array.isArray(data)) {
data = [];
}
return data;
}
getFavoriteStorageKey () {
return `tw:library-favorites:${this.props.id}`;
}
handleFavorite (id) {
const data = this.getFilteredData()[id];
const key = data[this.props.persistableKey];
this.setState(oldState => ({
favorites: oldState.favorites.includes(key) ? (
oldState.favorites.filter(i => i !== key)
) : (
[...oldState.favorites, key]
)
}));
}
handleClose () {
this.props.onRequestClose();
}
Expand Down Expand Up @@ -515,27 +486,22 @@ class LibraryComponent extends React.Component {
}

LibraryComponent.propTypes = {
data: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([
/* eslint-disable react/no-unused-prop-types, lines-around-comment */
// An item in the library
PropTypes.shape({
// @todo remove md5/rawURL prop from library, refactor to use storage
md5: PropTypes.string,
name: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node
]),
rawURL: PropTypes.string
}),
PropTypes.string
/* eslint-enable react/no-unused-prop-types, lines-around-comment */
])),
PropTypes.instanceOf(Promise)
]),
data: PropTypes.oneOfType([PropTypes.arrayOf(
/* eslint-disable react/no-unused-prop-types, lines-around-comment */
// An item in the library
PropTypes.shape({
// @todo remove md5/rawURL prop from library, refactor to use storage
md5: PropTypes.string,
name: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node
]),
rawURL: PropTypes.string
})
/* eslint-enable react/no-unused-prop-types, lines-around-comment */
), PropTypes.instanceOf(Promise)]),
filterable: PropTypes.bool,
id: PropTypes.string.isRequired,
persistableKey: PropTypes.string,
intl: intlShape.isRequired,
onItemMouseEnter: PropTypes.func,
onItemMouseLeave: PropTypes.func,
Expand All @@ -549,7 +515,6 @@ LibraryComponent.propTypes = {

LibraryComponent.defaultProps = {
filterable: true,
persistableKey: 'name',
showPlayButton: false
};

Expand Down
12 changes: 1 addition & 11 deletions src/containers/backdrop-library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ class BackdropLibrary extends React.Component {
bindAll(this, [
'handleItemSelect'
]);
this.state = {
data: getBackdropLibrary()
};
}
componentDidMount () {
if (this.state.data.then) {
this.state.data.then(data => this.setState({
data
}));
}
}
handleItemSelect (item) {
const vmBackdrop = {
Expand All @@ -48,7 +38,7 @@ class BackdropLibrary extends React.Component {
render () {
return (
<LibraryComponent
data={this.state.data.then ? null : this.state.data}
data={getBackdropLibrary()}
id="backdropLibrary"
header={"Backdrops"}
tags={backdropTags}
Expand Down
12 changes: 1 addition & 11 deletions src/containers/costume-library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ class CostumeLibrary extends React.PureComponent {
bindAll(this, [
'handleItemSelected'
]);
this.state = {
data: getCostumeLibrary()
};
}
componentDidMount () {
if (this.state.data.then) {
this.state.data.then(data => this.setState({
data
}));
}
}
handleItemSelected (item) {
const vmCostume = {
Expand All @@ -47,7 +37,7 @@ class CostumeLibrary extends React.PureComponent {
render () {
return (
<LibraryComponent
data={this.state.data.then ? null : this.state.data}
data={getCostumeLibrary()}
id="costumeLibrary"
tags={spriteTags}
header={"Costumes"}
Expand Down
20 changes: 10 additions & 10 deletions src/containers/sound-library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ class SoundLibrary extends React.PureComponent {
*/
this.handleStop = null;

const soundLibrary = getSoundLibrary();
this.state = {
data: null
data: Array.isArray(soundLibrary) ?
getSoundLibraryThumbnailData(soundLibrary, this.props.isRtl) :
soundLibrary
};
}
componentDidMount () {
const soundLibrary = getSoundLibrary();
if (soundLibrary.then) {
soundLibrary.then(data => this.setState({
data: getSoundLibraryThumbnailData(data, this.props.isRtl)
}));
} else {
this.setState({
data: getSoundLibraryThumbnailData(soundLibrary, this.props.isRtl)
})
if (this.state.data.then) {
this.state.data.then(data => {
this.setState({
data: getSoundLibraryThumbnailData(data, this.props.isRtl)
});
});
}

this.audioEngine = new AudioEngine();
Expand Down
12 changes: 1 addition & 11 deletions src/containers/sprite-library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ class SpriteLibrary extends React.PureComponent {
bindAll(this, [
'handleItemSelect'
]);
this.state = {
data: getSpriteLibrary()
};
}
componentDidMount () {
if (this.state.data.then) {
this.state.data.then(data => this.setState({
data
}));
}
}
handleItemSelect (item) {
// Randomize position of library sprite
Expand All @@ -45,7 +35,7 @@ class SpriteLibrary extends React.PureComponent {
render () {
return (
<LibraryComponent
data={this.state.data.then ? null : this.state.data}
data={getSpriteLibrary()}
id="spriteLibrary"
header={"Sprites"}
tags={spriteTags}
Expand Down

0 comments on commit 503a01e

Please sign in to comment.