mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-14 10:34:06 +02:00
# Description of Changes <img width="1569" height="980" alt="image" src="https://github.com/user-attachments/assets/dca1c227-ed84-4393-97a1-e3ce6eb1620b" /> <img width="1596" height="935" alt="image" src="https://github.com/user-attachments/assets/2003e1be-034a-4cbb-869e-6d5d912ab61d" /> <img width="1543" height="997" alt="image" src="https://github.com/user-attachments/assets/fe0c4f4b-eeee-4db4-a041-e554f350255a" /> --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing) for more details.
90 lines
2.3 KiB
TypeScript
90 lines
2.3 KiB
TypeScript
import { useAppConfig } from '@app/contexts/AppConfigContext';
|
|
import { alert } from '@app/components/toast';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
/**
|
|
* Hook to manage login-required functionality in admin sections
|
|
* Provides login state, validation, and alert functionality
|
|
*/
|
|
export function useLoginRequired() {
|
|
const { config } = useAppConfig();
|
|
const { t } = useTranslation();
|
|
const loginEnabled = config?.enableLogin ?? true;
|
|
|
|
/**
|
|
* Show alert when user tries to modify settings with login disabled
|
|
*/
|
|
const showLoginRequiredAlert = () => {
|
|
alert({
|
|
alertType: 'warning',
|
|
title: t('admin.error', 'Error'),
|
|
body: t('admin.settings.loginRequired', 'Login mode must be enabled to modify admin settings'),
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Validate that login is enabled before allowing action
|
|
* Returns true if login is enabled, false otherwise (and shows alert)
|
|
*/
|
|
const validateLoginEnabled = (): boolean => {
|
|
if (!loginEnabled) {
|
|
showLoginRequiredAlert();
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Wrap an async handler to check login state before executing
|
|
*/
|
|
const withLoginCheck = <T extends (...args: any[]) => Promise<any>>(
|
|
handler: T
|
|
): T => {
|
|
return (async (...args: any[]) => {
|
|
if (!validateLoginEnabled()) {
|
|
return;
|
|
}
|
|
return handler(...args);
|
|
}) as T;
|
|
};
|
|
|
|
/**
|
|
* Get styles for disabled inputs (cursor not-allowed)
|
|
*/
|
|
const getDisabledStyles = () => {
|
|
if (!loginEnabled) {
|
|
return {
|
|
input: { cursor: 'not-allowed' },
|
|
track: { cursor: 'not-allowed' },
|
|
thumb: { cursor: 'not-allowed' }
|
|
};
|
|
}
|
|
return undefined;
|
|
};
|
|
|
|
/**
|
|
* Wrap fetch function to skip API call when login disabled
|
|
*/
|
|
const withLoginCheckForFetch = <T extends (...args: any[]) => Promise<any>>(
|
|
fetchHandler: T,
|
|
skipWhenDisabled: boolean = true
|
|
): T => {
|
|
return (async (...args: any[]) => {
|
|
if (!loginEnabled && skipWhenDisabled) {
|
|
// Skip fetch when login disabled - component will use default/empty values
|
|
return;
|
|
}
|
|
return fetchHandler(...args);
|
|
}) as T;
|
|
};
|
|
|
|
return {
|
|
loginEnabled,
|
|
showLoginRequiredAlert,
|
|
validateLoginEnabled,
|
|
withLoginCheck,
|
|
withLoginCheckForFetch,
|
|
getDisabledStyles,
|
|
};
|
|
}
|