Skip to content

Commit

Permalink
Merge pull request #50 from teloscube/alioguzhan/update-decaf-context
Browse files Browse the repository at this point in the history
feat(context): add private configuration support
  • Loading branch information
alioguzhan authored Jan 26, 2025
2 parents 797ce36 + a6f9bc0 commit 30c3b8c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
5 changes: 4 additions & 1 deletion shell.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
with import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/24.05.tar.gz") { };
with import
(fetchTarball
"https://github.com/NixOS/nixpkgs/archive/release-24.11.tar.gz")
{ };

stdenv.mkDerivation {
name = "decaf-react";
Expand Down
16 changes: 11 additions & 5 deletions src/DecafApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import DecafVersionChecker from './DecafVersionChecker';
import { DecafWebappController } from './DecafWebappController';
import { OfflineNotifier } from './OfflineChecker';
import ZendeskWidget from './ZendeskWidget';
import { DecafContext, Principal, PublicConfig } from './context';
import { DecafContext, Principal, PrivateConfig, PublicConfig } from './context';

export interface DecafAppConfig {
/** version of the application */
Expand Down Expand Up @@ -33,6 +33,7 @@ export default function DecafApp(props: DecafAppProps) {
const [client, setClient] = React.useState<DecafClient | undefined>(undefined);
const [me, setMe] = React.useState<Principal | undefined>(undefined);
const [publicConfig, setPublicConfig] = React.useState<PublicConfig | undefined>(undefined);
const [privateConfig, setPrivateConfig] = React.useState<PrivateConfig | undefined>(undefined);
const [loading, setLoading] = React.useState(true);
const [redirectReason, setRedirectReason] = React.useState<RedirectReason>('not-authenticated');
const authInterval = React.useRef<NodeJS.Timeout | undefined>(undefined);
Expand All @@ -54,11 +55,16 @@ export default function DecafApp(props: DecafAppProps) {
const client = controller.getDecafClient();

if (client) {
Promise.all([client.barista.get('/me/'), client.barista.get('/conf/public/')])
.then(([meResp, configResp]) => {
Promise.all([
client.barista.get('/me/'),
client.barista.get('/conf/public/'),
client.barista.get('/conf/private/'),
])
.then(([meResp, configResp, privateConfig]) => {
setClient(client);
setMe(meResp.data);
setPublicConfig(configResp.data);
setPrivateConfig(privateConfig.data);
setLoading(false);
})
.catch(() => {
Expand Down Expand Up @@ -94,11 +100,11 @@ export default function DecafApp(props: DecafAppProps) {

if (loading) {
return <>{controller.loadingComponent}</>;
} else if (client === undefined || me === undefined || publicConfig === undefined) {
} else if (client === undefined || me === undefined || publicConfig === undefined || privateConfig === undefined) {
return controller.onInvalidSession(redirectReason);
} else {
return (
<DecafContext.Provider value={{ client, me, publicConfig, controller }}>
<DecafContext.Provider value={{ client, me, publicConfig, privateConfig, controller }}>
{props.config?.currentVersion && (
<DecafVersionChecker
basePath={props.config.basePath}
Expand Down
31 changes: 31 additions & 0 deletions src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const DecafContext = React.createContext<DecafContextType>({
client: undefined as unknown as DecafClient,
me: undefined as unknown as Principal,
publicConfig: undefined as unknown as PublicConfig,
privateConfig: undefined as unknown as PrivateConfig,
controller: undefined as unknown as DecafAppController,
});

Expand All @@ -23,6 +24,7 @@ export interface DecafContextType {
client: DecafClient;
me: Principal;
publicConfig: PublicConfig;
privateConfig: PrivateConfig;
controller: DecafAppController;
}

Expand Down Expand Up @@ -73,3 +75,32 @@ export interface PublicConfig {
/** password reset feature should be enabled or not */
pwdreset: true;
}

/**
* Type encoding for global private deployment configuration.
*
* This configuration value is instantiated during runtime from
* `/api/conf/private/` API endpoint.
*/
export interface PrivateConfig {
functions: {
portmngt: boolean;
fundmngt: boolean;
ebanking: boolean;
pretrade: boolean;
};
beanbag?: string;
releasenotes: string;
documentation: Resource[];
}

/**
* Type encoding for a single documentation item.
*/
export interface Resource {
title: string;
description: string;
role: 'privileged' | 'internal' | '*';
authed: boolean;
document: { type: 'internal' | 'external'; link: string };
}
2 changes: 1 addition & 1 deletion src/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { DecafApp, DecafSpinner } from './';

Expand Down

0 comments on commit 30c3b8c

Please sign in to comment.