Files

191 lines
8.7 KiB
TypeScript

// AK-P21 (AD-28 interim polish) — global feedback widget. The mini form is a
// real ~/ui Dialog (size XS 420) with a category select (bug/feature/question,
// designer brief P21); the category is prefixed into the message because the
// support-ticket contract intentionally has no category column. Shared by the
// legacy NavBar trigger (mobile visibility restriction removed) and the shadcn
// nav-actions trigger. Submission keeps the established add-feedback intent on
// the tenant root route (rate limit 3/24h lives in Feedback.server).
//
// Instead of the originally sketched screenshot checkbox, the reporter attaches
// an image themselves (decision 2026-07-21): the client never captures the page
// on its own, so no third-party personal data leaves the workspace without the
// reporter consciously picking the file. The upload is fail-closed scanned and
// ClamAV-checked server-side.
import { useFetcher, useLocation, useParams } from "react-router";
import { Fragment, useEffect, useState } from "react";
import { toast } from "sonner";
import { useTranslation } from "react-i18next";
import StarsIconFilled from "~/components/ui/icons/StarsIconFilled";
import { appPath } from "~/utils/app/CanonicalUrlUtils";
import { useAppOrAdminData } from "~/utils/data/useAppOrAdminData";
import { useRootData } from "~/utils/data/useRootData";
import { Button } from "~/ui/components/Button";
import { Dialog } from "~/ui/components/Dialog";
import { Select } from "~/ui/components/Select";
import { TextArea } from "~/ui/components/TextArea";
const FEEDBACK_CATEGORIES = ["bug", "feature", "question"] as const;
type FeedbackCategory = (typeof FEEDBACK_CATEGORIES)[number];
/** Mirrors the server-side contract in Feedback.server. */
const FEEDBACK_ATTACHMENT_ACCEPT = "image/png,image/jpeg,image/webp,image/gif";
const FEEDBACK_ATTACHMENT_MAX_MB = 5;
export default function AddFeedbackButton() {
const [isOpen, setIsOpen] = useState(false);
const { t } = useTranslation();
return (
<Fragment>
<div className="relative inline-flex">
<button
onClick={() => setIsOpen(!isOpen)}
type="button"
className="text-muted-foreground bg-secondary hover:bg-secondary/90 border-border relative inline-flex items-center rounded-full border p-2 font-medium shadow-inner focus:z-10 focus:outline-hidden focus:ring-2 focus:ring-offset-2"
aria-haspopup="dialog"
aria-expanded={isOpen}
aria-label={t("feedback.title")}
>
<span className="sr-only">{t("feedback.title")}</span>
<StarsIconFilled className="h-5 w-5 p-0.5" />
</button>
</div>
<FeedbackFormDialog open={isOpen} onClose={() => setIsOpen(false)} />
</Fragment>
);
}
export function FeedbackFormDialog({ open, onClose }: { open: boolean; onClose: () => void }) {
const { t } = useTranslation();
const params = useParams();
const appOrAdminData = useAppOrAdminData();
const { csrf } = useRootData();
const fetcher = useFetcher<{ success?: string; error?: string }>();
const location = useLocation();
const [category, setCategory] = useState<FeedbackCategory>("bug");
const [attachmentError, setAttachmentError] = useState<string | null>(null);
useEffect(() => {
if (open) {
setCategory("bug");
setAttachmentError(null);
}
}, [open]);
useEffect(() => {
if (fetcher.data?.success) {
toast.success(fetcher.data.success);
onClose();
} else if (fetcher.data?.error) {
toast.error(fetcher.data.error);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fetcher.data]);
const submitting = fetcher.state !== "idle";
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (attachmentError) return;
void fetcher.submit(e.currentTarget, {
method: "post",
encType: "multipart/form-data",
action: params.tenant ? appPath({ tenantSlug: params.tenant, mode: "host-aware" }) : "",
});
};
// Reject oversized or non-image picks before the upload is attempted; the
// server enforces the same limits again.
const onAttachmentChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.currentTarget.files?.[0] ?? null;
if (!file) {
setAttachmentError(null);
return;
}
if (file.size > FEEDBACK_ATTACHMENT_MAX_MB * 1024 * 1024) {
setAttachmentError(t("feedback.attachment.tooLarge", { megabytes: FEEDBACK_ATTACHMENT_MAX_MB }));
return;
}
if (!FEEDBACK_ATTACHMENT_ACCEPT.split(",").includes(file.type)) {
setAttachmentError(t("feedback.attachment.unsupportedType"));
return;
}
setAttachmentError(null);
};
return (
<Dialog open={open} onOpenChange={(nextOpen: boolean) => (nextOpen ? undefined : onClose())}>
<Dialog.Content className="w-[420px] max-w-[90vw] p-4" aria-label={t("feedback.title")} data-ui-id="ui.feedback.dialog">
<fetcher.Form onSubmit={onSubmit} method="post" className="flex w-full flex-col items-start">
<input type="hidden" name="action" value="add-feedback" readOnly hidden />
<input type="hidden" name="csrf" value={csrf ?? ""} readOnly hidden />
<input type="hidden" name="tenantId" value={appOrAdminData.currentTenant?.id} readOnly hidden />
<input type="hidden" name="fromUrl" value={location.pathname} readOnly hidden />
{/* Radix Select is not a native form control — mirror the picked category. */}
<input type="hidden" name="category" value={category} readOnly hidden />
<div className="flex w-full flex-col items-start gap-2">
<span className="text-heading-0 font-heading-0 text-text-neutral-primary">{t("feedback.title")}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t("feedback.description")}</span>
</div>
<div className="flex w-full flex-col items-start gap-4 pt-4">
<Select
className="w-full max-w-none"
showLabel
label={t("feedback.category.label")}
value={category}
onValueChange={(value: string) => setCategory(FEEDBACK_CATEGORIES.includes(value as FeedbackCategory) ? (value as FeedbackCategory) : "bug")}
data-ui-id="ui.feedback.category"
>
{FEEDBACK_CATEGORIES.map((key) => (
<Select.Item key={key} value={key}>
{t(`feedback.category.${key}`)}
</Select.Item>
))}
</Select>
<TextArea className="w-full max-w-none" label={t("feedback.message")} showLabel>
<TextArea.Input name="message" defaultValue="" rows={5} placeholder={t("feedback.placeholder")} required data-ui-id="ui.feedback.message" />
</TextArea>
<div className="flex w-full flex-col items-start gap-1">
<label
className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-primary"
htmlFor="feedback-attachment"
>
{t("feedback.attachment.label")}
</label>
<input
id="feedback-attachment"
type="file"
name="attachment"
accept={FEEDBACK_ATTACHMENT_ACCEPT}
onChange={onAttachmentChange}
data-ui-id="ui.feedback.attachment"
className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary w-full"
/>
{attachmentError ? (
<span className="text-body-extra-small-regular-150 font-body-extra-small-regular-150 text-text-feedback-negative" role="alert">
{attachmentError}
</span>
) : (
<span className="text-body-extra-small-regular-150 font-body-extra-small-regular-150 text-text-neutral-tertiary">
{t("feedback.attachment.hint", { megabytes: FEEDBACK_ATTACHMENT_MAX_MB })}
</span>
)}
</div>
<span className="text-body-extra-small-regular-150 font-body-extra-small-regular-150 text-text-neutral-tertiary">
{t("feedback.rateLimitHint")}
</span>
</div>
<div className="flex w-full items-center justify-end gap-2 pt-4">
<Button type="button" variant="tertiary" disabled={submitting} onClick={onClose}>
{t("shared.cancel")}
</Button>
<Button type="submit" loading={submitting} disabled={Boolean(attachmentError)} data-ui-id="ui.feedback.send">
{t("feedback.send")}
</Button>
</div>
</fetcher.Form>
</Dialog.Content>
</Dialog>
);
}