Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/src/agent/detail/permissions.ts

47 lines
1.7 KiB
TypeScript

// The detail panel's permissions editor: what it starts on and how a granted
// set reads. Kept out of the component so `npm test` can run it.
/**
* The editor's starting selection: the swarm's stored list when it sets one,
* else the default set. Names the catalogue (`all`) does not list are dropped,
* since the route refuses an unknown one, and the operator would be unable to
* clear a box they cannot see.
*/
export function editorSelection(
stored: string[] | undefined,
all: string[],
defaults: string[],
): string[] {
return (stored ?? defaults).filter((p) => all.includes(p));
}
/** What the panel says about a stored `permissions` field. */
export function grantSummary(stored: string[] | undefined): string {
if (stored === undefined) return "the default set";
if (stored.length === 0) return "none";
return `${stored.length} granted`;
}
/**
* How the panel reads the stored `unrestricted_internet`. An absent value
* counts as on, and says "legacy" because the swarm never set it.
*/
export function internetSummary(stored: boolean | undefined): string {
if (stored === undefined) return "unrestricted (legacy)";
return stored ? "unrestricted" : "restricted";
}
/**
* The `PUT` body that sets the internet flag. A `PUT` without `permissions`
* resets a stored list to the default, so the stored list goes along; an
* agent with none stored sends the flag alone, which leaves it unset.
*/
export function internetBody(
stored: string[] | undefined,
unrestricted: boolean,
): { permissions?: string[]; unrestricted_internet: boolean } {
return {
...(stored === undefined ? {} : { permissions: stored }),
unrestricted_internet: unrestricted,
};
}