-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update admin site - Implement 'autocomplete' for more fields - Improves admin loading time * Add "admin" buttons to the PUI interface * Only allow superuser access
- Loading branch information
1 parent
acb1ec4
commit dc741b6
Showing
19 changed files
with
169 additions
and
26 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
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
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,88 @@ | ||
import { t } from '@lingui/macro'; | ||
import { IconUserStar } from '@tabler/icons-react'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
import { ModelType } from '../../enums/ModelType'; | ||
import { navigateToLink } from '../../functions/navigation'; | ||
import { base_url } from '../../main'; | ||
import { useLocalState } from '../../states/LocalState'; | ||
import { useUserState } from '../../states/UserState'; | ||
import { ModelInformationDict } from '../render/ModelType'; | ||
import { ActionButton } from './ActionButton'; | ||
|
||
export type AdminButtonProps = { | ||
model: ModelType; | ||
pk: number | undefined; | ||
}; | ||
|
||
/* | ||
* A button that is used to navigate to the admin page for the selected item. | ||
* | ||
* This button is only rendered if: | ||
* - The admin interface is enabled for the server | ||
* - The selected model has an associated admin URL | ||
* - The user has "superuser" role | ||
* - The user has at least read rights for the selected item | ||
*/ | ||
export default function AdminButton(props: AdminButtonProps) { | ||
const user = useUserState(); | ||
|
||
const enabled: boolean = useMemo(() => { | ||
// Only users with superuser permission will see this button | ||
if (!user || !user.isLoggedIn() || !user.isSuperuser()) { | ||
return false; | ||
} | ||
|
||
// TODO: Check if the server has the admin interface enabled | ||
|
||
const modelDef = ModelInformationDict[props.model]; | ||
|
||
// No admin URL associated with the model | ||
if (!modelDef.admin_url) { | ||
return false; | ||
} | ||
|
||
// No primary key provided | ||
if (!props.pk) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}, [user, props.model, props.pk]); | ||
|
||
const openAdmin = useCallback( | ||
(event: any) => { | ||
const modelDef = ModelInformationDict[props.model]; | ||
const host = useLocalState.getState().host; | ||
|
||
if (!modelDef.admin_url) { | ||
return; | ||
} | ||
|
||
// TODO: Check the actual "admin" URL (it may be custom) | ||
const url = `${host}/admin${modelDef.admin_url}${props.pk}/`; | ||
|
||
if (event?.ctrlKey || event?.shiftKey) { | ||
// Open the link in a new tab | ||
window.open(url, '_blank'); | ||
} else { | ||
window.open(url, '_self'); | ||
} | ||
}, | ||
[props.model, props.pk] | ||
); | ||
|
||
return ( | ||
<ActionButton | ||
icon={<IconUserStar />} | ||
color="blue" | ||
size="lg" | ||
radius="sm" | ||
variant="filled" | ||
tooltip={t`Open in admin interface`} | ||
hidden={!enabled} | ||
onClick={openAdmin} | ||
/> | ||
); | ||
} |
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
Oops, something went wrong.