Skip to content

Commit

Permalink
Rework guiIdSetFromContainerId into a dict of sets (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahbedouch authored Aug 9, 2023
1 parent d3ba8f0 commit 4b029aa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions viser/client/src/ControlPanel/Generated.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export default function GeneratedGuiContainer({
if (viewer === undefined) viewer = React.useContext(ViewerContext)!;

const guiIdSet = viewer.useGui(
(state) => state.guiIdSetFromContainerId[containerId],
(state) => state.guiIdSetFromContainerId[containerId]
);
const guiConfigFromId = viewer.useGui((state) => state.guiConfigFromId);

// Render each GUI element in this container.
const out =
guiIdSet === undefined ? null : (
<Box pt="xs">
{[...Object.keys(guiIdSet)]
{[...guiIdSet]
.map((id) => guiConfigFromId[id])
.sort((a, b) => a.order - b.order)
.map((conf, index) => {

Check warning on line 50 in viser/client/src/ControlPanel/Generated.tsx

View workflow job for this annotation

GitHub Actions / eslint

'index' is defined but never used
Expand Down
15 changes: 6 additions & 9 deletions viser/client/src/ControlPanel/GuiState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ interface GuiState {
server: string;
websocketConnected: boolean;
backgroundAvailable: boolean;
// We use an object whose values are always null to emulate a set.
// TODO: is there a less hacky way?
guiIdSetFromContainerId: {
[containerId: string]: { [configId: string]: null } | undefined;
[containerId: string]: Set<string> | undefined;
};
guiConfigFromId: { [id: string]: GuiConfig };
guiValueFromId: { [id: string]: any };
Expand Down Expand Up @@ -82,10 +80,9 @@ export function useGuiState(initialServer: string) {
addGui: (guiConfig) =>
set((state) => {
state.guiConfigFromId[guiConfig.id] = guiConfig;
state.guiIdSetFromContainerId[guiConfig.container_id] = {
...state.guiIdSetFromContainerId[guiConfig.container_id],
[guiConfig.id]: null,
};
state.guiIdSetFromContainerId[guiConfig.container_id] = new Set(
state.guiIdSetFromContainerId[guiConfig.container_id]
).add(guiConfig.id);
}),
setGuiValue: (id, value) =>
set((state) => {
Expand Down Expand Up @@ -113,9 +110,9 @@ export function useGuiState(initialServer: string) {
if (guiConfig.type === "GuiAddTabGroupMessage")
guiConfig.tab_container_ids.forEach(state.removeGuiContainer);

delete state.guiIdSetFromContainerId[guiConfig.container_id]![
state.guiIdSetFromContainerId[guiConfig.container_id]!.delete(
guiConfig.id
];
);
delete state.guiConfigFromId[id];
delete state.guiValueFromId[id];
delete state.guiAttributeFromId[id];
Expand Down
3 changes: 3 additions & 0 deletions viser/client/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import ReactDOM from "react-dom/client";
import { Root } from "./App";
import { enableMapSet } from "immer"

enableMapSet();

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<Root />,
Expand Down

0 comments on commit 4b029aa

Please sign in to comment.