-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import Page from 'components/Page'; | ||
import SectionTitleContainer from 'elements/SectionTitleContainer'; | ||
import { useApi } from 'hooks/useApi'; | ||
import globalize from 'lib/globalize'; | ||
import React, { useCallback, useEffect, useRef, useState } from 'react'; | ||
import { getApiKeyApi } from '@jellyfin/sdk/lib/utils/api/api-key-api'; | ||
import type { AuthenticationInfo } from '@jellyfin/sdk/lib/generated-client/models/authentication-info'; | ||
import Loading from 'components/loading/LoadingComponent'; | ||
import { Api } from '@jellyfin/sdk'; | ||
import confirm from 'components/confirm/confirm'; | ||
import ApiKeyCell from 'components/dashboard/apikeys/ApiKeyCell'; | ||
|
||
const ApiKeys = () => { | ||
const { api } = useApi(); | ||
const [ keys, setKeys ] = useState<AuthenticationInfo[]>([]); | ||
const [ loading, setLoading ] = useState(true); | ||
const element = useRef<HTMLDivElement>(null); | ||
|
||
const loadKeys = (currentApi: Api) => { | ||
return getApiKeyApi(currentApi) | ||
.getKeys() | ||
.then(({ data }) => { | ||
if (data.Items) { | ||
setKeys(data.Items); | ||
} | ||
}) | ||
.catch((err) => { | ||
console.error('[apikeys] failed to load api keys', err); | ||
}); | ||
}; | ||
|
||
const revokeKey = useCallback((accessToken: string) => { | ||
if (api) { | ||
confirm(globalize.translate('MessageConfirmRevokeApiKey'), globalize.translate('HeaderConfirmRevokeApiKey')).then(function () { | ||
setLoading(true); | ||
getApiKeyApi(api) | ||
.revokeKey({ key: accessToken }) | ||
.then(() => loadKeys(api)) | ||
.then(() => setLoading(false)) | ||
.catch(err => { | ||
console.error('[apikeys] failed to revoke key', err); | ||
}); | ||
}).catch(err => { | ||
console.error('[apikeys] failed to show confirmation dialog', err); | ||
}); | ||
} | ||
}, [api]); | ||
|
||
useEffect(() => { | ||
if (!api) { | ||
return; | ||
} | ||
|
||
loadKeys(api).then(() => { | ||
setLoading(false); | ||
}).catch(err => { | ||
console.error('[apikeys] failed to load api keys', err); | ||
}); | ||
|
||
if (loading) { | ||
return; | ||
} | ||
|
||
const page = element.current; | ||
|
||
if (!page) { | ||
console.error('[apikeys] Unexpected null page reference'); | ||
return; | ||
} | ||
|
||
const showNewKeyPopup = () => { | ||
import('../../../components/prompt/prompt').then(({ default: prompt }) => { | ||
prompt({ | ||
title: globalize.translate('HeaderNewApiKey'), | ||
label: globalize.translate('LabelAppName'), | ||
description: globalize.translate('LabelAppNameExample') | ||
}).then((value) => { | ||
getApiKeyApi(api) | ||
.createKey({ app: value }) | ||
.then(() => loadKeys(api)) | ||
.catch(err => { | ||
console.error('[apikeys] failed to create api key', err); | ||
}); | ||
}).catch(() => { | ||
// popup closed | ||
}); | ||
}).catch(err => { | ||
console.error('[apikeys] failed to load api key popup', err); | ||
}); | ||
}; | ||
|
||
(page.querySelector('.btnNewKey') as HTMLButtonElement).addEventListener('click', showNewKeyPopup); | ||
|
||
return () => { | ||
(page.querySelector('.btnNewKey') as HTMLButtonElement).removeEventListener('click', showNewKeyPopup); | ||
}; | ||
}, [api, loading]); | ||
|
||
if (loading) { | ||
return <Loading />; | ||
} | ||
|
||
return ( | ||
<Page | ||
id='apiKeysPage' | ||
title={globalize.translate('HeaderApiKeys')} | ||
className='mainAnimatedPage type-interior' | ||
> | ||
<div ref={element} className='content-primary'> | ||
<SectionTitleContainer | ||
title={globalize.translate('HeaderApiKeys')} | ||
isBtnVisible={true} | ||
btnId='btnAddSchedule' | ||
btnClassName='fab submit sectionTitleButton btnNewKey' | ||
btnTitle={globalize.translate('Add')} | ||
btnIcon='add' | ||
/> | ||
<p>{globalize.translate('HeaderApiKeysHelp')}</p> | ||
<br /> | ||
<table className='tblApiKeys detailTable'> | ||
<caption className='clipForScreenReader'>{globalize.translate('ApiKeysCaption')}</caption> | ||
<thead> | ||
<tr> | ||
<th scope='col' className='detailTableHeaderCell'></th> | ||
<th scope='col' className='detailTableHeaderCell'>{globalize.translate('HeaderApiKey')}</th> | ||
<th scope='col' className='detailTableHeaderCell'>{globalize.translate('HeaderApp')}</th> | ||
<th scope='col' className='detailTableHeaderCell'>{globalize.translate('HeaderDateIssued')}</th> | ||
</tr> | ||
</thead> | ||
<tbody className='resultBody'> | ||
{keys.map(key => { | ||
return <ApiKeyCell key={key.AccessToken} apiKey={key} revokeKey={revokeKey} />; | ||
})} | ||
</tbody> | ||
</table> | ||
</div> | ||
</Page> | ||
); | ||
}; | ||
|
||
export default ApiKeys; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { FunctionComponent, useCallback } from 'react'; | ||
import type { AuthenticationInfo } from '@jellyfin/sdk/lib/generated-client/models/authentication-info'; | ||
import ButtonElement from 'elements/ButtonElement'; | ||
import datetime from 'scripts/datetime'; | ||
import globalize from 'lib/globalize'; | ||
|
||
type ApiKeyCellProps = { | ||
apiKey: AuthenticationInfo; | ||
revokeKey?: (accessToken: string) => void; | ||
}; | ||
|
||
const ApiKeyCell: FunctionComponent<ApiKeyCellProps> = ({ apiKey, revokeKey }: ApiKeyCellProps) => { | ||
const getDate = (dateCreated: string | undefined) => { | ||
const date = datetime.parseISO8601Date(dateCreated, true); | ||
return datetime.toLocaleDateString(date) + ' ' + datetime.getDisplayTime(date); | ||
}; | ||
|
||
const onClick = useCallback(() => { | ||
if (apiKey?.AccessToken && revokeKey !== undefined) { | ||
revokeKey(apiKey.AccessToken); | ||
} | ||
}, [apiKey, revokeKey]); | ||
|
||
return ( | ||
<tr className='detailTableBodyRow detailTableBodyRow-shaded apiKey'> | ||
<td className='detailTableBodyCell'> | ||
<ButtonElement | ||
className='raised raised-mini btnRevoke' | ||
title={globalize.translate('ButtonRevoke')} | ||
onClick={onClick} | ||
/> | ||
</td> | ||
<td className='detailTableBodyCell' style={{ verticalAlign: 'middle' }}> | ||
{apiKey.AccessToken} | ||
</td> | ||
<td className='detailTableBodyCell' style={{ verticalAlign: 'middle' }}> | ||
{apiKey.AppName} | ||
</td> | ||
<td className='detailTableBodyCell' style={{ verticalAlign: 'middle' }}> | ||
{getDate(apiKey.DateCreated)} | ||
</td> | ||
</tr> | ||
); | ||
}; | ||
|
||
export default ApiKeyCell; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters