|
| 1 | +import Page from 'components/Page'; |
| 2 | +import SectionTitleContainer from 'elements/SectionTitleContainer'; |
| 3 | +import { useApi } from 'hooks/useApi'; |
| 4 | +import globalize from 'lib/globalize'; |
| 5 | +import React, { useCallback, useEffect, useRef, useState } from 'react'; |
| 6 | +import { getApiKeyApi } from '@jellyfin/sdk/lib/utils/api/api-key-api'; |
| 7 | +import { AuthenticationInfo } from '@jellyfin/sdk/lib/generated-client'; |
| 8 | +import Loading from 'components/loading/LoadingComponent'; |
| 9 | +import { Api } from '@jellyfin/sdk'; |
| 10 | +import confirm from 'components/confirm/confirm'; |
| 11 | +import ApiKeyCell from 'components/dashboard/apikeys/ApiKeyCell'; |
| 12 | + |
| 13 | +const ApiKeys = () => { |
| 14 | + const { api } = useApi(); |
| 15 | + const [ keys, setKeys ] = useState<AuthenticationInfo[]>([]); |
| 16 | + const [ loading, setLoading ] = useState(true); |
| 17 | + const element = useRef<HTMLDivElement>(null); |
| 18 | + |
| 19 | + const loadKeys = (currentApi: Api) => { |
| 20 | + return getApiKeyApi(currentApi) |
| 21 | + .getKeys() |
| 22 | + .then(({ data }) => { |
| 23 | + if (data.Items) { |
| 24 | + setKeys(data.Items); |
| 25 | + } |
| 26 | + }) |
| 27 | + .catch((err) => { |
| 28 | + console.error('[apikeys] failed to load api keys', err); |
| 29 | + }); |
| 30 | + }; |
| 31 | + |
| 32 | + const revokeKey = useCallback((accessToken: string) => { |
| 33 | + if (api) { |
| 34 | + confirm(globalize.translate('MessageConfirmRevokeApiKey'), globalize.translate('HeaderConfirmRevokeApiKey')).then(function () { |
| 35 | + setLoading(true); |
| 36 | + getApiKeyApi(api) |
| 37 | + .revokeKey({ key: accessToken }) |
| 38 | + .then(() => loadKeys(api)) |
| 39 | + .then(() => setLoading(false)) |
| 40 | + .catch(err => { |
| 41 | + console.error('[apikeys] failed to revoke key', err); |
| 42 | + }); |
| 43 | + }).catch(err => { |
| 44 | + console.error('[apikeys] failed to show confirmation dialog', err); |
| 45 | + }); |
| 46 | + } |
| 47 | + }, [api]); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (!api) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + loadKeys(api).then(() => { |
| 55 | + setLoading(false); |
| 56 | + }).catch(err => { |
| 57 | + console.error('[apikeys] failed to load api keys', err); |
| 58 | + }); |
| 59 | + |
| 60 | + if (loading) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const page = element.current; |
| 65 | + |
| 66 | + if (!page) { |
| 67 | + console.error('[apikeys] Unexpected null page reference'); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const showNewKeyPopup = () => { |
| 72 | + import('../../../components/prompt/prompt').then(({ default: prompt }) => { |
| 73 | + prompt({ |
| 74 | + title: globalize.translate('HeaderNewApiKey'), |
| 75 | + label: globalize.translate('LabelAppName'), |
| 76 | + description: globalize.translate('LabelAppNameExample') |
| 77 | + }).then((value) => { |
| 78 | + getApiKeyApi(api) |
| 79 | + .createKey({ app: value }) |
| 80 | + .then(() => loadKeys(api)) |
| 81 | + .catch(err => { |
| 82 | + console.error('[apikeys] failed to create api key', err); |
| 83 | + }); |
| 84 | + }).catch(() => { |
| 85 | + // popup closed |
| 86 | + }); |
| 87 | + }).catch(err => { |
| 88 | + console.error('[apikeys] failed to load api key popup', err); |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + (page.querySelector('.btnNewKey') as HTMLButtonElement).addEventListener('click', showNewKeyPopup); |
| 93 | + |
| 94 | + return () => { |
| 95 | + (page.querySelector('.btnNewKey') as HTMLButtonElement).removeEventListener('click', showNewKeyPopup); |
| 96 | + }; |
| 97 | + }, [api, loading]); |
| 98 | + |
| 99 | + if (loading) { |
| 100 | + return <Loading />; |
| 101 | + } |
| 102 | + |
| 103 | + return ( |
| 104 | + <Page |
| 105 | + id='apiKeysPage' |
| 106 | + title={globalize.translate('HeaderApiKeys')} |
| 107 | + className='mainAnimatedPage type-interior' |
| 108 | + > |
| 109 | + <div ref={element} className='content-primary'> |
| 110 | + <SectionTitleContainer |
| 111 | + title={globalize.translate('HeaderApiKeys')} |
| 112 | + isBtnVisible={true} |
| 113 | + btnId='btnAddSchedule' |
| 114 | + btnClassName='fab submit sectionTitleButton btnNewKey' |
| 115 | + btnTitle={globalize.translate('Add')} |
| 116 | + btnIcon='add' |
| 117 | + /> |
| 118 | + <p>{globalize.translate('HeaderApiKeysHelp')}</p> |
| 119 | + <br /> |
| 120 | + <table className='tblApiKeys detailTable'> |
| 121 | + <caption className='clipForScreenReader'>{globalize.translate('ApiKeysCaption')}</caption> |
| 122 | + <thead> |
| 123 | + <tr> |
| 124 | + <th scope='col' className='detailTableHeaderCell'></th> |
| 125 | + <th scope='col' className='detailTableHeaderCell'>{globalize.translate('HeaderApiKey')}</th> |
| 126 | + <th scope='col' className='detailTableHeaderCell'>{globalize.translate('HeaderApp')}</th> |
| 127 | + <th scope='col' className='detailTableHeaderCell'>{globalize.translate('HeaderDateIssued')}</th> |
| 128 | + </tr> |
| 129 | + </thead> |
| 130 | + <tbody className='resultBody'> |
| 131 | + {keys.map(key => { |
| 132 | + return <ApiKeyCell key={key.AccessToken} apiKey={key} revokeKey={revokeKey} />; |
| 133 | + })} |
| 134 | + </tbody> |
| 135 | + </table> |
| 136 | + </div> |
| 137 | + </Page> |
| 138 | + ); |
| 139 | +}; |
| 140 | + |
| 141 | +export default ApiKeys; |
0 commit comments