Skip to content

Commit

Permalink
cleanup types
Browse files Browse the repository at this point in the history
  • Loading branch information
Psvensso committed Jan 11, 2024
1 parent ba23f9f commit 6f7d0d4
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions packages/react-resizable-panels/src/utils/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ export type PanelGroupState = {
expandToSizes: {
[panelId: string]: number;
};
layout: number[];
panelSizes?: Record<string, number>;
};

type GroupPanelLayouts = Pick<PanelGroupState, "expandToSizes"> & {
layout: number[];
};

function getPanelGroupKey(autoSaveId: string): string {
return `react-resizable-panels:${autoSaveId}`;
}
Expand Down Expand Up @@ -52,32 +55,12 @@ function loadSerializedPanelGroupState(

return null;
}
function removeLayoutItem(
oldKey: string,
oldState: PanelGroupState,
newKey: string
) {
const oldValues = oldKey
.split(",")
.reduce<Record<string, number>>((acc, panelKey: string, i) => {
acc[panelKey] = oldState.layout[i] || 0;
return acc;
}, {});

const newValues = newKey.split(",").reduce<number[]>((acc, newKey) => {
const oldValue = oldValues[newKey];
if (oldValue) {
acc.push(oldValue);
}
return acc;
}, []);
return newValues;
}
export function loadPanelGroupState(
autoSaveId: string,
panels: PanelData[],
storage: PanelGroupStorage
): PanelGroupState | null {
): GroupPanelLayouts | null {
const state = loadSerializedPanelGroupState(autoSaveId, storage);
if (!state) {
return null;
Expand All @@ -86,7 +69,6 @@ export function loadPanelGroupState(
return {
expandToSizes: state.expandToSizes || {},
layout: panels.map(({ id }) => state?.panelSizes?.[id] || 0),
panelSizes: state.panelSizes || {},
};
}

Expand All @@ -98,7 +80,7 @@ export function savePanelGroupState(
storage: PanelGroupStorage
): void {
const panelGroupKey = getPanelGroupKey(autoSaveId);
const oldState = loadPanelGroupState(autoSaveId, panels, storage);
const oldState = loadSerializedPanelGroupState(autoSaveId, storage);
const newState = {
expandToSizes: Object.fromEntries(panelSizesBeforeCollapse.entries()),
panelSizes: {
Expand Down

0 comments on commit 6f7d0d4

Please sign in to comment.