mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-09-18 17:39:24 +00:00

# Description of Changes <!-- Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --> --- ## 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) ### 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. --------- Co-authored-by: ConnorYoh <40631091+ConnorYoh@users.noreply.github.com> Co-authored-by: Anthony Stirling <77850077+Frooodle@users.noreply.github.com>
47 lines
1.7 KiB
TypeScript
47 lines
1.7 KiB
TypeScript
import { useEffect, useMemo } from 'react';
|
|
import { useRightRail } from '../contexts/RightRailContext';
|
|
import { RightRailAction, RightRailButtonConfig } from '../types/rightRail';
|
|
|
|
export interface RightRailButtonWithAction extends RightRailButtonConfig {
|
|
onClick: RightRailAction;
|
|
}
|
|
|
|
/**
|
|
* Registers one or more RightRail buttons and their actions.
|
|
* - Automatically registers on mount and unregisters on unmount
|
|
* - Updates registration when the input array reference changes
|
|
*/
|
|
export function useRightRailButtons(buttons: readonly RightRailButtonWithAction[]) {
|
|
const { registerButtons, unregisterButtons, setAction } = useRightRail();
|
|
|
|
// Memoize configs and ids to reduce churn
|
|
const configs: RightRailButtonConfig[] = useMemo(
|
|
() => buttons.map(({ onClick, ...cfg }) => cfg),
|
|
[buttons]
|
|
);
|
|
const ids: string[] = useMemo(() => buttons.map(b => b.id), [buttons]);
|
|
|
|
useEffect(() => {
|
|
if (!buttons || buttons.length === 0) return;
|
|
|
|
// DEV warnings for duplicate ids or missing handlers
|
|
if (process.env.NODE_ENV === 'development') {
|
|
const idSet = new Set<string>();
|
|
buttons.forEach(b => {
|
|
if (!b.onClick) console.warn('[RightRail] Missing onClick for id:', b.id);
|
|
if (idSet.has(b.id)) console.warn('[RightRail] Duplicate id in buttons array:', b.id);
|
|
idSet.add(b.id);
|
|
});
|
|
}
|
|
|
|
// Register visual button configs (idempotent merge by id)
|
|
registerButtons(configs);
|
|
|
|
// Bind/update actions independent of registration
|
|
buttons.forEach(({ id, onClick }) => setAction(id, onClick));
|
|
|
|
// Cleanup unregisters by ids present in this call
|
|
return () => unregisterButtons(ids);
|
|
}, [registerButtons, unregisterButtons, setAction, configs, ids, buttons]);
|
|
}
|