mirror of
https://github.com/tiqi-group/pydase.git
synced 2025-06-06 13:30:41 +02:00
frontend: adds support for displayOrder in web settings
This commit is contained in:
parent
7bc12b340f
commit
0d70b7492d
@ -5,5 +5,5 @@ export const WebSettingsContext = createContext<Record<string, WebSetting>>({});
|
|||||||
export interface WebSetting {
|
export interface WebSetting {
|
||||||
displayName: string;
|
displayName: string;
|
||||||
display: boolean;
|
display: boolean;
|
||||||
index: number;
|
displayOrder: number;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import { GenericComponent } from "./GenericComponent";
|
|||||||
import { LevelName } from "./NotificationsComponent";
|
import { LevelName } from "./NotificationsComponent";
|
||||||
import { SerializedObject } from "../types/SerializedObject";
|
import { SerializedObject } from "../types/SerializedObject";
|
||||||
import useLocalStorage from "../hooks/useLocalStorage";
|
import useLocalStorage from "../hooks/useLocalStorage";
|
||||||
|
import useSortedEntries from "../hooks/useSortedEntries";
|
||||||
|
|
||||||
interface DataServiceProps {
|
interface DataServiceProps {
|
||||||
props: DataServiceJSON;
|
props: DataServiceJSON;
|
||||||
@ -21,6 +22,8 @@ export const DataServiceComponent = React.memo(
|
|||||||
// Retrieve the initial state from localStorage, default to true if not found
|
// Retrieve the initial state from localStorage, default to true if not found
|
||||||
const [open, setOpen] = useLocalStorage(`dataServiceComponent-${id}-open`, true);
|
const [open, setOpen] = useLocalStorage(`dataServiceComponent-${id}-open`, true);
|
||||||
|
|
||||||
|
const sortedEntries = useSortedEntries(props);
|
||||||
|
|
||||||
if (displayName !== "") {
|
if (displayName !== "") {
|
||||||
return (
|
return (
|
||||||
<div className="component dataServiceComponent" id={id}>
|
<div className="component dataServiceComponent" id={id}>
|
||||||
@ -30,9 +33,9 @@ export const DataServiceComponent = React.memo(
|
|||||||
</Card.Header>
|
</Card.Header>
|
||||||
<Collapse in={open}>
|
<Collapse in={open}>
|
||||||
<Card.Body>
|
<Card.Body>
|
||||||
{Object.entries(props).map(([key, value]) => (
|
{sortedEntries.map((value) => (
|
||||||
<GenericComponent
|
<GenericComponent
|
||||||
key={key}
|
key={value.full_access_path}
|
||||||
attribute={value}
|
attribute={value}
|
||||||
isInstantUpdate={isInstantUpdate}
|
isInstantUpdate={isInstantUpdate}
|
||||||
addNotification={addNotification}
|
addNotification={addNotification}
|
||||||
@ -46,9 +49,9 @@ export const DataServiceComponent = React.memo(
|
|||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
<div className="component dataServiceComponent" id={id}>
|
<div className="component dataServiceComponent" id={id}>
|
||||||
{Object.entries(props).map(([key, value]) => (
|
{sortedEntries.map((value) => (
|
||||||
<GenericComponent
|
<GenericComponent
|
||||||
key={key}
|
key={value.full_access_path}
|
||||||
attribute={value}
|
attribute={value}
|
||||||
isInstantUpdate={isInstantUpdate}
|
isInstantUpdate={isInstantUpdate}
|
||||||
addNotification={addNotification}
|
addNotification={addNotification}
|
||||||
|
@ -4,6 +4,7 @@ import { GenericComponent } from "./GenericComponent";
|
|||||||
import { LevelName } from "./NotificationsComponent";
|
import { LevelName } from "./NotificationsComponent";
|
||||||
import { SerializedObject } from "../types/SerializedObject";
|
import { SerializedObject } from "../types/SerializedObject";
|
||||||
import { useRenderCount } from "../hooks/useRenderCount";
|
import { useRenderCount } from "../hooks/useRenderCount";
|
||||||
|
import useSortedEntries from "../hooks/useSortedEntries";
|
||||||
|
|
||||||
interface DictComponentProps {
|
interface DictComponentProps {
|
||||||
value: Record<string, SerializedObject>;
|
value: Record<string, SerializedObject>;
|
||||||
@ -14,16 +15,16 @@ interface DictComponentProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const DictComponent = React.memo((props: DictComponentProps) => {
|
export const DictComponent = React.memo((props: DictComponentProps) => {
|
||||||
const { value, docString, isInstantUpdate, addNotification, id } = props;
|
const { docString, isInstantUpdate, addNotification, id } = props;
|
||||||
|
|
||||||
|
const sortedEntries = useSortedEntries(props.value);
|
||||||
const renderCount = useRenderCount();
|
const renderCount = useRenderCount();
|
||||||
const valueArray = Object.values(value);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={"listComponent"} id={id}>
|
<div className={"listComponent"} id={id}>
|
||||||
{process.env.NODE_ENV === "development" && <div>Render count: {renderCount}</div>}
|
{process.env.NODE_ENV === "development" && <div>Render count: {renderCount}</div>}
|
||||||
<DocStringComponent docString={docString} />
|
<DocStringComponent docString={docString} />
|
||||||
{valueArray.map((item) => {
|
{sortedEntries.map((item) => {
|
||||||
return (
|
return (
|
||||||
<GenericComponent
|
<GenericComponent
|
||||||
key={item.full_access_path}
|
key={item.full_access_path}
|
||||||
|
@ -4,6 +4,7 @@ import { GenericComponent } from "./GenericComponent";
|
|||||||
import { LevelName } from "./NotificationsComponent";
|
import { LevelName } from "./NotificationsComponent";
|
||||||
import { SerializedObject } from "../types/SerializedObject";
|
import { SerializedObject } from "../types/SerializedObject";
|
||||||
import { useRenderCount } from "../hooks/useRenderCount";
|
import { useRenderCount } from "../hooks/useRenderCount";
|
||||||
|
import useSortedEntries from "../hooks/useSortedEntries";
|
||||||
|
|
||||||
interface ListComponentProps {
|
interface ListComponentProps {
|
||||||
value: SerializedObject[];
|
value: SerializedObject[];
|
||||||
@ -14,7 +15,9 @@ interface ListComponentProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const ListComponent = React.memo((props: ListComponentProps) => {
|
export const ListComponent = React.memo((props: ListComponentProps) => {
|
||||||
const { value, docString, isInstantUpdate, addNotification, id } = props;
|
const { docString, isInstantUpdate, addNotification, id } = props;
|
||||||
|
|
||||||
|
const sortedEntries = useSortedEntries(props.value);
|
||||||
|
|
||||||
const renderCount = useRenderCount();
|
const renderCount = useRenderCount();
|
||||||
|
|
||||||
@ -22,7 +25,7 @@ export const ListComponent = React.memo((props: ListComponentProps) => {
|
|||||||
<div className={"listComponent"} id={id}>
|
<div className={"listComponent"} id={id}>
|
||||||
{process.env.NODE_ENV === "development" && <div>Render count: {renderCount}</div>}
|
{process.env.NODE_ENV === "development" && <div>Render count: {renderCount}</div>}
|
||||||
<DocStringComponent docString={docString} />
|
<DocStringComponent docString={docString} />
|
||||||
{value.map((item) => {
|
{sortedEntries.map((item) => {
|
||||||
return (
|
return (
|
||||||
<GenericComponent
|
<GenericComponent
|
||||||
key={item.full_access_path}
|
key={item.full_access_path}
|
||||||
|
28
frontend/src/hooks/useSortedEntries.ts
Normal file
28
frontend/src/hooks/useSortedEntries.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { useContext } from "react";
|
||||||
|
import { WebSettingsContext } from "../WebSettings";
|
||||||
|
import { SerializedObject } from "../types/SerializedObject";
|
||||||
|
|
||||||
|
export default function useSortedEntries(
|
||||||
|
props: Record<string, SerializedObject> | SerializedObject[],
|
||||||
|
) {
|
||||||
|
const webSettings = useContext(WebSettingsContext);
|
||||||
|
|
||||||
|
// Get the order for sorting
|
||||||
|
const getOrder = (fullAccessPath: string) => {
|
||||||
|
return webSettings[fullAccessPath]?.displayOrder ?? Number.MAX_SAFE_INTEGER;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Sort entries based on whether props is an array or an object
|
||||||
|
let sortedEntries;
|
||||||
|
if (Array.isArray(props)) {
|
||||||
|
// Need to make copy of array to leave the original array unmodified
|
||||||
|
sortedEntries = [...props].sort((objectA, objectB) => {
|
||||||
|
return getOrder(objectA.full_access_path) - getOrder(objectB.full_access_path);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sortedEntries = Object.values(props).sort((objectA, objectB) => {
|
||||||
|
return getOrder(objectA.full_access_path) - getOrder(objectB.full_access_path);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return sortedEntries;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user