|
| 1 | +import React, { FC } from 'react' |
| 2 | +import Button from 'components/base/forms/Button' |
| 3 | +import Tooltip from 'components/Tooltip' |
| 4 | +import Constants from 'common/constants' |
| 5 | +import { useHasPermission } from 'common/providers/Permission' |
| 6 | +import { EnvironmentPermission } from 'common/types/permissions.types' |
| 7 | +import CreateServerSideKeyModal from './CreateServerSideKeyModal' |
| 8 | +import ServerSideKeyRow from './ServerSideKeyRow' |
| 9 | +import { useServerSideKeys } from 'components/pages/sdk-keys/hooks/useServerSideKeys' |
| 10 | + |
| 11 | +type ServerSideKey = { |
| 12 | + id: string |
| 13 | + key: string |
| 14 | + name: string |
| 15 | +} |
| 16 | + |
| 17 | +type ServerSideSDKKeysProps = { |
| 18 | + environmentId: string |
| 19 | + environmentName: string |
| 20 | +} |
| 21 | + |
| 22 | +const ServerSideSDKKeys: FC<ServerSideSDKKeysProps> = ({ |
| 23 | + environmentId, |
| 24 | + environmentName, |
| 25 | +}) => { |
| 26 | + const { permission: isAdmin } = useHasPermission({ |
| 27 | + id: environmentId, |
| 28 | + level: 'environment', |
| 29 | + permission: EnvironmentPermission.ADMIN, |
| 30 | + }) |
| 31 | + |
| 32 | + const { handleCreateKey, handleRemove, isDeletingKey, isLoading, keys } = |
| 33 | + useServerSideKeys({ environmentId }) |
| 34 | + |
| 35 | + const handleCreate = () => { |
| 36 | + openModal( |
| 37 | + 'Create Server-side Environment Keys', |
| 38 | + <CreateServerSideKeyModal |
| 39 | + environmentName={environmentName} |
| 40 | + onSubmit={handleCreateKey} |
| 41 | + />, |
| 42 | + 'p-0', |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + const filterByName = (item: ServerSideKey, search: string) => |
| 47 | + item.name.toLowerCase().includes(search.toLowerCase()) |
| 48 | + |
| 49 | + const renderKeyRow = ({ id, key, name }: ServerSideKey) => ( |
| 50 | + <ServerSideKeyRow |
| 51 | + id={id} |
| 52 | + keyValue={key} |
| 53 | + name={name} |
| 54 | + isDeleting={isDeletingKey(id)} |
| 55 | + onRemove={handleRemove} |
| 56 | + /> |
| 57 | + ) |
| 58 | + |
| 59 | + return ( |
| 60 | + <FormGroup className='my-4'> |
| 61 | + <div className='col-md-6'> |
| 62 | + <h5 className='mb-2'>Server-side Environment Keys</h5> |
| 63 | + <p className='fs-small lh-sm mb-0'> |
| 64 | + Flags can be evaluated locally within your own Server environments |
| 65 | + using our{' '} |
| 66 | + <Button |
| 67 | + theme='text' |
| 68 | + href='https://docs.flagsmith.com/clients/overview#server-side-sdks' |
| 69 | + target='_blank' |
| 70 | + > |
| 71 | + Server-side Environment Keys |
| 72 | + </Button> |
| 73 | + . |
| 74 | + </p> |
| 75 | + <p className='fs-small lh-sm mb-0'> |
| 76 | + Server-side SDKs should be initialised with a Server-side Environment |
| 77 | + Key. |
| 78 | + </p> |
| 79 | + {isAdmin ? ( |
| 80 | + <Button onClick={handleCreate} className='my-4'> |
| 81 | + Create Server-side Environment Key |
| 82 | + </Button> |
| 83 | + ) : ( |
| 84 | + <Tooltip |
| 85 | + title={ |
| 86 | + <Button className='my-4' disabled> |
| 87 | + Create Server-side Environment Key |
| 88 | + </Button> |
| 89 | + } |
| 90 | + place='right' |
| 91 | + > |
| 92 | + {Constants.environmentPermissions(EnvironmentPermission.ADMIN)} |
| 93 | + </Tooltip> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + {isLoading && ( |
| 97 | + <div className='text-center'> |
| 98 | + <Loader /> |
| 99 | + </div> |
| 100 | + )} |
| 101 | + {keys && !!keys.length && ( |
| 102 | + <PanelSearch |
| 103 | + id='server-side-keys-list' |
| 104 | + title='Server-side Environment Keys' |
| 105 | + className='no-pad' |
| 106 | + items={keys} |
| 107 | + filterRow={filterByName} |
| 108 | + renderRow={renderKeyRow} |
| 109 | + /> |
| 110 | + )} |
| 111 | + </FormGroup> |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +export default ServerSideSDKKeys |
0 commit comments