stand2026_10_06
This commit is contained in:
1 parent
41f80ce947
commit
266f8c3b4f
199 files changed
+17061
-87
No files matched your search
@@ -0,0 +1,195 @@
|
||||
import { api_component_create, api_component_remove, api_component_update } from "@/actions/rk2/api-component";
|
||||
import { api_signals_create } from "@/actions/rk2/api-signals";
|
||||
import { ComponentCreate, ComponentDefaults, ComponentResponse, ComponentType, ComponentTypeDefinition, ComponentUpdate, PropertyDefinition } from "@/types/anlagen/components";
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
interface Props {
|
||||
component?: ComponentResponse,
|
||||
componentTypes: ComponentTypeDefinition[]
|
||||
componentDefaults: ComponentDefaults[]
|
||||
}
|
||||
interface ComponentNames {
|
||||
id: string,
|
||||
name: string
|
||||
}
|
||||
|
||||
const emptyComponent: ComponentResponse = {
|
||||
id: "",
|
||||
name: "",
|
||||
type: "",
|
||||
index: 0,
|
||||
postfix: "",
|
||||
prefix: "",
|
||||
properties: {}
|
||||
|
||||
}
|
||||
|
||||
export function useComponent({ component, componentTypes, componentDefaults }: Props) {
|
||||
|
||||
const [data, setData] = useState<ComponentResponse>(emptyComponent);
|
||||
const [componentNames, setComponentNames] = useState<ComponentNames[]>([])
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- init component Data
|
||||
//-------------------------------------------------------------------------
|
||||
useEffect(() => {
|
||||
console.log('Render',component?.properties)
|
||||
if (!component)
|
||||
setData(() => ({ ...emptyComponent,properties:{} }))
|
||||
else
|
||||
setData(() => ({ ...emptyComponent,properties:{}, ...component }))
|
||||
|
||||
}, [component])
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- init Components Names
|
||||
//-------------------------------------------------------------------------
|
||||
useEffect(() => {
|
||||
let compNames: ComponentNames[] = [];
|
||||
|
||||
componentTypes.forEach((c) => compNames.push({ id: c.type, name: c.label }))
|
||||
setComponentNames(() => compNames);
|
||||
}, [componentTypes])
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- set Values of Compnent
|
||||
//-------------------------------------------------------------------------
|
||||
const setValue = <K extends keyof ComponentResponse>(fieldName: K, value: ComponentResponse[K]): void => {
|
||||
setData((cur) => ({ ...cur, [fieldName]: value }))
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Suche Type von Componente
|
||||
//-------------------------------------------------------------------------
|
||||
const findCompType = (type: string): ComponentTypeDefinition | undefined => {
|
||||
return componentTypes.find((item) => item.type === type)
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Propertie Typ Suchen
|
||||
//-------------------------------------------------------------------------
|
||||
const findCompPropertyType = (propName: string): PropertyDefinition | undefined => {
|
||||
let comType = findCompType(data.type);
|
||||
let propType = comType?.properties.find((p) => p.key === propName);
|
||||
|
||||
return propType
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Propertie Änderung
|
||||
//-------------------------------------------------------------------------
|
||||
const changePropertie = (name: string, value: unknown) => {
|
||||
|
||||
let newData = { ...data };
|
||||
if (!newData.properties) return;
|
||||
newData.properties[name] = value;
|
||||
|
||||
setData((cur) => ({ ...cur, properties: newData.properties }));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Default Properties hinzufügen
|
||||
//-------------------------------------------------------------------------
|
||||
const setDefaultPropertie = () => {
|
||||
|
||||
let newData = { ...data }; // Compenenten daten kopieren
|
||||
let comType = findCompType(data.type);
|
||||
if(!comType) return;
|
||||
let currentProperties = newData.properties;
|
||||
if(!currentProperties) currentProperties = {};
|
||||
|
||||
comType.properties.forEach((dp)=>{
|
||||
if(!currentProperties[dp.key]) currentProperties[dp.key] = dp.defaultValue;
|
||||
})
|
||||
|
||||
|
||||
|
||||
setData((cur) => ({ ...cur, properties: currentProperties }));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Defaults
|
||||
//-------------------------------------------------------------------------
|
||||
const getCompDefaults = (compType: ComponentType) => {
|
||||
let defaults = componentDefaults.find((c) => c.type === compType);
|
||||
if (!defaults) defaults = { name: "unbekannt", signals: [], type: "unbekannt" }
|
||||
|
||||
return defaults;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Api Create
|
||||
//-------------------------------------------------------------------------
|
||||
const createComponent = async (unitId: string, data: ComponentCreate) => {
|
||||
let defaults = getCompDefaults(data.type);
|
||||
let comTyp = findCompType(data.type);
|
||||
if (!data.name || data.name == "") data.name = defaults.name;
|
||||
|
||||
if (!comTyp) return;
|
||||
|
||||
const record = Object.fromEntries(
|
||||
comTyp.properties.map(({ key, type, defaultValue }) => [key, defaultValue])
|
||||
);
|
||||
|
||||
if (comTyp && comTyp.properties) data.properties = record;
|
||||
console.log(data)
|
||||
setLoading(true);
|
||||
let resultCreatComp = await api_component_create(unitId, data);
|
||||
if (resultCreatComp.success) {
|
||||
for (let defSig of defaults.signals) {
|
||||
await api_signals_create({ name: defSig.name, type: defSig.type, componentId: resultCreatComp.data.id })
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
setLoading(false);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Api Update
|
||||
//-------------------------------------------------------------------------
|
||||
const updateComponent = async (compId: string, data: ComponentResponse) => {
|
||||
|
||||
let { id, ...update } = data;
|
||||
setLoading(true);
|
||||
await api_component_update(compId, update);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
//--- Api Remove
|
||||
//-------------------------------------------------------------------------
|
||||
const removeComponent = async (compId: string) => {
|
||||
setLoading(true);
|
||||
await api_component_remove(compId);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return {
|
||||
loading,
|
||||
compnentData: data,
|
||||
setValue,
|
||||
componentNames,
|
||||
findCompType,
|
||||
findCompPropertyType,
|
||||
createComponent,
|
||||
updateComponent,
|
||||
removeComponent,
|
||||
changePropertie,
|
||||
setDefaultPropertie
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
import { GroupCreate, GroupResponse } from "@/types/anlagen/groups";
|
||||
|
||||
|
||||
import { useCallback } from "react";
|
||||
|
||||
interface Props {
|
||||
group: GroupResponse | undefined;
|
||||
|
||||
}
|
||||
|
||||
export function useGroup({ group }: Props) {
|
||||
|
||||
const emptyGroup = {
|
||||
|
||||
name: "",
|
||||
container: true,
|
||||
nr: "",
|
||||
parentId: ""
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
emptyGroup
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
import { GroupResponse } from "@/types/anlagen/groups"
|
||||
import { useCallback, useMemo, useState } from "react"
|
||||
|
||||
|
||||
|
||||
export interface GroupTree {
|
||||
group: GroupResponse
|
||||
childs: GroupTree[]
|
||||
}
|
||||
|
||||
export function useGroupTree(groups: GroupResponse[]) {
|
||||
|
||||
const [openGroups, setOpenGroups] = useState<Set<string>>(new Set())
|
||||
const [selectedGroupId, setSelectedGroupId] = useState<string | null>(null)
|
||||
|
||||
/**
|
||||
* Erstellt aus dem flachen Array den Tree.
|
||||
*/
|
||||
const tree = useMemo(() => {
|
||||
const tree: GroupTree[] = []
|
||||
|
||||
const map = new Map<string, GroupTree>()
|
||||
|
||||
// Alle Nodes erstellen
|
||||
for (const group of groups) {
|
||||
map.set(group.id, {
|
||||
group,
|
||||
childs: [],
|
||||
})
|
||||
}
|
||||
|
||||
// Parent-Child Beziehungen herstellen
|
||||
for (const group of groups) {
|
||||
const node = map.get(group.id)
|
||||
|
||||
if (!node) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Kein Parent => Root Node
|
||||
if (!group.parentId) {
|
||||
tree.push(node)
|
||||
continue
|
||||
}
|
||||
|
||||
const parent = map.get(group.parentId)
|
||||
|
||||
if (parent) {
|
||||
parent.childs.push(node)
|
||||
} else {
|
||||
// Optional:
|
||||
// Parent existiert nicht -> ebenfalls Root
|
||||
tree.push(node)
|
||||
}
|
||||
}
|
||||
|
||||
return tree
|
||||
}, [groups])
|
||||
|
||||
/**
|
||||
* Prüft, ob eine Gruppe geöffnet ist.
|
||||
*/
|
||||
const isOpen = useCallback(
|
||||
(id: string) => {
|
||||
return openGroups.has(id)
|
||||
},
|
||||
[openGroups]
|
||||
)
|
||||
|
||||
/**
|
||||
* Öffnet oder schließt eine Gruppe.
|
||||
*/
|
||||
const toggle = useCallback((id: string) => {
|
||||
setOpenGroups((current) => {
|
||||
const next = new Set(current)
|
||||
|
||||
if (next.has(id)) {
|
||||
next.delete(id)
|
||||
} else {
|
||||
next.add(id)
|
||||
}
|
||||
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Öffnet eine Gruppe.
|
||||
*/
|
||||
const open = useCallback((id: string) => {
|
||||
setOpenGroups((current) => {
|
||||
const next = new Set(current)
|
||||
|
||||
next.add(id)
|
||||
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Schließt eine Gruppe.
|
||||
*/
|
||||
const close = useCallback((id: string) => {
|
||||
setOpenGroups((current) => {
|
||||
const next = new Set(current)
|
||||
|
||||
next.delete(id)
|
||||
|
||||
return next
|
||||
})
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Schließt alle Gruppen.
|
||||
*/
|
||||
const closeAll = useCallback(() => {
|
||||
setOpenGroups(new Set())
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Öffnet alle Container.
|
||||
*/
|
||||
const openAll = useCallback(() => {
|
||||
const ids = new Set(
|
||||
groups
|
||||
.filter((group) => group.container)
|
||||
.map((group) => group.id)
|
||||
)
|
||||
|
||||
setOpenGroups(ids)
|
||||
}, [groups])
|
||||
|
||||
// ----------------------------------------
|
||||
// Selection
|
||||
// ----------------------------------------
|
||||
|
||||
const select = useCallback((id: string|undefined) => {
|
||||
id ? setSelectedGroupId(id) : setSelectedGroupId(null)
|
||||
}, [])
|
||||
|
||||
const deselect = useCallback(() => {
|
||||
setSelectedGroupId(null)
|
||||
}, [])
|
||||
|
||||
const isSelected = useCallback(
|
||||
(id: string) => {
|
||||
return selectedGroupId === id
|
||||
},
|
||||
[selectedGroupId]
|
||||
)
|
||||
|
||||
// ----------------------------------------
|
||||
// Ausgewählten GroupResponse zurückgeben
|
||||
// ----------------------------------------
|
||||
|
||||
const selectedGroup = useMemo(() => {
|
||||
if (!selectedGroupId) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
groups.find((group) => group.id === selectedGroupId) ??
|
||||
null
|
||||
)
|
||||
}, [groups, selectedGroupId])
|
||||
|
||||
return {
|
||||
tree,
|
||||
openGroups,
|
||||
isOpen,
|
||||
toggle,
|
||||
open,
|
||||
close,
|
||||
openAll,
|
||||
closeAll,
|
||||
// Selection
|
||||
selectedGroupId,
|
||||
selectedGroup,
|
||||
isSelected,
|
||||
select,
|
||||
deselect,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
|
||||
|
||||
import { GroupCreate, GroupDetail, GroupResponse, GroupTree } from "@/types/anlagen/groups";
|
||||
|
||||
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
|
||||
|
||||
interface Props {
|
||||
groups: GroupResponse[];
|
||||
}
|
||||
|
||||
|
||||
export function useGroups({ groups: initGroups }: Props) {
|
||||
|
||||
|
||||
const [groupsExtended, setGroupsExteded] = useState<GroupDetail[]>([]);
|
||||
|
||||
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
const gDeatial = initGroups.map((g) => ({ group: g, parent: g.parentId ? getGroupById(g.parentId) : undefined }))
|
||||
setGroupsExteded(() => gDeatial);
|
||||
}, [initGroups])
|
||||
|
||||
|
||||
|
||||
const getGroupById = useCallback((id: string) => {
|
||||
return initGroups.find((g) => g.id === id);
|
||||
}, [])
|
||||
|
||||
const getGroupPath = (group: GroupResponse) => {
|
||||
const g: GroupResponse[] = []
|
||||
let currentGroup = group;
|
||||
g.push(group);
|
||||
while (currentGroup) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const getPathToRoot = (startGroupId?: string )=> {
|
||||
if(!startGroupId) return [];
|
||||
const groupMap = new Map(initGroups.map((group) => [group.id, group]));
|
||||
|
||||
const path: GroupResponse[] = []
|
||||
|
||||
let current = groupMap.get(startGroupId)
|
||||
|
||||
while (current) {
|
||||
path.push(current)
|
||||
|
||||
if (!current.parentId) {
|
||||
break
|
||||
}
|
||||
|
||||
current = groupMap.get(current.parentId)
|
||||
}
|
||||
|
||||
return path.reverse()
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
getGroupById,
|
||||
groupsExtended,
|
||||
getPathToRoot
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { api_signalsByComp } from "@/actions/rk2/api-signals";
|
||||
import { ComponentResponse } from "@/types/anlagen/components";
|
||||
import { SignalResponse, SignalType } from "@/types/anlagen/signals";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
interface Props {
|
||||
signal?: SignalResponse | undefined;
|
||||
componenteId?: string
|
||||
|
||||
}
|
||||
|
||||
const emptySignal: Partial<SignalResponse> = {
|
||||
anlageId: "",
|
||||
id: "",
|
||||
name: "",
|
||||
type: SignalType.DIGITAL_INPUT,
|
||||
componentId: "",
|
||||
description: "",
|
||||
ioId: ""
|
||||
}
|
||||
export function useSignal({ signal, componenteId }: Props) {
|
||||
const [signalData, setSignalData] = useState<Partial<SignalResponse>>(emptySignal);
|
||||
|
||||
const [signalByComp, setSignalByComp] = useState<SignalResponse[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const loadCompSignal = async () => {
|
||||
if (componenteId) {
|
||||
let sigOfComp = await api_signalsByComp(componenteId)
|
||||
setSignalByComp(()=>sigOfComp);
|
||||
}
|
||||
}
|
||||
loadCompSignal();
|
||||
}, [componenteId])
|
||||
|
||||
function setValue<K extends keyof SignalResponse>(fieldName: K, value: SignalResponse[K]): void {
|
||||
|
||||
setSignalData((cur) => ({ ...cur, [fieldName]: value }))
|
||||
}
|
||||
|
||||
const string_to_signalType = (strType: string): SignalType => {
|
||||
|
||||
let signalT = Object.values(SignalType).find((s) => s === strType)
|
||||
if (!signalT) signalT = SignalType.ANALOG_INPUT;
|
||||
return signalT;
|
||||
}
|
||||
|
||||
return {
|
||||
setSignal: (signal: SignalResponse) => setSignalData(() => ({ ...signal })),
|
||||
signalData,
|
||||
setValue,
|
||||
string_to_signalType,
|
||||
signalByComp
|
||||
// compType
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { UnitResponse, UnitType } from "@/types/anlagen/units";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
interface Props {
|
||||
unit: UnitResponse | undefined;
|
||||
|
||||
}
|
||||
const emptyUnit: UnitResponse = {
|
||||
id:"",
|
||||
name: "",
|
||||
type: UnitType.BAND,
|
||||
groupId: "",
|
||||
bmk: "",
|
||||
prefix: "",
|
||||
postfix: "",
|
||||
info: "",
|
||||
|
||||
components: [],
|
||||
}
|
||||
|
||||
export function useUnit({ unit }: Props) {
|
||||
|
||||
const [data, setData] = useState<UnitResponse>(emptyUnit);
|
||||
|
||||
useEffect(() => {
|
||||
if (unit) setData(() => ({...emptyUnit, ...unit }))
|
||||
}, [unit])
|
||||
|
||||
const setValue = <K extends keyof UnitResponse>(fieldName: K, value: UnitResponse[K]): void => {
|
||||
setData((cur) => ({ ...cur, [fieldName]: value }))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
const unitType = Object.values(UnitType).map((t => ({ id: t, value: t })))
|
||||
|
||||
function isUnitType(value: string): value is UnitType {
|
||||
return Object.values(UnitType).includes(value as UnitType);
|
||||
}
|
||||
|
||||
function string_to_UnitType(value: string): UnitType {
|
||||
|
||||
if (!isUnitType(value)) {
|
||||
throw new Error(`Ungültiger UnitType: ${value}`);
|
||||
}
|
||||
const unitType: UnitType = value;
|
||||
return unitType
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
}, [])
|
||||
|
||||
return {
|
||||
unitData: data,
|
||||
setValue,
|
||||
emptyUnit,
|
||||
unitType,
|
||||
string_to_UnitType,
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { UnitResponse } from "@/types/anlagen/units";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
interface Props {
|
||||
units: UnitResponse[];
|
||||
|
||||
}
|
||||
|
||||
export function useElements({ units: initUnits }: Props) {
|
||||
const [selectedUnit, setSelectedUnit] = useState<UnitResponse>();
|
||||
|
||||
const [units, setUnits] = useState<UnitResponse[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
setUnits(() => [...orderByListAndNr(initUnits)])
|
||||
}, [initUnits])
|
||||
|
||||
const orderByListAndNr = (units: UnitResponse[]): UnitResponse[] => {
|
||||
return units.sort((unit1, unit2) => {
|
||||
const nameCompare = unit1.name.localeCompare(unit2.name);
|
||||
return nameCompare ;
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
units,
|
||||
selectedUnit: selectedUnit,
|
||||
setSelectedUnnit: setSelectedUnit
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user