Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

1.4 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */
import type { PanelGroupStorage } from "react-resizable-panels";
import { z } from "zod";
import { Objects } from "@/utils/objects";

const schema = z.record(z.string(), z.tuple([z.number(), z.number()]));

let storedValue: string | null = null;

/**
 * This does 2 things:
 *  - stores the storage in memory, so it persists across moving the sidebar
 *  - flips the order of the store when the direction is flipped, since the helper sidebar
 *  will either come first or last.
 */
export function createStorage(location: "left" | "bottom"): PanelGroupStorage {
  return {
    getItem(_name) {
      if (!storedValue) {
        return storedValue ?? null;
      }
      if (location === "left") {
        return storedValue;
      }

      // flip
      try {
        const parsed = schema.parse(JSON.parse(storedValue));
        return JSON.stringify(
          Objects.mapValues(parsed, (value) => {
            return value.toReversed();
          }),
        );
      } catch {
        return null;
      }
    },
    setItem(name, value) {
      if (location !== "left") {
        // flip
        try {
          const parsed = schema.parse(JSON.parse(value));
          value = JSON.stringify(
            Objects.mapValues(parsed, (value) => {
              return value.toReversed();
            }),
          );
        } catch {
          return null;
        }
      }

      storedValue = value || null;
    },
  };
}