-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from cophilot/13-extension-support
added extension
- Loading branch information
Showing
12 changed files
with
219 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { useEffect, useState } from 'react'; | ||
import './ExtensionButtons.scss'; | ||
import GameStorage from '../utils/GameStorage'; | ||
|
||
interface ExtensionButtonsProps { | ||
definition: any; | ||
onExtensionOn?: (extensionName: string, extensionDefinition: any) => void; | ||
onExtensionOff?: (extensionName: string) => void; | ||
} | ||
|
||
/** | ||
* This is a ExtensionButtons component | ||
* @author cophilot | ||
* @version 1.0.0 | ||
* @created 2024-7-21 | ||
*/ | ||
function ExtensionButtons({ | ||
definition, | ||
onExtensionOn, | ||
onExtensionOff, | ||
}: ExtensionButtonsProps) { | ||
const extensionsDefinition = definition.extensions || {}; | ||
const extensionsNames = Object.keys(extensionsDefinition); | ||
|
||
const [selectedExtensions, setSelectedExtensions] = useState<string[]>( | ||
GameStorage.getSelectedExtension(definition.title, []) | ||
); | ||
|
||
const handleExtensionClick = (extensionName: string) => { | ||
let newSelectedExtensions = [...selectedExtensions]; | ||
if (selectedExtensions.includes(extensionName)) { | ||
newSelectedExtensions = selectedExtensions.filter( | ||
(name) => name !== extensionName | ||
); | ||
onExtensionOff?.(extensionName); | ||
} else { | ||
newSelectedExtensions.push(extensionName); | ||
onExtensionOn?.(extensionName, extensionsDefinition[extensionName]); | ||
} | ||
|
||
GameStorage.setSelectedExtension( | ||
definition.title, | ||
newSelectedExtensions | ||
); | ||
setSelectedExtensions(newSelectedExtensions); | ||
}; | ||
|
||
useEffect(() => { | ||
GameStorage.getSelectedExtension(definition.title, []).forEach( | ||
(extensionName: string) => { | ||
onExtensionOn?.( | ||
extensionName, | ||
extensionsDefinition[extensionName] | ||
); | ||
} | ||
); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return ( | ||
<div className="print-hide"> | ||
{extensionsNames.length > 0 && <h2>Extensions</h2>} | ||
{extensionsNames.map((name) => ( | ||
<button | ||
key={name} | ||
className={ | ||
'btn nav-btn ' + | ||
(selectedExtensions.includes(name) ? 'selected' : '') | ||
} | ||
onClick={() => handleExtensionClick(name)}> | ||
{name} | ||
</button> | ||
))} | ||
</div> | ||
); | ||
} | ||
export default ExtensionButtons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters