Files
CoHaiSe-CLOUD/frontend/src/components/confirm.tsx
T
Gitea ActionsandEsiah a82711c9b1
CD — Deployment / deploy (push) Successful in 16s
refactor(ui): switch icons to WebP and standardize icon-only actions (#22)
- Replace PNG icon assets with WebP in public/icons
- Route every icon through icons.tsx and add missing exports
  (Clear, Save, OpenedFolder, Deactivate, PasswordReset, TrashEmpty, Filter)
- Replace text buttons with icon-only actions backed by tooltips
- Unify confirm/delete/primary dialog buttons on the accent red
- Use liquid glass hover on chat messages

---------

Co-authored-by: xEsiah <[email protected]>
Reviewed-on: #22
Reviewed-by: Esiah <[email protected]>
Co-authored-by: Gitea Actions <-[email protected]>
2026-11-04 10:44:43 +00:00

95 lines
2.9 KiB
TypeScript

import {
createContext,
useCallback,
useContext,
useState,
type ReactNode,
} from "react";
import { useI18n } from "../lib/i18n.js";
import { Modal } from "./feedback.js";
import { IconAction } from "./IconAction.js";
import { CloseIcon, TrashIcon, MoveIcon } from "./icons.js";
export interface ConfirmOptions {
/** Titre de la modale. */
title?: string;
/** Corps du message ; accepte du balisage pour mettre un nom en valeur. */
message: ReactNode;
confirmLabel?: string;
cancelLabel?: string;
/** `danger` reserve l'action a une operation destructive. */
tone?: "default" | "danger";
/** Icone du bouton de confirmation (defaut selon le tone). */
confirmIcon?: ReactNode;
}
type ConfirmFn = (options: ConfirmOptions) => Promise<boolean>;
interface PendingConfirm {
options: ConfirmOptions;
resolve: (value: boolean) => void;
}
const ConfirmContext = createContext<ConfirmFn | null>(null);
/**
* Remplace `window.confirm` par une modale maison aux couleurs de l'app.
* `useConfirm()` renvoie une promesse resolue a `true` (confirme) ou `false`.
*/
export const ConfirmProvider = ({ children }: { children: ReactNode }) => {
const { t } = useI18n();
const [pending, setPending] = useState<PendingConfirm | null>(null);
const confirm = useCallback<ConfirmFn>(
(options) => new Promise<boolean>((resolve) => setPending({ options, resolve })),
[],
);
const answer = (value: boolean) => {
if (pending) pending.resolve(value);
setPending(null);
};
const tone = pending?.options.tone ?? "default";
return (
<ConfirmContext.Provider value={confirm}>
{children}
<Modal
open={pending !== null}
title={pending?.options.title ?? t("Confirmation")}
onClose={() => answer(false)}
>
<div className="flex flex-col gap-5">
<p className="text-sm leading-relaxed text-muted-foreground">
{pending?.options.message}
</p>
<div className="grid grid-cols-2 gap-2">
<IconAction
label={pending?.options.cancelLabel ?? t("Annuler")}
icon={<CloseIcon className="h-4 w-4" />}
className="w-full"
onClick={() => answer(false)}
/>
<IconAction
label={pending?.options.confirmLabel ?? t("Confirmer")}
icon={
pending?.options.confirmIcon ??
(tone === "danger" ? <TrashIcon className="h-4 w-4" /> : <MoveIcon className="h-4 w-4" />)
}
className="w-full bg-accent text-accent-foreground hover:bg-accent"
onClick={() => answer(true)}
/>
</div>
</div>
</Modal>
</ConfirmContext.Provider>
);
};
export const useConfirm = (): ConfirmFn => {
const context = useContext(ConfirmContext);
if (!context) throw new Error("useConfirm doit etre utilise sous ConfirmProvider");
return context;
};