Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

6.6 KB
Raw Blame
import React, { useEffect } from 'react';
import { AlertTriangle, HelpCircle, Paintbrush, Trash2, X } from 'lucide-react';
import { uiStoreActions, useUiStore } from '../stores';
import { isImeComposing, useI18n } from '../i18n';

export const ConfirmDialogModal: React.FC = React.memo(() => {
  const { t, td } = useI18n();
  const dialog = useUiStore((s) => s.appDialog);

  useEffect(() => {
    if (!dialog.isOpen) return;
    const handleKeyDown = (e: KeyboardEvent) => {
      if (isImeComposing(e)) return;
      if (e.key === 'Escape') {
        e.preventDefault();
        e.stopPropagation();
        uiStoreActions.resolveAppDialog(false);
      }
    };
    window.addEventListener('keydown', handleKeyDown, true);
    return () => window.removeEventListener('keydown', handleKeyDown, true);
  }, [dialog.isOpen]);

  if (!dialog.isOpen) return null;

  const isDanger = dialog.variant === 'danger';
  const isAlert = dialog.variant === 'alert';

  const handleCancel = () => {
    uiStoreActions.resolveAppDialog(false);
  };

  const handleConfirm = () => {
    uiStoreActions.resolveAppDialog(true);
  };

  return (
    <div
      role={isAlert ? 'alertdialog' : 'dialog'}
      aria-modal="true"
      aria-labelledby="app-confirm-dialog-title"
      data-testid="app-confirm-dialog"
      className="fixed inset-0 z-[70] flex items-center justify-center bg-ui-overlay p-4 select-none"
      onClick={handleCancel}
    >
      <div
        onClick={(e) => e.stopPropagation()}
        className="bg-ui-panel rounded-ui-lg shadow-ui-popover w-full max-w-[420px] overflow-hidden text-xs text-ui-primary border border-ui-default"
      >
        {/* Title Bar */}
        <div className="h-10 px-4 flex items-center justify-between bg-ui-header border-b border-ui-subtle">
          <div className="flex items-center gap-2 min-w-0">
            {isDanger ? (
              <Trash2 className="w-3.5 h-3.5 text-ui-danger shrink-0" />
            ) : isAlert ? (
              <AlertTriangle className="w-3.5 h-3.5 text-ui-warning shrink-0" />
            ) : (
              <HelpCircle className="w-3.5 h-3.5 text-ui-accent shrink-0" />
            )}
            <span
              id="app-confirm-dialog-title"
              className="text-xs font-medium text-ui-primary tracking-wide truncate"
            >
              {td(dialog.title)}
            </span>
          </div>
          <button
            type="button"
            onClick={handleCancel}
            aria-label={t('Close')}
            title={t('Cancel (Esc)')}
            className="w-6 h-6 rounded-ui-sm flex items-center justify-center text-ui-muted hover:text-ui-primary hover:bg-ui-control-hover transition-colors cursor-pointer shrink-0"
          >
            <X className="w-3.5 h-3.5" />
          </button>
        </div>

        {/* Body */}
        <div className="p-4 space-y-3">
          {dialog.preview && (
            <div className="bg-ui-header border border-ui-subtle rounded-ui-md p-2.5 flex items-center gap-3">
              <div className="w-10 h-10 bg-ui-panel border border-ui-subtle overflow-hidden shrink-0 flex items-center justify-center rounded-ui-sm">
                {dialog.preview.imageUrl ? (
                  <img
                    src={dialog.preview.imageUrl}
                    alt={dialog.preview.title}
                    className="w-7 h-7 object-contain brightness-125"
                  />
                ) : (
                  <Paintbrush className="w-4 h-4 text-ui-icon-muted" />
                )}
              </div>
              <div className="flex-1 min-w-0">
                <div
                  className="text-xs font-medium text-ui-primary truncate"
                  title={dialog.preview.title}
                >
                  {dialog.preview.title}
                </div>
                {dialog.preview.subtitle && (
                  <div
                    className="text-[10px] text-ui-muted truncate mt-0.5"
                    title={dialog.preview.subtitle}
                  >
                    {dialog.preview.subtitle}
                  </div>
                )}
              </div>
            </div>
          )}

          <div className="flex items-start gap-3">
            {!dialog.preview && (
              <div
                className={`w-8 h-8 rounded-full border flex items-center justify-center shrink-0 mt-0.5 ${
                  isDanger
                    ? 'bg-ui-danger/15 border-ui-danger/35 text-ui-danger'
                    : isAlert
                      ? 'bg-ui-warning/15 border-ui-warning/35 text-ui-warning'
                      : 'bg-ui-control border-ui-default text-ui-icon'
                }`}
              >
                {isDanger ? (
                  <Trash2 className="w-4 h-4" />
                ) : isAlert ? (
                  <AlertTriangle className="w-4 h-4" />
                ) : (
                  <HelpCircle className="w-4 h-4" />
                )}
              </div>
            )}

            <div className="flex-1 min-w-0 space-y-1">
              <p className="text-[12.5px] leading-relaxed text-ui-primary whitespace-pre-line break-words">
                {dialog.message}
              </p>
              {dialog.detail && (
                <p className="text-[11px] leading-relaxed text-ui-muted whitespace-pre-line break-words">
                  {dialog.detail}
                </p>
              )}
            </div>
          </div>
        </div>

        {/* Footer */}
        <div className="px-4 py-2.5 bg-ui-header border-t border-ui-subtle flex items-center justify-end gap-2">
          {!isAlert && (
            <button
              type="button"
              onClick={handleCancel}
              className="px-3.5 py-1.5 rounded-ui-sm bg-ui-control hover:bg-ui-control-hover border border-ui-default text-xs font-medium text-ui-primary transition-colors cursor-pointer"
            >
              {td(dialog.cancelLabel)}
            </button>
          )}
          <button
            type="button"
            autoFocus
            onClick={handleConfirm}
            className={
              isDanger
                ? 'px-3.5 py-1.5 rounded-ui-sm bg-ui-danger/20 hover:bg-ui-danger/30 border border-ui-danger/45 text-xs font-medium text-ui-danger transition-colors cursor-pointer inline-flex items-center gap-1.5'
                : 'px-4 py-1.5 rounded-ui-sm bg-ui-accent text-ui-accent-contrast hover:opacity-90 text-xs font-medium transition-opacity cursor-pointer'
            }
          >
            {isDanger && <Trash2 className="w-3.5 h-3.5" />}
            <span>{td(dialog.confirmLabel)}</span>
          </button>
        </div>
      </div>
    </div>
  );
});

ConfirmDialogModal.displayName = 'ConfirmDialogModal';