Files
archie/ui/src/settings/connection.ts
T

53 lines
1.7 KiB
TypeScript

/** The operations the runtime can perform through a connection. */
export const OPERATIONS = ["clone", "push", "pr"] as const;
/** One organisation-owned forge connection, as the document writes it. */
export interface Connection {
id: string;
name: string;
type: string;
host: string;
account: string;
token: { engine: string; key: string };
/** Permitted operations per execution identity ID. */
permits?: Record<string, string[]>;
}
/** permits renders the document's map as rows the page can edit. */
export const permits = (c: Connection): [string, string[]][] =>
Object.entries(c.permits ?? {});
export function addPermit(c: Connection): void {
c.permits ??= {};
c.permits[""] = ["clone"];
}
export function removePermit(c: Connection, id: string): void {
if (c.permits) delete c.permits[id];
}
// Renaming rebuilds the entry: the identity reference is the map key.
export function renamePermit(c: Connection, from: string, to: string): void {
if (!c.permits || from === to) return;
const operations = c.permits[from];
delete c.permits[from];
if (to) c.permits[to] = operations;
}
export function toggleOperation(
c: Connection,
id: string,
operation: string,
on: boolean,
): void {
const current = new Set(c.permits?.[id] ?? []);
if (on) current.add(operation);
else current.delete(operation);
c.permits ??= {};
c.permits[id] = OPERATIONS.filter((o) => current.has(o));
}
/** Identities a permit row may pick: any not already permitted on another row. */
export const choosableIdentities = <T extends { id: string }>(c: Connection, identities: T[], current: string): T[] =>
identities.filter((a) => a.id === current || !(a.id in (c.permits ?? {})));