The minimalistic document archiving platform.
import type { ReactNode } from 'react';
import { createContext, useContext, useState } from 'react';
import { useAppTranslations } from '@/modules/i18n/hooks/use-app-translations';
import { AlertDialog } from '@/modules/ui/components/alert-dialog';
type AlertButton = {
text: string;
onPress?: () => void;
style?: 'default' | 'cancel' | 'destructive';
};
type AlertOptions = {
title: string;
message?: string | ReactNode;
buttons?: AlertButton[];
};
type AlertContextType = {
showAlert: (options: AlertOptions) => void;
};
const AlertContext = createContext<AlertContextType | null>(null);
export function AlertProvider({ children }: { children: ReactNode }) {
const t = useAppTranslations();
const [alertState, setAlertState] = useState<AlertOptions & { visible: boolean }>({
visible: false,
title: '',
message: '',
buttons: [],
});
const showAlert = (options: AlertOptions) => {
setAlertState({
visible: true,
title: options.title,
message: options.message,
buttons: options.buttons ?? [{ text: t.common.ok, style: 'default' }],
});
};
const handleDismiss = () => {
setAlertState((prev) => ({ ...prev, visible: false }));
};
return (
<AlertContext.Provider value={{ showAlert }}>
{children}
<AlertDialog
visible={alertState.visible}
title={alertState.title}
message={alertState.message}
buttons={alertState.buttons ?? []}
onDismiss={handleDismiss}
/>
</AlertContext.Provider>
);
}
export function useAlert() {
const context = useContext(AlertContext);
if (!context) {
throw new Error('useAlert must be used within an AlertProvider');
}
return context;
}