|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import { ReactWidget } from '@jupyterlab/apputils'; |
| 3 | + |
| 4 | +import '../../style/base.css'; |
| 5 | +import { ISecret, ISecretsManager } from '../token'; |
| 6 | + |
| 7 | +interface ISecretsPanelProps { |
| 8 | + manager: ISecretsManager; |
| 9 | +} |
| 10 | + |
| 11 | +const EyeIcon = () => ( |
| 12 | + <svg viewBox="0 0 24 24" fill="currentColor"> |
| 13 | + <path d="M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z" /> |
| 14 | + </svg> |
| 15 | +); |
| 16 | + |
| 17 | +const EyeOffIcon = () => ( |
| 18 | + <svg viewBox="0 0 24 24" fill="currentColor"> |
| 19 | + <path d="M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z" /> |
| 20 | + </svg> |
| 21 | +); |
| 22 | + |
| 23 | +const SecretsPanel: React.FC<ISecretsPanelProps> = ({ manager }) => { |
| 24 | + const [secrets, setSecrets] = useState<ISecret[]>([]); |
| 25 | + const [isLoading, setIsLoading] = useState(true); |
| 26 | + const [namespaces, setNamespaces] = useState<string[]>([]); |
| 27 | + const [currentNamespace, setCurrentNamespace] = useState('default'); |
| 28 | + const [visibleSecrets, setVisibleSecrets] = useState<Set<string>>(new Set()); |
| 29 | + |
| 30 | + const fetchNamespaces = async () => { |
| 31 | + try { |
| 32 | + const namespacesList = await manager.listNamespaces(); |
| 33 | + if (namespacesList) { |
| 34 | + setNamespaces(namespacesList); |
| 35 | + } |
| 36 | + } catch (error) { |
| 37 | + console.error('Error fetching namespaces:', error); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const fetchSecrets = async () => { |
| 42 | + try { |
| 43 | + const secretsList = await manager.list(currentNamespace); |
| 44 | + if (secretsList) { |
| 45 | + setSecrets(secretsList.values); |
| 46 | + } |
| 47 | + } catch (error) { |
| 48 | + console.error('Error fetching secrets:', error); |
| 49 | + } finally { |
| 50 | + setIsLoading(false); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + fetchNamespaces(); |
| 56 | + }, []); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + fetchSecrets(); |
| 60 | + }, [currentNamespace]); |
| 61 | + |
| 62 | + const toggleSecretVisibility = (secretId: string) => { |
| 63 | + setVisibleSecrets(prev => { |
| 64 | + const newSet = new Set(prev); |
| 65 | + if (newSet.has(secretId)) { |
| 66 | + newSet.delete(secretId); |
| 67 | + } else { |
| 68 | + newSet.add(secretId); |
| 69 | + } |
| 70 | + return newSet; |
| 71 | + }); |
| 72 | + }; |
| 73 | + |
| 74 | + if (isLoading) { |
| 75 | + return <div>Loading secrets...</div>; |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="jp-SecretsPanel"> |
| 80 | + <div className="jp-SecretsPanel-header"> |
| 81 | + <h2>Secrets Manager</h2> |
| 82 | + <div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}> |
| 83 | + <select |
| 84 | + className="jp-SecretsPanel-namespace-select" |
| 85 | + value={currentNamespace} |
| 86 | + onChange={e => setCurrentNamespace(e.target.value)} |
| 87 | + > |
| 88 | + {namespaces.map(namespace => ( |
| 89 | + <option key={namespace} value={namespace}> |
| 90 | + {namespace} |
| 91 | + </option> |
| 92 | + ))} |
| 93 | + </select> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div className="jp-SecretsPanel-content"> |
| 98 | + {secrets.length === 0 ? ( |
| 99 | + <div className="jp-SecretsPanel-empty">No secrets found.</div> |
| 100 | + ) : ( |
| 101 | + <ul className="jp-SecretsPanel-list"> |
| 102 | + {secrets.map(secret => ( |
| 103 | + <li key={secret.id} className="jp-SecretsPanel-list-item"> |
| 104 | + <span className="jp-SecretsPanel-secret-name">{secret.id}</span> |
| 105 | + <span |
| 106 | + className={`jp-SecretsPanel-secret-value ${!visibleSecrets.has(secret.id) ? 'hidden' : ''}`} |
| 107 | + > |
| 108 | + {visibleSecrets.has(secret.id) ? secret.value : '••••••••'} |
| 109 | + </span> |
| 110 | + <button |
| 111 | + className="jp-SecretsPanel-eye-button" |
| 112 | + onClick={() => toggleSecretVisibility(secret.id)} |
| 113 | + title={ |
| 114 | + visibleSecrets.has(secret.id) |
| 115 | + ? 'Hide secret' |
| 116 | + : 'Show secret' |
| 117 | + } |
| 118 | + > |
| 119 | + {visibleSecrets.has(secret.id) ? <EyeIcon /> : <EyeOffIcon />} |
| 120 | + </button> |
| 121 | + </li> |
| 122 | + ))} |
| 123 | + </ul> |
| 124 | + )} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export interface ISecretsManagerWidgetOptions { |
| 131 | + manager: ISecretsManager; |
| 132 | +} |
| 133 | + |
| 134 | +export class SecretsManagerWidget extends ReactWidget { |
| 135 | + constructor(options: ISecretsManagerWidgetOptions) { |
| 136 | + super(); |
| 137 | + this.addClass('jp-SecretsManagerWidget'); |
| 138 | + this._manager = options.manager; |
| 139 | + } |
| 140 | + |
| 141 | + render(): JSX.Element { |
| 142 | + return <SecretsPanel manager={this._manager} />; |
| 143 | + } |
| 144 | + |
| 145 | + private _manager: ISecretsManager; |
| 146 | +} |
0 commit comments