-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useLibraries } from "@/lib/api" | ||
import { Library, TerseScratch } from "@/lib/api/types" | ||
|
||
type LibrariesT = { | ||
libraries: Library[] | ||
} | ||
|
||
type Props = { | ||
scratch: TerseScratch | ||
onChange: (value: LibrariesT) => void | ||
} | ||
|
||
export default function LibraryPanel({ scratch, onChange }: Props) { | ||
const libraries = useLibraries() | ||
|
||
const hasLibrary = libName => scratch.libraries.some(lib => lib.name == libName) | ||
const libraryVersion = lib => { | ||
const scratchlib = scratch.libraries.find(scratchlib => scratchlib.name == lib.name) | ||
if (scratchlib != null) { | ||
return scratchlib.version | ||
} else { | ||
return lib.supported_versions[0] | ||
} | ||
} | ||
|
||
const setLibraryVersion = (libName, ver) => { | ||
// clone the libraries | ||
const libs = JSON.parse(JSON.stringify(scratch.libraries)) | ||
// Check if the library is already enabled, if so return it | ||
const scratchlib = scratch.libraries.find(scratchlib => scratchlib.name == libName) | ||
if (scratchlib != null) { | ||
// If it is, set the version | ||
scratchlib.version = ver | ||
} else { | ||
// If it isn't, add the library to the list | ||
libs.push({ name: libName, version: ver }) | ||
} | ||
onChange({ | ||
libraries: libs, | ||
}) | ||
} | ||
const unsetLibrary = libName => { | ||
// clone the libraries | ||
let libs = JSON.parse(JSON.stringify(scratch.libraries)) | ||
// Only keep the libs whose name are not libName | ||
libs = libs.filter(lib => lib.name != libName) | ||
onChange({ | ||
libraries: libs, | ||
}) | ||
} | ||
const toggleLibrary = lib => { | ||
if (hasLibrary(lib.name)) { | ||
unsetLibrary(lib.name) | ||
} else { | ||
setLibraryVersion(lib.name, lib.supported_versions[0]) | ||
} | ||
} | ||
|
||
const librariesElements = libraries.map(lib => <div key={lib.name}> | ||
<input type="checkbox" checked={hasLibrary(lib.name)} onChange={() => toggleLibrary(lib)} /> | ||
<label>{lib.name}</label> | ||
<select disabled={!hasLibrary(lib.name)} value={libraryVersion(lib)} onChange={e => setLibraryVersion(lib.name, e.target.value)}> | ||
{lib.supported_versions.map(ver => <option key={ver} value={ver}>{ver}</option>)} | ||
</select> | ||
</div>) | ||
|
||
return <div> | ||
<section> | ||
<h3>Libraries</h3> | ||
{librariesElements} | ||
</section> | ||
</div> | ||
} |
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
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
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