2026-10-07 17:24:38 -04:00
import React , { useState } from 'react' ;
2026-10-05 18:10:18 -04:00
import { AlertCircle , FileArchive , RefreshCw , Settings2 , Trash2 , UploadCloud } from 'lucide-react' ;
2026-10-07 17:24:38 -04:00
import { getExtensionStates , useExtensionRegistry } from '@/extensions/registry' ;
2026-10-05 18:10:18 -04:00
import { cn } from '@/lib/cn' ;
2026-11-23 16:25:46 +00:00
import { formatDate } from '@/lib/dates' ;
2026-10-05 18:10:18 -04:00
import { httpErrorToHuman } from '@/api/http' ;
import {
disableAdminExtensionInput ,
enableAdminExtensionInput ,
2026-10-06 14:01:38 -04:00
extensionReplacement ,
2026-10-05 18:10:18 -04:00
installAdminExtensionInput ,
removeAdminExtensionInput ,
updateAdminExtensionSettingsInput ,
type AdminExtension ,
2026-10-06 14:01:38 -04:00
type AdminExtensionReplacement ,
2026-10-05 18:10:18 -04:00
type AdminExtensionSettingField ,
type AdminExtensionSettingValue ,
useAdminExtensions ,
useAdminExtensionSettings ,
useDisableAdminExtension ,
useEnableAdminExtension ,
useInstallAdminExtension ,
useRemoveAdminExtension ,
uploadAdminExtensionSettingFileInput ,
clearAdminExtensionSettingFileInput ,
useClearAdminExtensionSettingFile ,
useUploadAdminExtensionSettingFile ,
useUpdateAdminExtensionSettings ,
} from '@/api/admin/extensions/queries' ;
import SettingField , { submittedItems , type SettingFileActions } from './SettingField' ;
import SettingsSections , { settingsBodyClass } from './SettingsSections' ;
import AdminContentBlock from '@/components/admin/AdminContentBlock' ;
import Button from '@/components/elements/Button' ;
import Icon from '@/components/elements/Icon' ;
import Label from '@/components/elements/Label' ;
2026-10-07 10:46:54 +01:00
import NamedIcon from '@/components/elements/NamedIcon' ;
2026-10-05 18:10:18 -04:00
import ListToolbar from '@/components/elements/ListToolbar' ;
import { NewButton } from '@/components/elements/NewButton' ;
import Spinner from '@/components/elements/Spinner' ;
import SpinnerOverlay from '@/components/elements/SpinnerOverlay' ;
import { Alert } from '@/components/elements/alert' ;
import { Dialog } from '@/components/elements/dialog' ;
import { ServerError } from '@/components/elements/ScreenBlock' ;
import Tooltip from '@/components/elements/tooltip/Tooltip' ;
import Checkbox from '@/components/ui/Checkbox' ;
import Switch from '@/components/ui/Switch' ;
import { FileInput } from '@/components/form/controls' ;
import { Empty , EmptyContent , EmptyDescription , EmptyHeader , EmptyMedia , EmptyTitle } from '@/components/ui/empty' ;
2026-11-23 14:16:00 +00:00
import Translate from '@/components/elements/Translate' ;
import { useTranslation , type Translation } from '@/sdk/localization' ;
2026-10-05 18:10:18 -04:00
type ExtensionState = NonNullable < AdminExtension [ 'state' ] >;
type ActionButtonProps = {
label : string ;
icon : React.ComponentProps < typeof Icon >[ 'icon' ];
disabled? : boolean ;
color? : React.ComponentProps < typeof Button >[ 'color' ];
isLoading? : boolean ;
onClick : () => void ;
};
2026-10-11 03:10:18 -04:00
// A failure in any other phase happened while the extension was running, not while it loaded.
const LOAD_PHASES = new Set ([ 'register' , 'boot' , 'compatibility' ]);
2026-11-23 14:16:00 +00:00
const stateBadge = ( t : Translation [ 't' ], state : ExtensionState = '' , errorPhase? : string | null ) => {
2026-10-05 18:10:18 -04:00
switch ( state ) {
case 'not_registered' :
2026-11-23 14:16:00 +00:00
return { label : t ( 'Not set up' ), className : 'bg-accent/10 text-accent' };
2026-10-05 18:10:18 -04:00
case 'error' :
2026-10-11 03:10:18 -04:00
return {
2026-11-23 14:16:00 +00:00
label : ! errorPhase || LOAD_PHASES . has ( errorPhase ) ? t ( 'Failed to load' ) : t ( 'Runtime error' ),
2026-10-11 03:10:18 -04:00
className : 'bg-destructive/15 text-destructive' ,
};
2026-10-05 18:10:18 -04:00
case 'invalid' :
2026-11-23 14:16:00 +00:00
return { label : t ( 'Invalid package' ), className : 'bg-destructive/15 text-destructive' };
2026-10-05 18:10:18 -04:00
default :
return null ;
}
};
2026-11-23 14:16:00 +00:00
const extensionName = ( extension : AdminExtension , t : Translation [ 't' ]) : string =>
extension . name || extension . id || t ( 'Unknown extension' );
2026-10-05 18:10:18 -04:00
2026-11-23 14:16:00 +00:00
const extensionId = ( extension : AdminExtension , t : Translation [ 't' ]) : string =>
extension . id || extensionName ( extension , t );
2026-10-05 18:10:18 -04:00
const canRemove = ( extension : AdminExtension ) : boolean => extension . state !== 'invalid' && !! extension . id ;
const canEnable = ( extension : AdminExtension ) : boolean =>
extension . state !== 'invalid' && ! extension . enabled && !! extension . id ;
const canDisable = ( extension : AdminExtension ) : boolean => extension . enabled === true && !! extension . id ;
2026-11-23 14:16:00 +00:00
const extensionInitials = ( extension : AdminExtension , t : Translation [ 't' ]) : string =>
extensionName ( extension , t )
2026-10-05 18:10:18 -04:00
. split ( /[\s_-]+/ )
. filter ( Boolean )
. slice ( 0 , 2 )
. map (( part ) => part [ 0 ] ? . toUpperCase () ?? '' )
. join ( '' ) || 'EX' ;
2026-10-07 10:46:54 +01:00
function ExtensionMarkContent ({ extension } : { extension : AdminExtension }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-07 10:46:54 +01:00
const [ failedUrl , setFailedUrl ] = useState < string | null >( null );
const iconUrl = extension . icon_url ;
if ( iconUrl && iconUrl !== failedUrl ) {
return (
< img
src = { iconUrl }
2026-10-08 16:53:20 -04:00
alt = ''
loading = 'lazy'
decoding = 'async'
className = 'h-full w-full object-contain'
2026-10-07 10:46:54 +01:00
onError = {() => setFailedUrl ( iconUrl )}
/>
);
}
if ( extension . icon ) {
return < NamedIcon name = { extension . icon } size = { 20 } />;
}
2026-11-23 14:16:00 +00:00
return <>{ extensionInitials ( extension , t )}</>;
2026-10-07 10:46:54 +01:00
}
export function ExtensionMark ({ extension } : { extension : AdminExtension }) {
2026-10-05 18:10:18 -04:00
return (
< div
2026-10-08 16:53:20 -04:00
className = 'flex h-10 w-10 shrink-0 items-center justify-center overflow-hidden rounded-sm bg-secondary text-sm font-semibold text-secondary-foreground'
aria-hidden = 'true'
2026-10-05 18:10:18 -04:00
>
2026-10-07 10:46:54 +01:00
< ExtensionMarkContent extension = { extension } />
2026-10-05 18:10:18 -04:00
</ div >
);
}
function ActionButton ({ label , icon , disabled , color = 'grey' , isLoading , onClick } : ActionButtonProps ) {
return (
< NewButton
icon = { icon }
color = { color }
isSecondary
disabled = { disabled }
isLoading = { isLoading }
2026-10-08 16:53:20 -04:00
className = 'h-8 px-2.5 text-sm'
2026-10-05 18:10:18 -04:00
onClick = { onClick }
>
{ label }
</ NewButton >
);
}
2026-10-06 14:01:38 -04:00
function ReplacementNotice ({ replacement } : { replacement : AdminExtensionReplacement }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
const components = { code : < code className = 'font-mono' /> };
2026-10-06 14:01:38 -04:00
return (
2026-11-23 14:16:00 +00:00
< Alert type = 'warning' title = { t ( 'Replace an installed extension?' )}>
{ replacement . installedVersion ? (
< Translate
i18nKey = 'This package is <code>{{id}}</code> v{{version}}, and v{{installedVersion}} of <code>{{id}}</code> is already installed. Installing it replaces the installed files.'
values = {{
id : replacement.id ,
version : replacement.version ,
installedVersion : replacement.installedVersion ,
}}
components = { components }
/>
) : (
< Translate
i18nKey = 'This package is <code>{{id}}</code> v{{version}}, and a copy of <code>{{id}}</code> is already installed. Installing it replaces the installed files.'
values = {{ id : replacement.id , version : replacement.version }}
components = { components }
/>
)}
{ replacement . enabled && (
<>
{ ' ' }
{ t (
'The extension is enabled and stays enabled, so the new version runs its migrations and code right away.'
)}
</>
)}
2026-10-06 14:01:38 -04:00
</ Alert >
);
}
2026-10-05 18:10:18 -04:00
function InstallExtensionDialog ({ open , onClose } : { open : boolean ; onClose : () => void }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
const [ file , setFile ] = useState < File | null >( null );
const [ enable , setEnable ] = useState ( false );
2026-10-06 14:01:38 -04:00
const [ replacement , setReplacement ] = useState < AdminExtensionReplacement | null >( null );
2026-10-05 18:10:18 -04:00
const installExtension = useInstallAdminExtension ();
const submitting = installExtension . isPending ;
const submit = async () => {
if ( ! file ) {
return ;
}
try {
2026-10-06 14:01:38 -04:00
await installExtension . mutateAsync ( installAdminExtensionInput ( file , enable , replacement !== null ));
2026-10-05 18:10:18 -04:00
onClose ();
2026-10-06 14:01:38 -04:00
} catch ( error ) {
// A package with an installed id waits for the admin to confirm; other errors are toasted by the mutation.
const conflict = extensionReplacement ( error );
2026-10-08 16:53:20 -04:00
2026-10-06 14:01:38 -04:00
if ( conflict ) {
setReplacement ( conflict );
}
2026-10-05 18:10:18 -04:00
}
};
return (
< Dialog
open = { open }
2026-11-23 14:16:00 +00:00
title = { t ( 'Install extension' )}
2026-10-05 18:10:18 -04:00
preventExternalClose = { submitting }
hideCloseIcon = { submitting }
onClose = { onClose }
>
< SpinnerOverlay visible = { submitting } />
2026-10-08 16:53:20 -04:00
< div className = 'space-y-5' >
2026-10-05 18:10:18 -04:00
< div >
2026-11-23 14:16:00 +00:00
< Label htmlFor = 'extension_package' >{ t ( 'Package' )}</ Label >
2026-10-05 18:10:18 -04:00
< FileInput
2026-10-08 16:53:20 -04:00
id = 'extension_package'
accept = '.pteroext,.zip,application/zip'
2026-11-23 14:16:00 +00:00
aria-label = { t ( 'Extension package' )}
2026-10-05 18:10:18 -04:00
disabled = { submitting }
2026-10-06 14:01:38 -04:00
onChange = {( event ) => {
setFile ( event . currentTarget . files ? .[ 0 ] ?? null );
setReplacement ( null );
}}
2026-10-05 18:10:18 -04:00
/>
</ div >
2026-10-08 16:53:20 -04:00
< label className = 'flex items-center gap-3 text-sm text-foreground' >
2026-10-05 18:10:18 -04:00
< Checkbox checked = { enable } onChange = { setEnable } disabled = { submitting } />
2026-11-23 14:16:00 +00:00
{ t ( 'Enable after install' )}
2026-10-05 18:10:18 -04:00
</ label >
2026-10-06 14:01:38 -04:00
{ replacement && < ReplacementNotice replacement = { replacement } />}
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 16:53:20 -04:00
< div className = 'flex flex-wrap justify-end mt-6' >
2026-10-05 18:10:18 -04:00
< Button
2026-10-08 16:53:20 -04:00
type = 'button'
2026-10-05 18:10:18 -04:00
isSecondary
2026-10-08 16:53:20 -04:00
className = 'w-full sm:w-auto sm:mr-2'
2026-10-05 18:10:18 -04:00
disabled = { submitting }
onClick = { onClose }
>
2026-11-23 14:16:00 +00:00
{ t ( 'Cancel' )}
2026-10-05 18:10:18 -04:00
</ Button >
< Button
2026-10-08 16:53:20 -04:00
type = 'button'
2026-10-06 14:01:38 -04:00
color = { replacement ? 'red' : 'primary' }
2026-10-08 16:53:20 -04:00
className = 'w-full mt-4 sm:w-auto sm:mt-0'
2026-10-05 18:10:18 -04:00
disabled = { ! file || submitting }
isLoading = { submitting }
onClick = {() => void submit ()}
>
2026-11-23 14:16:00 +00:00
{ replacement ? t ( 'Replace {{id}}' , { id : replacement.id }) : t ( 'Install' )}
2026-10-05 18:10:18 -04:00
</ Button >
</ div >
</ Dialog >
);
}
function InstallExtensionButton() {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
< Dialog.Trigger
trigger = {({ onClick }) => (
< NewButton icon = { UploadCloud } onClick = { onClick }>
2026-11-23 14:16:00 +00:00
{ t ( 'Install' )}
2026-10-05 18:10:18 -04:00
</ NewButton >
)}
>
{({ open , onClose }) => < InstallExtensionDialog open = { open } onClose = { onClose } />}
</ Dialog.Trigger >
);
}
function ExtensionToolbar ({
failedCount ,
isFetching ,
onRefresh ,
} : {
failedCount : number ;
isFetching : boolean ;
onRefresh : () => void ;
}) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
< ListToolbar
summary = {
failedCount > 0 && (
2026-10-08 16:53:20 -04:00
< span className = 'inline-flex items-center gap-1.5 text-warning' >
2026-10-05 18:10:18 -04:00
< Icon icon = { AlertCircle } />
2026-11-23 14:16:00 +00:00
{ t ( '{{count}} extensions need attention' , { count : failedCount })}
2026-10-05 18:10:18 -04:00
</ span >
)
}
>
2026-10-08 16:53:20 -04:00
< div className = 'flex items-center gap-2' >
2026-11-23 14:16:00 +00:00
< Tooltip content = { t ( 'Refresh' )}>
2026-10-08 16:53:20 -04:00
< span className = 'inline-flex' >
2026-10-05 18:10:18 -04:00
< Button
2026-10-08 16:53:20 -04:00
type = 'button'
color = 'grey'
2026-10-05 18:10:18 -04:00
isSecondary
2026-11-23 14:16:00 +00:00
aria-label = { t ( 'Refresh extensions' )}
2026-10-05 18:10:18 -04:00
disabled = { isFetching }
isLoading = { isFetching }
2026-10-08 16:53:20 -04:00
className = 'inline-flex h-9 w-9 items-center justify-center p-0'
2026-10-05 18:10:18 -04:00
onClick = { onRefresh }
>
< Icon icon = { RefreshCw } />
</ Button >
</ span >
</ Tooltip >
< InstallExtensionButton />
</ div >
</ ListToolbar >
);
}
const resolveSettingValue = (
values : Record < string , AdminExtensionSettingValue | undefined >,
field : AdminExtensionSettingField
) : AdminExtensionSettingValue | undefined => {
if ( field . input in values ) {
return values [ field . input ];
}
// Secrets arrive masked; blank means "keep the stored value".
return field . field === 'password' ? '' : field . value ;
};
const shouldIncludeSettingValue = (
field : AdminExtensionSettingField ,
value : AdminExtensionSettingValue | undefined
) : value is AdminExtensionSettingValue => {
// Files are saved through their own upload endpoint.
if ( value === undefined || field . field === 'file' ) {
return false ;
}
return ! ( field . field === 'password' && value === '' );
};
const buildSettingsPayload = (
schema : AdminExtensionSettingField [],
getValue : ( field : AdminExtensionSettingField ) => AdminExtensionSettingValue | undefined
) => {
const settings : Record < string , AdminExtensionSettingValue > = {};
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
for ( const field of schema ) {
const value = getValue ( field );
2026-10-08 16:53:20 -04:00
2026-10-05 18:10:18 -04:00
if ( shouldIncludeSettingValue ( field , value )) {
settings [ field . input ] = field . field === 'list' ? submittedItems ( value ) : value ;
}
}
return settings ;
};
function SettingsEmptyState ({ extensionEnabled } : { extensionEnabled : boolean | undefined }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
2026-10-12 09:04:03 -04:00
< Empty size = 'compact' >
2026-10-05 18:10:18 -04:00
< EmptyHeader >
2026-10-08 16:53:20 -04:00
< EmptyMedia variant = 'icon' >
2026-10-05 18:10:18 -04:00
< Settings2 />
</ EmptyMedia >
2026-11-23 14:16:00 +00:00
< EmptyTitle >{ extensionEnabled ? t ( 'No settings' ) : t ( 'Settings unavailable' )}</ EmptyTitle >
2026-10-05 18:10:18 -04:00
< EmptyDescription >
{ extensionEnabled
2026-11-23 14:16:00 +00:00
? t ( "This extension doesn't register any settings." )
: t ( 'Enable the extension to load its settings.' )}
2026-10-05 18:10:18 -04:00
</ EmptyDescription >
</ EmptyHeader >
</ Empty >
);
}
function SettingsDialogContent ({
error ,
showLoading ,
registered ,
schema ,
extensionEnabled ,
getValue ,
onChange ,
fileActions ,
} : {
error : unknown ;
showLoading : boolean ;
registered : boolean ;
schema : AdminExtensionSettingField [];
extensionEnabled : boolean | undefined ;
getValue : ( field : AdminExtensionSettingField ) => AdminExtensionSettingValue | undefined ;
onChange : ( field : AdminExtensionSettingField ) => ( value : AdminExtensionSettingValue ) => void ;
fileActions : ( field : AdminExtensionSettingField ) => SettingFileActions ;
}) {
if ( error ) {
return (
< div className = { settingsBodyClass }>
2026-10-08 16:53:20 -04:00
< Alert type = 'danger' >
2026-10-05 18:10:18 -04:00
< span >{ httpErrorToHuman ( error )}</ span >
</ Alert >
</ div >
);
}
if ( showLoading ) {
return (
< div className = { cn ( settingsBodyClass , 'justify-center' )}>
2026-10-08 16:53:20 -04:00
< Spinner size = 'large' centered />
2026-10-05 18:10:18 -04:00
</ div >
);
}
if ( ! registered || schema . length === 0 ) {
return (
< div className = { cn ( settingsBodyClass , 'justify-center' )}>
< SettingsEmptyState extensionEnabled = { extensionEnabled } />
</ div >
);
}
return (
< SettingsSections
schema = { schema }
renderField = {( field ) => (
< SettingField
key = { field . input }
field = { field }
value = { getValue ( field )}
onChange = { onChange ( field )}
file = { field . field === 'file' ? fileActions ( field ) : undefined }
/>
)}
/>
);
}
function SettingsDialogActions ({
submitting ,
canSave ,
onCancel ,
onSubmit ,
} : {
submitting : boolean ;
canSave : boolean ;
onCancel : () => void ;
onSubmit : () => void ;
}) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
2026-10-08 16:53:20 -04:00
< div className = 'flex flex-wrap justify-end mt-6' >
2026-10-05 18:10:18 -04:00
< Button
2026-10-08 16:53:20 -04:00
type = 'button'
2026-10-05 18:10:18 -04:00
isSecondary
2026-10-08 16:53:20 -04:00
className = 'w-full sm:w-auto sm:mr-2'
2026-10-05 18:10:18 -04:00
disabled = { submitting }
onClick = { onCancel }
>
2026-11-23 14:16:00 +00:00
{ t ( 'Cancel' )}
2026-10-05 18:10:18 -04:00
</ Button >
< Button
2026-10-08 16:53:20 -04:00
type = 'button'
className = 'w-full mt-4 sm:w-auto sm:mt-0'
2026-10-05 18:10:18 -04:00
disabled = { submitting || ! canSave }
isLoading = { submitting }
onClick = { onSubmit }
>
2026-11-23 14:16:00 +00:00
{ t ( 'Save Settings' )}
2026-10-05 18:10:18 -04:00
</ Button >
</ div >
);
}
function SettingsDialog ({
extension ,
open ,
onClose ,
} : {
extension : AdminExtension ;
open : boolean ;
onClose : () => void ;
}) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
const id = extension . id ?? '' ;
const { data , error , isFetching } = useAdminExtensionSettings ( id , { enabled : open && id . length > 0 });
const updateSettings = useUpdateAdminExtensionSettings ();
const uploadFile = useUploadAdminExtensionSettingFile ();
const clearFile = useClearAdminExtensionSettingFile ();
const [ values , setValues ] = useState < Record < string , AdminExtensionSettingValue | undefined >>({});
const submitting = updateSettings . isPending ;
const registered = data ? . data ? . registered ?? false ;
const schema = data ? . data . schema ?? [];
const showLoading = isFetching && ! data ;
const getValue = ( field : AdminExtensionSettingField ) : AdminExtensionSettingValue | undefined =>
resolveSettingValue ( values , field );
const handleChange = ( field : AdminExtensionSettingField ) => ( value : AdminExtensionSettingValue ) =>
setValues (( current ) => ({ ... current , [ field . input ] : value }));
const fileActions = ( field : AdminExtensionSettingField ) : SettingFileActions => ({
pending : uploadFile.isPending || clearFile . isPending ,
onUpload : ( file ) => uploadFile . mutate ( uploadAdminExtensionSettingFileInput ( id , field . input , file )),
onClear : () => clearFile . mutate ( clearAdminExtensionSettingFileInput ( id , field . input )),
});
const submit = async () => {
const settings = buildSettingsPayload ( schema , getValue );
try {
await updateSettings . mutateAsync ( updateAdminExtensionSettingsInput ( id , settings ));
onClose ();
} catch {
// Error toast is handled by the mutation.
}
};
return (
< Dialog
open = { open }
2026-11-23 14:16:00 +00:00
title = { t ( '{{name}} settings' , { name : extensionName ( extension , t ) })}
2026-10-05 18:10:18 -04:00
preventExternalClose = { submitting }
hideCloseIcon = { submitting }
onClose = { onClose }
>
< SpinnerOverlay visible = { submitting } />
< SettingsDialogContent
error = { error }
showLoading = { showLoading }
registered = { registered }
schema = { schema }
extensionEnabled = { extension . enabled }
getValue = { getValue }
onChange = { handleChange }
fileActions = { fileActions }
/>
< SettingsDialogActions
submitting = { submitting }
canSave = { registered && schema . length > 0 }
onCancel = { onClose }
onSubmit = { submit }
/>
</ Dialog >
);
}
2026-10-08 18:49:15 -04:00
type ExtensionMutationState = {
isPending : boolean ;
variables ?: { path : { extension? : string } };
};
2026-10-05 18:10:18 -04:00
2026-10-08 18:49:15 -04:00
const isPendingFor = ( mutation : ExtensionMutationState , extension : AdminExtension ) : boolean =>
mutation . isPending && mutation . variables ? . path . extension === extension . id ;
2026-10-05 18:10:18 -04:00
2026-11-23 14:16:00 +00:00
const extensionSubtitle = ( extension : AdminExtension , t : Translation [ 't' ]) : string =>
[
extension . version ? `v ${ extension . version } ` : null ,
extension . author && t ( 'by {{author}}' , { author : extension.author }),
]
2026-10-08 18:49:15 -04:00
. filter ( Boolean )
. join ( ' · ' );
2026-10-05 18:10:18 -04:00
2026-10-08 18:49:15 -04:00
function ExtensionCardHeader ({ extension , name , id } : { extension : AdminExtension ; name : string ; id : string }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
const subtitle = extensionSubtitle ( extension , t );
const badge = stateBadge ( t , extension . state as ExtensionState | undefined , extension . error_phase );
2026-10-05 18:10:18 -04:00
return (
2026-10-08 18:49:15 -04:00
< div className = 'flex min-w-0 items-start gap-3' >
< ExtensionMark extension = { extension } />
< div className = 'min-w-0 flex-1' >
< div className = 'flex flex-wrap items-center gap-2' >
< h2 className = 'truncate text-base font-semibold text-foreground' title = { ` ${ name } ( ${ id } )` }>
{ name }
</ h2 >
{ badge && (
< span className = { cn ( 'rounded-sm px-1.5 py-0.5 text-xs font-medium' , badge . className )}>
{ badge . label }
</ span >
)}
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 18:49:15 -04:00
< p className = 'mt-0.5 truncate text-xs text-muted-foreground' >
{ subtitle && <>{ subtitle } · </>}
< span className = 'font-mono' >{ id }</ span >
</ p >
2026-10-05 18:10:18 -04:00
</ div >
2026-10-08 18:49:15 -04:00
</ div >
);
}
2026-10-05 18:10:18 -04:00
2026-10-08 18:49:15 -04:00
function ExtensionCardDetails ({ extension } : { extension : AdminExtension }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-08 18:49:15 -04:00
return (
<>
2026-10-05 18:10:18 -04:00
< p
2026-10-08 16:53:20 -04:00
className = 'mt-3 line-clamp-3 text-sm leading-relaxed text-card-foreground/80'
2026-10-05 18:10:18 -04:00
title = { extension . description || undefined }
>
2026-11-23 14:16:00 +00:00
{ extension . description || t ( 'No description provided.' )}
2026-10-05 18:10:18 -04:00
</ p >
2026-10-08 18:49:15 -04:00
{ extension . state === 'not_registered' && (
2026-10-08 16:53:20 -04:00
< p className = 'mt-3 text-sm text-muted-foreground' >
2026-11-23 14:16:00 +00:00
{ t ( 'Found in the extensions folder. Turn it on to finish setting it up.' )}
2026-10-05 18:10:18 -04:00
</ p >
)}
{ extension . error && (
2026-10-08 16:53:20 -04:00
< div className = 'mt-3 rounded-sm border border-destructive/30 bg-destructive/10 p-3' >
< p className = 'break-words text-sm text-destructive' >{ extension . error }</ p >
2026-10-11 03:10:18 -04:00
{ extension . errored_at && (
< p className = 'mt-1 text-xs text-destructive/80' >
2026-11-23 14:16:00 +00:00
{ extension . error_phase
? t ( 'In {{phase}}, {{date}}' , {
phase : extension.error_phase ,
2026-11-23 16:25:46 +00:00
date : formatDate ( extension . errored_at , 'll LTS' ),
2026-11-23 14:16:00 +00:00
})
2026-11-23 16:25:46 +00:00
: formatDate ( extension . errored_at , 'll LTS' )}
2026-10-11 03:10:18 -04:00
</ p >
)}
2026-10-05 18:10:18 -04:00
</ div >
)}
2026-10-08 18:49:15 -04:00
</>
);
}
function ExtensionToggle ({ extension , name } : { extension : AdminExtension ; name : string }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-08 18:49:15 -04:00
const enableExtension = useEnableAdminExtension ();
const disableExtension = useDisableAdminExtension ();
const pending = isPendingFor ( enableExtension , extension ) || isPendingFor ( disableExtension , extension );
const allowed = extension . enabled ? canDisable ( extension ) : canEnable ( extension );
const onChange = ( checked : boolean ) => {
if ( ! extension . id ) {
return ;
}
if ( checked ) {
enableExtension . mutate ( enableAdminExtensionInput ( extension . id ));
} else {
disableExtension . mutate ( disableAdminExtensionInput ( extension . id ));
}
};
return (
< Switch
2026-11-23 14:16:00 +00:00
aria-label = { t ( 'Enable {{name}}' , { name })}
2026-10-08 18:49:15 -04:00
className = 'mr-auto'
checked = { extension . enabled === true }
disabled = { pending || ! allowed }
onChange = { onChange }
/>
);
}
function RemoveExtensionButton ({ extension , name } : { extension : AdminExtension ; name : string }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-08 18:49:15 -04:00
const removeExtension = useRemoveAdminExtension ();
const removePending = isPendingFor ( removeExtension , extension );
const submitRemove = ( close : () => void ) => {
if ( ! extension . id ) {
return ;
}
removeExtension . mutate ( removeAdminExtensionInput ( extension . id , name ), {
onSuccess : close ,
});
};
return (
< Dialog.ConfirmTrigger
2026-11-23 14:16:00 +00:00
title = { t ( 'Remove extension' )}
confirm = { t ( 'Remove' )}
2026-10-08 18:49:15 -04:00
preventExternalClose = { removePending }
hideCloseIcon = { removePending }
pending = { removePending }
trigger = {({ onClick }) => (
< ActionButton
2026-11-23 14:16:00 +00:00
label = { t ( 'Remove' )}
2026-10-08 18:49:15 -04:00
icon = { Trash2 }
color = 'red'
disabled = { ! canRemove ( extension ) || removePending }
isLoading = { removePending }
onClick = { onClick }
/>
)}
onConfirmed = {( _event , close ) => submitRemove ( close )}
>
< SpinnerOverlay visible = { removePending } />
2026-11-23 14:16:00 +00:00
< Translate
i18nKey = 'Removing <bold>{{name}}</bold> deletes the extension files, published assets, settings, subuser permissions it added, and install record.'
values = {{ name }}
components = {{ bold : < strong /> }}
/>
2026-10-08 18:49:15 -04:00
</ Dialog.ConfirmTrigger >
);
}
function ExtensionCard ({ extension } : { extension : AdminExtension }) {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
const id = extensionId ( extension , t );
const name = extensionName ( extension , t );
2026-10-08 18:49:15 -04:00
return (
< article className = 'flex h-full flex-col rounded-sm border border-border bg-card p-4' >
< ExtensionCardHeader extension = { extension } name = { name } id = { id } />
< ExtensionCardDetails extension = { extension } />
2026-10-05 18:10:18 -04:00
2026-10-08 16:53:20 -04:00
< div className = 'mt-auto pt-4' >
< div className = 'flex flex-wrap items-center gap-2 border-t border-border pt-4' >
2026-10-08 18:49:15 -04:00
< ExtensionToggle extension = { extension } name = { name } />
2026-10-05 18:10:18 -04:00
{ extension . enabled && (
< Dialog.Trigger
trigger = {({ onClick }) => (
2026-11-23 14:16:00 +00:00
< ActionButton label = { t ( 'Settings' )} icon = { Settings2 } onClick = { onClick } />
2026-10-05 18:10:18 -04:00
)}
>
{({ open , onClose }) => (
< SettingsDialog extension = { extension } open = { open } onClose = { onClose } />
)}
</ Dialog.Trigger >
)}
2026-10-08 18:49:15 -04:00
< RemoveExtensionButton extension = { extension } name = { name } />
2026-10-05 18:10:18 -04:00
</ div >
</ div >
</ article >
);
}
function EmptyExtensionsState() {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-05 18:10:18 -04:00
return (
2026-10-08 16:53:20 -04:00
< Empty className = 'border bg-card' >
2026-10-05 18:10:18 -04:00
< EmptyHeader >
2026-10-08 16:53:20 -04:00
< EmptyMedia variant = 'icon' >
2026-10-05 18:10:18 -04:00
< FileArchive />
</ EmptyMedia >
2026-11-23 14:16:00 +00:00
< EmptyTitle >{ t ( 'No extensions installed' )}</ EmptyTitle >
2026-10-05 18:10:18 -04:00
< EmptyDescription >
2026-11-23 14:16:00 +00:00
{ t ( 'Install an extension package to add features and integrations to this panel.' )}
2026-10-05 18:10:18 -04:00
</ EmptyDescription >
</ EmptyHeader >
< EmptyContent >
< InstallExtensionButton />
</ EmptyContent >
</ Empty >
);
}
2026-10-08 16:53:20 -04:00
function ExtensionGrid ({ extensions } : { extensions : AdminExtension [] }) {
if ( extensions . length === 0 ) {
return < EmptyExtensionsState />;
}
return (
< div className = 'grid grid-cols-1 gap-4 lg:grid-cols-2 xl:grid-cols-3' >
{ extensions . map (( extension ) => (
< ExtensionCard key = { extension . id ?? extension . name } extension = { extension } />
))}
</ div >
);
}
2026-10-05 18:10:18 -04:00
export default function ExtensionsContainer() {
2026-11-23 14:16:00 +00:00
const { t } = useTranslation ();
2026-10-07 17:24:38 -04:00
const runtimeStates = useExtensionRegistry ( getExtensionStates );
2026-10-05 18:10:18 -04:00
const runtimeFailures = runtimeStates . filter (( state ) => state . status === 'failed' );
const { data , error , isFetching , refetch } = useAdminExtensions ();
const extensions = data ? . data ?? [];
const failedCount = extensions . filter (( extension ) => [ 'error' , 'invalid' ]. includes ( extension . state ?? '' )). length ;
if ( error ) {
return < ServerError message = { httpErrorToHuman ( error )} onRetry = {() => refetch ()} />;
}
return (
< AdminContentBlock
2026-11-23 14:16:00 +00:00
title = { t ( 'Admin · Extensions' )}
heading = { t ( 'Extensions' )}
description = { t ( 'Manage extensions that add features and integrations to your panel.' )}
2026-10-05 18:10:18 -04:00
>
{ runtimeFailures . length > 0 && (
< Alert type = 'danger' className = 'mb-4' >
{ runtimeFailures . map (( state ) => (
< p key = { state . id }>
{ state . id } : { state . error }
</ p >
))}
</ Alert >
)}
{ data ? . meta ? . enabled === false && (
2026-10-08 16:53:20 -04:00
< Alert type = 'warning' className = 'mb-4' >
2026-11-23 14:16:00 +00:00
< Translate
i18nKey = "Extensions are turned off on this panel, so installed extensions won't load. Set <code>{{variable}}</code> in your <code>{{file}}</code> to turn them on."
values = {{ variable : 'PTERODACTYL_EXTENSIONS_ENABLED=true' , file : '.env' }}
components = {{ code : < code className = 'font-mono' /> }}
/>
2026-10-05 18:10:18 -04:00
</ Alert >
)}
{ data && < ExtensionToolbar failedCount = { failedCount } isFetching = { isFetching } onRefresh = {() => refetch ()} />}
2026-10-08 16:53:20 -04:00
{ data ? < ExtensionGrid extensions = { extensions } /> : < Spinner size = 'large' centered />}
2026-10-05 18:10:18 -04:00
</ AdminContentBlock >
);
}