Repository navigation
Conversation
83ca3f4 to
41f4f13
Compare
41f4f13 to
8d1c712
Compare
|
@robin-lecomte after a quick chat with @lterm this morning, i'll implement the error messages in toasts instead, unless you think this is a bad idea?
|
This commit gives feedback to the user when they request (or have already requested) access to an item. Add a `requestAccessState` prop `"not-requested" | "sent" | "already-requested"` to `FilePreview` and `NoAccessPreview`. As soon as the state changes from "not-requested", the button is disabled.
8d1c712 to
874f2b1
Compare
There was a problem hiding this comment.
Thanks for the PR! The feature is useful, but I'm requesting changes before merging: the current API causes several problems as soon as an app wires it in the natural way (try → "sent", catch → "error"). I reproduced each case locally with stories.
1. The error is passed as a state, but it is an event
The error toast is fired by a useEffect watching requestAccessState (NoAccessPreview.tsx L45-52). Consequences:
- A 2nd failure shows nothing. The state is already
"error", React does not re-render, so no new toast. The user thinks the request went through. - Double submit. The button stays clickable while the request is pending, so a double-click on a slow network sends two requests.
- The toast comes back by itself. After a failure, moving to another file and back to the folder remounts the component, and the toast shows again with no new request.
Proposal: onRequestAccess returns a Promise. The ui-kit locks the button while it is pending and shows the toast in the catch, on every failure. "error" is removed from the states.
// FilePreview.tsx
onRequestAccess?: (file: FilePreviewType) => Promise<void>;
// NoAccessPreview.tsx
const [isSending, setIsSending] = useState(false);
const state = file.requestAccessState ?? "not-requested";
const isLocked =
isSending || state === "sent" || state === "already-requested";
const handleRequestAccess = async () => {
if (isLocked || !onRequestAccess) return;
setIsSending(true);
try {
await onRequestAccess(file);
} catch {
toast(
t("components.filePreview.folderAccessDenied.requestError"),
VariantType.ERROR,
);
} finally {
setIsSending(false);
}
};2. One single state for the whole carousel
requestAccessState is a FilePreview prop, while access denial is per file (isFolderAccessDenied in types.ts). With two denied folders in files, if I request access to the first one and move to the second, the second shows "Request sent" with a disabled button: I can't request access, even though no request was made.
Proposal: move the status onto the file, next to isFolderAccessDenied, and remove the FilePreview prop. We also need a key per file. Otherwise React keeps the same NoAccessPreview instance from one denied folder to the next, and the local isSending state carries over.
// types.ts
export type RequestAccessState = "not-requested" | "sent" | "already-requested";
export interface FilePreviewType {
// ...
isFolderAccessDenied?: boolean;
/** Access request status, only used when `isFolderAccessDenied` is true. */
requestAccessState?: RequestAccessState;
}// FilePreview.tsx, renderViewer
<NoAccessPreview key={currentFile.id} file={currentFile} /* ... */ />On the app side, the wiring becomes:
const handleRequestAccess = async (file: FilePreviewType) => {
await api.requestAccess(file.id); // if it rejects, the ui-kit shows the toast
setFiles((prev) =>
prev.map((f) =>
f.id === file.id ? { ...f, requestAccessState: "sent" } : f,
),
);
};3. RequestAccessState is not exported by the package
src/index.ts lists its type exports one by one, so the export * in preview/index.ts is not enough. An app writing import { type RequestAccessState } from "@gouvfr-lasuite/ui-components" gets TS2305, and will end up copying the union.
// src/index.ts
export type {
FilePreviewType,
PreviewControlsProps,
RequestAccessState,
VolumeBarProps,
} from "./components/preview";4. The JSDoc contradicts the behaviour
The onRequestAccess JSDoc says "The consuming app handles the access request and its feedback", but the ui-kit already shows the error toast. An app that follows the JSDoc shows its own toast, and the user sees two toasts for a single error. With the proposal from point 1, the ui-kit owns the toast, and the JSDoc should say so:
/**
* Sends the access request for `file`. Return a promise: while it is pending
* the button is locked, and if it rejects the preview shows an error toast.
* Do not show your own error toast. On success, set the file's
* `requestAccessState` to "sent".
*/
onRequestAccess?: (file: FilePreviewType) => Promise<void>;To update accordingly
- add stories and e2e tests
- Stories: there is currently no error case. A story with an
onRequestAccessthat rejects would help. - E2E tests: two failures in a row show two toasts, a double-click calls
onRequestAccessonly once, and navigating between two denied folders keeps independent statuses.
Optional — button layout (@robin-lecomte , what do you think?)
Depending on the language, the buttons can wrap onto two lines. Robin, is that fine for you, or would you rather keep them on a single line?
This is following @PanchoutNathan's review. `onRequestAccess` now returns `Promise<void>`. `NoAccessPreview` owns the async lifecycle and locks the button while the request is in flight to prevent double-submits. The error toast is shown in its own catch block on every failure: one toast per failure. This fixes three bugs with the old "error" state approach: - a second failure showed no toast (state didn't change, no re-render) - double-clicking sent two requests - remounting an errored component re-fired the stale toast
This is following @PanchoutNathan's review. Each `FilePreviewType` now carries its own optional `requestAccessState`: `"sent" | "already-requested"`. `"not-requested"` was removed as it's redundant with no `requestAccessState` (undefined). The FilePreview-level prop is removed. This fixes the carousel bug: with a single prop shared across all files, requesting access to one denied folder disabled the button on every other denied folder in the same preview. `key={currentFile.id}` is added to `NoAccessPreview` so local `isSending` state resets on navigation rather than carrying over between folders.
This is following @PanchoutNathan's review. Apps importing it from `@gouvfr-lasuite/ui-components` got TS2305 because `src/index.ts` lists type exports one by one and the type was missing from the list.
This is following @PanchoutNathan's review - JSDoc on `onRequestAccess` now states the ui-kit owns the error toast and instructs callers to set `requestAccessState: "sent"` on success - `FilePreviewType` table in the story meta documents `requestAccessState` - `FilePreviewProps` table and argTypes reflect the `Promise<void>` signature
6a6e1f6 to
d1762b6
Compare

Fixes #377
Purpose
This gives feedback to the user when they request (or have already requested) access to an item.
Add a
requestAccessStateprop"not-requested" | "sent" | "already-requested"toFilePreviewandNoAccessPreview.As soon as the state changes from "not-requested", the button is disabled.
It might make sense to add a
error <error message>variant to therequestAccessStatetype, to give feedback on error to the user. As a reminder, toasts are not an option as they're displayed behind the backdrop, and as such as unreadable.If there's such a error, the button might be displayed as disabled a error button.
Edit: Robin proposed the following