-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d554d6
commit d058feb
Showing
3 changed files
with
122 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/packages/state/src/recoil/sidebar/sidebar-utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
vi.mock("recoil"); | ||
vi.mock("recoil-relay"); | ||
|
||
import { mergeGroups } from "./sidebar-utils"; | ||
|
||
describe("test sidebar groups resolution", () => { | ||
it("merges current and config groups", () => { | ||
expect( | ||
mergeGroups( | ||
[ | ||
{ name: "one", paths: ["one.one", "one.three"] }, | ||
{ name: "three", paths: [] }, | ||
], | ||
|
||
[ | ||
{ name: "zero", paths: [] }, | ||
{ | ||
name: "one", | ||
paths: ["one.zero", "one.one", "one.two"], | ||
}, | ||
{ name: "two", paths: [] }, | ||
] | ||
) | ||
).toStrictEqual([ | ||
{ name: "zero", paths: [] }, | ||
{ name: "one", paths: ["one.zero", "one.one", "one.two", "one.three"] }, | ||
{ name: "two", paths: [] }, | ||
{ name: "three", paths: [] }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { State } from "../types"; | ||
|
||
const hasNeighbor = (sink: string[], source: string[], key: string) => { | ||
const index = source.indexOf(key); | ||
const before = source[index - 1]; | ||
const after = source[index + 1]; | ||
|
||
return sink.includes(before) || sink.includes(after); | ||
}; | ||
|
||
const insertFromNeighbor = (sink: string[], source: string[], key: string) => { | ||
if (sink.includes(key)) { | ||
return; | ||
} | ||
|
||
const sourceIndex = source.indexOf(key); | ||
const before = source[sourceIndex - 1]; | ||
const beforeIndex = sink.indexOf(before); | ||
|
||
if (beforeIndex >= 0) { | ||
sink.splice(beforeIndex + 1, 0, key); | ||
return; | ||
} | ||
|
||
const after = source[sourceIndex + 1]; | ||
const afterIndex = sink.indexOf(after); | ||
|
||
if (afterIndex >= 0) { | ||
sink.splice(afterIndex, 0, key); | ||
return; | ||
} | ||
|
||
sink.push(key); | ||
return; | ||
}; | ||
|
||
const merge = (sink: string[], source: string[]) => { | ||
const missing = new Set(source.filter((key) => !sink.includes(key))); | ||
|
||
while (missing.size) { | ||
const force = ![...missing].some((name) => hasNeighbor(sink, source, name)); | ||
for (const name of missing) { | ||
if (!force && !hasNeighbor(sink, source, name)) { | ||
continue; | ||
} | ||
insertFromNeighbor(sink, source, name); | ||
missing.delete(name); | ||
} | ||
} | ||
}; | ||
|
||
export const mergeGroups = ( | ||
sink: State.SidebarGroup[], | ||
source: State.SidebarGroup[] | ||
) => { | ||
const mapping = Object.fromEntries(sink.map((g) => [g.name, g])); | ||
const configMapping = Object.fromEntries(source.map((g) => [g.name, g])); | ||
const sinkKeys = sink.map(({ name }) => name); | ||
const sourceKeys = source.map(({ name }) => name); | ||
|
||
merge(sinkKeys, sourceKeys); | ||
|
||
for (const key of sinkKeys) { | ||
mapping[key] = mapping[key] ?? configMapping[key]; | ||
} | ||
const resolved = sinkKeys.map((g) => mapping[g] ?? configMapping[g]); | ||
for (const { name } of resolved) { | ||
const i = sourceKeys.indexOf(name); | ||
if (i < 0) { | ||
continue; | ||
} | ||
|
||
merge(mapping[name].paths || [], source[i].paths); | ||
} | ||
|
||
return resolved; | ||
}; |