More features
This commit is contained in:
@@ -4,15 +4,20 @@ import {Group} from "./Group";
|
||||
import {FactorySelect} from "./FactorySelect";
|
||||
import styles from "./Home.module.css"
|
||||
import {sortByProperty} from "../src/utils";
|
||||
import {useDetails} from "../src/hooks/useDetails";
|
||||
import {Entity} from "../src/types";
|
||||
import pako from 'pako';
|
||||
|
||||
interface Group {
|
||||
name: string
|
||||
isExported?: boolean
|
||||
factories: string[]
|
||||
intermediates: string[]
|
||||
inputs: string[]
|
||||
}
|
||||
|
||||
export const HomeComponent: FC = () => {
|
||||
const details = useDetails()
|
||||
const [newGroupValue, setNewGroupValue] = useState("New group")
|
||||
const [excludedSuggestions, setExcludedSuggestions] = useLocalStorage<string[]>('excludedSuggestions', [])
|
||||
const [basicValues, setBasicValues] = useLocalStorage<string[]>('basicValues', [])
|
||||
@@ -20,9 +25,24 @@ export const HomeComponent: FC = () => {
|
||||
const [groups, setGroups] = useLocalStorage<Group[]>('serviceGroups', [])
|
||||
const [clientGroups, setClientGroups] = useState<typeof groups>([])
|
||||
|
||||
const doNotSuggest = useMemo<string[]>(() => {
|
||||
return groups.flatMap(group => [...group.intermediates, ...group.factories, ...excludedSuggestions])
|
||||
}, [groups, excludedSuggestions])
|
||||
const doNotSuggest = useMemo<Set<string>>(() => {
|
||||
return new Set([...groups.flatMap(group => [...(group.name.includes('Mall') ? group.intermediates : []), ...group.factories]), ...excludedSuggestions, ...basicValues])
|
||||
}, [basicValues, groups, excludedSuggestions])
|
||||
|
||||
const basicSet = useMemo<Set<string>>(() => {
|
||||
return new Set(basicValues)
|
||||
}, [basicValues])
|
||||
|
||||
const exportedSet = useMemo<Set<string>>(() => {
|
||||
return new Set(groups
|
||||
.filter(group => group.isExported)
|
||||
.flatMap(group => group.factories)
|
||||
)
|
||||
}, [groups])
|
||||
|
||||
const missingFactories = useMemo<Entity[]>(() => {
|
||||
return details.filter(detail => !doNotSuggest.has(detail.href) && detail.recipe)
|
||||
}, [details, doNotSuggest])
|
||||
|
||||
useEffect(() => setClientGroups(groups), [groups])
|
||||
const removeGroup = (idx: number) => setGroups(groups => {
|
||||
@@ -43,10 +63,25 @@ export const HomeComponent: FC = () => {
|
||||
groups.sort(sortByProperty(group => group.name))
|
||||
return groups
|
||||
}), [setGroups])
|
||||
const toggleExported = useCallback((idx: number) => setGroups(groups => {
|
||||
groups[idx].isExported = !groups[idx].isExported
|
||||
groups.sort(sortByProperty(group => group.name))
|
||||
return groups
|
||||
}), [setGroups])
|
||||
|
||||
const store = () => {
|
||||
const string = JSON.stringify(groups)
|
||||
const btoa = (bin: Uint8Array) => Buffer.from(bin).toString('base64')
|
||||
const atob = (str: string) => Buffer.from(str, 'base64')
|
||||
const compressed = btoa(pako.deflate(string))
|
||||
console.log(string.length, compressed.length)
|
||||
const uncompressed = pako.inflate(atob(compressed), {to: "string"})
|
||||
}
|
||||
|
||||
return (
|
||||
<main>
|
||||
<h1>Factorio Microservices</h1>
|
||||
<button onClick={store}>Store</button>
|
||||
<fieldset>
|
||||
<legend>Basic Values</legend>
|
||||
<FactorySelect
|
||||
@@ -71,6 +106,18 @@ export const HomeComponent: FC = () => {
|
||||
Add group "{newGroupValue}"
|
||||
</button>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<legend>Missing factories</legend>
|
||||
<ul>
|
||||
{
|
||||
missingFactories.map(missing => <li key={missing.href}
|
||||
onContextMenu={event => {
|
||||
event.preventDefault()
|
||||
setExcludedSuggestions([...excludedSuggestions, missing.href])
|
||||
}}>{missing.name}</li>)
|
||||
}
|
||||
</ul>
|
||||
</fieldset>
|
||||
<div className={styles.grid}>
|
||||
{
|
||||
clientGroups.map((group, idx) => <Group
|
||||
@@ -79,12 +126,14 @@ export const HomeComponent: FC = () => {
|
||||
intermediateFactories={group.intermediates}
|
||||
inputFactories={group.inputs}
|
||||
name={group.name}
|
||||
isExported={group.isExported ?? false}
|
||||
onRemove={() => removeGroup(idx)}
|
||||
onRename={(name: string) => renameGroup(idx, name)}
|
||||
onSetOutputFactories={uids => setFactories(idx, uids, 'factories')}
|
||||
onSetIntermediateFactories={uids => setFactories(idx, uids, 'intermediates')}
|
||||
onSetInputFactories={uids => setFactories(idx, uids, 'inputs')}
|
||||
doNotSuggest={doNotSuggest}
|
||||
onToggleExported={() => toggleExported(idx)}
|
||||
sets={{doNotSuggest, basic: basicSet, exported: exportedSet}}
|
||||
onDoIgnore={uid => setExcludedSuggestions([...excludedSuggestions, uid])}
|
||||
/>)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user