mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-08-27 14:49:23 +00:00
54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
![]() |
import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
|
||
|
import { RightRailAction, RightRailButtonConfig } from '../types/rightRail';
|
||
|
|
||
|
interface RightRailContextValue {
|
||
|
buttons: RightRailButtonConfig[];
|
||
|
actions: Record<string, RightRailAction>;
|
||
|
registerButtons: (buttons: RightRailButtonConfig[]) => void;
|
||
|
unregisterButtons: (ids: string[]) => void;
|
||
|
setAction: (id: string, action: RightRailAction) => void;
|
||
|
clear: () => void;
|
||
|
}
|
||
|
|
||
|
const RightRailContext = createContext<RightRailContextValue | undefined>(undefined);
|
||
|
|
||
|
export function RightRailProvider({ children }: { children: React.ReactNode }) {
|
||
|
const [buttons, setButtons] = useState<RightRailButtonConfig[]>([]);
|
||
|
const [actions, setActions] = useState<Record<string, RightRailAction>>({});
|
||
|
|
||
|
const registerButtons = useCallback((newButtons: RightRailButtonConfig[]) => {
|
||
|
setButtons(prev => {
|
||
|
const merged = [...prev.filter(b => !newButtons.some(nb => nb.id === b.id)), ...newButtons];
|
||
|
return merged.sort((a, b) => (a.order || 0) - (b.order || 0));
|
||
|
});
|
||
|
}, []);
|
||
|
|
||
|
const unregisterButtons = useCallback((ids: string[]) => {
|
||
|
setButtons(prev => prev.filter(b => !ids.includes(b.id)));
|
||
|
setActions(prev => Object.fromEntries(Object.entries(prev).filter(([id]) => !ids.includes(id))));
|
||
|
}, []);
|
||
|
|
||
|
const setAction = useCallback((id: string, action: RightRailAction) => {
|
||
|
setActions(prev => ({ ...prev, [id]: action }));
|
||
|
}, []);
|
||
|
|
||
|
const clear = useCallback(() => {
|
||
|
setButtons([]);
|
||
|
setActions({});
|
||
|
}, []);
|
||
|
|
||
|
const value = useMemo<RightRailContextValue>(() => ({ buttons, actions, registerButtons, unregisterButtons, setAction, clear }), [buttons, actions, registerButtons, unregisterButtons, setAction, clear]);
|
||
|
|
||
|
return (
|
||
|
<RightRailContext.Provider value={value}>
|
||
|
{children}
|
||
|
</RightRailContext.Provider>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function useRightRail() {
|
||
|
const ctx = useContext(RightRailContext);
|
||
|
if (!ctx) throw new Error('useRightRail must be used within RightRailProvider');
|
||
|
return ctx;
|
||
|
}
|