Files
pterodactyl-panel/resources/scripts/api/admin/settings/queries.ts
T
ItzLinaton 60b72365d8 Feat: Custom User Avatars (V2) (#7295)
Added custom user avatar support.

## Extra Details:
- Users can upload custom `PNG, JPEG, SVG, WebP, AVIF`, and `ICO`
avatars.
- Added avatar management to the user account area.
- Added admin controls to update and remove user avatars.
- Custom User avatars appear in:
  - Panel Navbar.
  - Panel Admin Activity Logs.
  - Server Sub user List.
  - Server Activity Logs
  - Personal Account Activity Logs.
  - Server Ownership Details.
  
---

<img width="1329" height="327"
alt="{B32EBD7F-1FCA-4356-966C-0D60A9A23E49}"
src="https://github.com/user-attachments/assets/881fa5b3-9599-4370-975d-83b6bd91a7d1"
/>

---

<img width="997" height="847" alt="adminpage"
src="https://github.com/user-attachments/assets/989acbcc-351c-431b-ab7f-557ba4c7e070"
/>

---

<img width="955" height="781" alt="serverinfo"
src="https://github.com/user-attachments/assets/6556afac-1971-4112-9686-b71137072c5f"
/>

---

<img width="1376" height="919" alt="userarea"
src="https://github.com/user-attachments/assets/d49f0ddb-1f38-4884-b186-8db78bfed4b0"
/>

---

<img width="936" height="232" alt="userslist"
src="https://github.com/user-attachments/assets/0e8a4b4a-719a-4d52-a485-d7577736b43f"
/>

---

<img width="983" height="772" alt="activity"
src="https://github.com/user-attachments/assets/37ef7176-a804-43f7-ae32-ae65e1a2671b"
/>
2026-10-20 15:09:33 -04:00

192 lines
7.3 KiB
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import {
adminGetSettingsOptions,
adminGetSettingsQueryKey,
adminSendTestMailMutation,
adminUpdateAdvancedSettingsMutation,
adminUpdateGeneralSettingsMutation,
adminUpdateMailSettingsMutation,
} from '@/api/generated/@tanstack/react-query.gen';
import type {
AdminGetSettingsResponse,
AdminSendTestMailData,
AdminUpdateAdvancedSettingsData,
AdminUpdateGeneralSettingsData,
AdminUpdateMailSettingsData,
Options,
} from '@/api/generated';
import { setSiteSettingsQueryData } from '@/api/settings/queries';
import { notifyHttpError } from '@/plugins/notifications';
export type AdminSettings = AdminGetSettingsResponse;
type AdminGeneralSettingsBody = AdminUpdateGeneralSettingsData['body'];
type AdminMailSettingsBody = NonNullable<AdminUpdateMailSettingsData['body']>;
type AdminMailSmtpSettingsBody = NonNullable<AdminMailSettingsBody['smtp']>;
type AdminAdvancedSettingsBody = AdminUpdateAdvancedSettingsData['body'];
type UpdateAdminMailSettingsOptions = { successNotification?: boolean };
type AdminSettingsSection = 'Advanced' | 'General' | 'Mail';
export interface GeneralSettingsValues {
name: string;
twoFactorRequired: NonNullable<AdminGeneralSettingsBody['misc']>['required_2fa'];
userAvatarsEnabled: boolean;
locale: string;
}
export interface MailSettingsValues {
host: string;
port: number;
encryption: NonNullable<AdminMailSmtpSettingsBody['encryption']> | '';
username: string;
// Blank keeps the existing password; "!e" clears it.
password: string;
fromAddress: string;
fromName: string;
}
export interface AdvancedSettingsValues {
recaptchaEnabled: boolean;
recaptchaSecretKey: string;
recaptchaWebsiteKey: string;
guzzleTimeout: number;
guzzleConnectTimeout: number;
allocationsEnabled: boolean;
allocationsRangeStart:
| NonNullable<AdminAdvancedSettingsBody['pterodactyl:client_features:allocations:range_start']>
| '';
allocationsRangeEnd:
| NonNullable<AdminAdvancedSettingsBody['pterodactyl:client_features:allocations:range_end']>
| '';
}
const adminMailSettingsBody = (values: MailSettingsValues): AdminMailSettingsBody => ({
smtp: {
host: values.host,
port: values.port,
encryption: values.encryption === '' ? null : values.encryption,
username: values.username,
password: values.password,
from_address: values.fromAddress,
from_name: values.fromName,
},
});
export const updateAdminGeneralSettingsInput = (
values: GeneralSettingsValues
): Options<AdminUpdateGeneralSettingsData> => ({
body: {
branding: { name: values.name },
misc: {
required_2fa: values.twoFactorRequired,
user_avatars_enabled: values.userAvatarsEnabled ? 'true' : 'false',
},
default_locale: values.locale,
},
});
export const updateAdminMailSettingsInput = (values: MailSettingsValues): Options<AdminUpdateMailSettingsData> => ({
body: adminMailSettingsBody(values),
});
export const updateAdminAdvancedSettingsInput = (
values: AdvancedSettingsValues
): Options<AdminUpdateAdvancedSettingsData> => ({
body: {
'recaptcha:enabled': values.recaptchaEnabled ? 'true' : 'false',
'recaptcha:secret_key': values.recaptchaSecretKey === '' ? null : values.recaptchaSecretKey,
'recaptcha:website_key': values.recaptchaWebsiteKey,
'pterodactyl:guzzle:timeout': values.guzzleTimeout,
'pterodactyl:guzzle:connect_timeout': values.guzzleConnectTimeout,
'pterodactyl:client_features:allocations:enabled': values.allocationsEnabled ? 'true' : 'false',
'pterodactyl:client_features:allocations:range_start':
values.allocationsRangeStart === '' ? null : values.allocationsRangeStart,
'pterodactyl:client_features:allocations:range_end':
values.allocationsRangeEnd === '' ? null : values.allocationsRangeEnd,
},
});
export const testAdminMailInput = (): Options<AdminSendTestMailData> => ({});
export const adminSettingsQueryOptions = () => adminGetSettingsOptions();
type AdminSettingsQueryOptions = { enabled?: boolean };
const adminSettingsUpdateToast = (section: AdminSettingsSection) => ({
title: `${section} settings updated`,
description: `${section} settings have been updated.`,
});
export const useAdminSettings = (options?: AdminSettingsQueryOptions) =>
useQuery({ ...adminSettingsQueryOptions(), ...options });
export const useUpdateAdminGeneralSettings = () => {
const queryClient = useQueryClient();
return useMutation({
...adminUpdateGeneralSettingsMutation(),
onSuccess: async (_data, { body }) => {
setSiteSettingsQueryData(queryClient, (settings) => ({
...settings,
name: body.branding?.name ?? settings.name,
locale: body.default_locale,
userAvatarsEnabled: body.misc?.user_avatars_enabled === 'true',
}));
await queryClient.invalidateQueries({ queryKey: adminGetSettingsQueryKey() });
const notification = adminSettingsUpdateToast('General');
toast.success(notification.title, { description: notification.description });
},
onError: (error) => notifyHttpError(error, 'Unable to update general settings'),
});
};
export const useUpdateAdminMailSettings = ({ successNotification = true }: UpdateAdminMailSettingsOptions = {}) => {
const queryClient = useQueryClient();
return useMutation({
...adminUpdateMailSettingsMutation(),
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: adminGetSettingsQueryKey() });
if (successNotification) {
const notification = adminSettingsUpdateToast('Mail');
toast.success(notification.title, { description: notification.description });
}
},
onError: (error) => notifyHttpError(error, 'Unable to update mail settings'),
});
};
export const useSendTestAdminMail = () =>
useMutation({
...adminSendTestMailMutation(),
onSuccess: () =>
toast.success('Mail settings tested', {
description: 'Mail settings have been saved and a test email has been sent to your account.',
}),
onError: (error) => notifyHttpError(error, 'Unable to test mail settings'),
});
export const useUpdateAdminAdvancedSettings = () => {
const queryClient = useQueryClient();
return useMutation({
...adminUpdateAdvancedSettingsMutation(),
onSuccess: async (_data, { body }) => {
setSiteSettingsQueryData(queryClient, (settings) => ({
...settings,
recaptcha: {
enabled: body['recaptcha:enabled'] === 'true',
siteKey: body['recaptcha:website_key'],
},
}));
await queryClient.invalidateQueries({ queryKey: adminGetSettingsQueryKey() });
const notification = adminSettingsUpdateToast('Advanced');
toast.success(notification.title, { description: notification.description });
},
onError: (error) => notifyHttpError(error, 'Unable to update advanced settings'),
});
};