From 7a43686676b6a006f225e154d37da60a365f1d1a Mon Sep 17 00:00:00 2001 From: Kitteh Date: Tue, 25 May 2021 10:39:11 +0100 Subject: [PATCH] fix regression with rendering in KeyValue keys list navagation. --- src/pages/Secrets/KeyValue/KeyValueView.tsx | 101 ++++++++++++-------- 1 file changed, 59 insertions(+), 42 deletions(-) diff --git a/src/pages/Secrets/KeyValue/KeyValueView.tsx b/src/pages/Secrets/KeyValue/KeyValueView.tsx index 7d1b3e7..a8d66d4 100644 --- a/src/pages/Secrets/KeyValue/KeyValueView.tsx +++ b/src/pages/Secrets/KeyValue/KeyValueView.tsx @@ -8,69 +8,81 @@ import i18next from "i18next"; export type KVKeysListProps = { page: Page; + baseMount: string; + secretMountType: string; + secretPath: string[]; }; -type KVKeysListState = - | { - dataLoaded: false; - } - | { - dataLoaded: true; - keys: string[]; - }; +type KVKeysListState = { + dataLoaded: boolean; + keys: string[]; +}; export class KVKeysList extends Component { constructor() { super(); this.state = { dataLoaded: false, + keys: [], }; } - loadData(): void { - void getSecrets( - this.props.page.state.baseMount, - this.props.page.state.secretMountType, - this.props.page.state.secretPath, - ) - .then((keys) => { - this.setState({ - dataLoaded: true, - keys: keys, - }); - }) - .catch((e: Error) => { - // getSecrets also 404's on no keys so dont go all the way back. - if (e == DoesNotExistError) { - if (this.props.page.state.secretPath.length != 0) { - void this.props.page.goBack(); - return; - } else { - this.setState({ - dataLoaded: true, - keys: null, - }); - } - } else { - setErrorText(e.message); - } + async loadData(): Promise { + const page = this.props.page; + try { + const keys = await getSecrets( + this.props.baseMount, + this.props.secretMountType, + this.props.secretPath, + ); + this.setState({ + dataLoaded: true, + keys: keys, }); - return; + return; + } catch (e: unknown) { + const error = e as Error; + if (error == DoesNotExistError) { + // getSecrets also 404's on no keys so dont go all the way back. + if (this.props.secretPath.length != 0) { + await page.goBack(); + return; + } + } else { + setErrorText(error.message); + } + this.setState({ + dataLoaded: true, + keys: null, + }); + } + } + + componentDidUpdate(prevProps: KVKeysListProps): void { + // Do not set state.dataLoaded to false in here + // to provide a more smooth experiance when going through multiple keys. + if ( + prevProps.baseMount !== this.props.baseMount || + prevProps.secretMountType !== this.props.secretMountType || + prevProps.secretPath !== this.props.secretPath + ) { + void this.loadData(); + } } componentDidMount(): void { - if (!this.state.dataLoaded) { - this.loadData(); - } + void this.loadData(); } render(): JSX.Element { if (!this.state.dataLoaded) { return

{i18next.t("content_loading")}

; } + if (this.state.keys == null) { return

{i18next.t("kv_view_none_here_text")}

; } + return (