From 1e92093a50962f4702339e872caa4f82af90af70 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 12 Apr 2022 12:54:57 +0200 Subject: anastasis: discovery --- .../src/hooks/use-anastasis-reducer.ts | 77 ++++++++++++++++++++-- 1 file changed, 71 insertions(+), 6 deletions(-) (limited to 'packages/anastasis-webui/src/hooks') diff --git a/packages/anastasis-webui/src/hooks/use-anastasis-reducer.ts b/packages/anastasis-webui/src/hooks/use-anastasis-reducer.ts index b18610427..321cf3f0a 100644 --- a/packages/anastasis-webui/src/hooks/use-anastasis-reducer.ts +++ b/packages/anastasis-webui/src/hooks/use-anastasis-reducer.ts @@ -1,8 +1,12 @@ import { TalerErrorCode } from "@gnu-taler/taler-util"; import { + AggregatedPolicyMetaInfo, BackupStates, + discoverPolicies, + DiscoveryCursor, getBackupStartState, getRecoveryStartState, + PolicyMetaInfo, RecoveryStates, reduceAction, ReducerState, @@ -15,6 +19,7 @@ const remoteReducer = false; interface AnastasisState { reducerState: ReducerState | undefined; currentError: any; + discoveryState: DiscoveryUiState; } async function getBackupStartStateRemote(): Promise { @@ -98,9 +103,21 @@ export interface ReducerTransactionHandle { transition(action: string, args: any): Promise; } +/** + * UI-relevant state of the policy discovery process. + */ +export interface DiscoveryUiState { + state: "none" | "active" | "finished"; + + aggregatedPolicies?: AggregatedPolicyMetaInfo[]; + + cursor?: DiscoveryCursor; +} + export interface AnastasisReducerApi { currentReducerState: ReducerState | undefined; currentError: any; + discoveryState: DiscoveryUiState; dismissError: () => void; startBackup: () => void; startRecover: () => void; @@ -109,6 +126,8 @@ export interface AnastasisReducerApi { transition(action: string, args: any): Promise; exportState: () => string; importState: (s: string) => void; + discoverStart(): Promise; + discoverMore(): Promise; /** * Run multiple reducer steps in a transaction without * affecting the UI-visible transition state in-between. @@ -152,6 +171,9 @@ export function useAnastasisReducer(): AnastasisReducerApi { () => ({ reducerState: getStateFromStorage(), currentError: undefined, + discoveryState: { + state: "none", + }, }), ); @@ -192,6 +214,7 @@ export function useAnastasisReducer(): AnastasisReducerApi { return { currentReducerState: anastasisState.reducerState, currentError: anastasisState.currentError, + discoveryState: anastasisState.discoveryState, async startBackup() { let s: ReducerState; if (remoteReducer) { @@ -213,17 +236,59 @@ export function useAnastasisReducer(): AnastasisReducerApi { } }, exportState() { - const state = getStateFromStorage() - return JSON.stringify(state) + const state = getStateFromStorage(); + return JSON.stringify(state); }, importState(s: string) { try { - const state = JSON.parse(s) - setAnastasisState({ reducerState: state, currentError: undefined }) + const state = JSON.parse(s); + setAnastasisState({ + reducerState: state, + currentError: undefined, + discoveryState: { + state: "none", + }, + }); } catch (e) { - throw Error('could not restore the state') + throw Error("could not restore the state"); + } + }, + async discoverStart(): Promise { + const res = await discoverPolicies(this.currentReducerState!, undefined); + const aggregatedPolicies: AggregatedPolicyMetaInfo[] = []; + const polHashToIndex: Record = {}; + for (const pol of res.policies) { + const oldIndex = polHashToIndex[pol.policy_hash]; + if (oldIndex != null) { + aggregatedPolicies[oldIndex].providers.push({ + provider_url: pol.provider_url, + version: pol.version, + }); + } else { + aggregatedPolicies.push({ + attribute_mask: pol.attribute_mask, + policy_hash: pol.policy_hash, + providers: [ + { + provider_url: pol.provider_url, + version: pol.version, + }, + ], + secret_name: pol.secret_name, + }); + polHashToIndex[pol.policy_hash] = aggregatedPolicies.length - 1; + } } + setAnastasisState({ + ...anastasisState, + discoveryState: { + state: "finished", + aggregatedPolicies, + cursor: res.cursor, + }, + }); }, + async discoverMore(): Promise {}, async startRecover() { let s: ReducerState; if (remoteReducer) { @@ -301,7 +366,7 @@ export function useAnastasisReducer(): AnastasisReducerApi { } class ReducerTxImpl implements ReducerTransactionHandle { - constructor(public transactionState: ReducerState) { } + constructor(public transactionState: ReducerState) {} async transition(action: string, args: any): Promise { let s: ReducerState; if (remoteReducer) { -- cgit v1.2.3