import { when } from "mobx"; import { getEnv } from "mobx-state-tree"; import { inject, observer } from "mobx-react"; import { type FC, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { IconAnnotationAccepted, IconAnnotationImported, IconAnnotationPrediction, IconAnnotationPropagated, IconAnnotationRejected, IconAnnotationReviewRemoved, IconAnnotationSkipped, IconAnnotationSubmitted, IconCheck, IconDraftCreated, IconSparks, IconHistoryRewind, } from "@humansignal/icons"; import { Tooltip, Userpic } from "@humansignal/ui"; import { Space } from "../../common/Space/Space"; import { cn } from "../../utils/bem"; import { userDisplayName } from "@humansignal/core"; import { humanDateDiff } from "../../utils/utilities"; import { EmptyState } from "../SidePanels/Components/EmptyState"; import "./AnnotationHistory.prefix.css"; type HistoryItemType = | "prediction" | "imported" | "submitted" | "updated" | "skipped" | "accepted" | "rejected" | "fixed_and_accepted" | "draft_created" | "deleted_review" | "propagated_annotation"; const injector = inject(({ store }) => { const as = store.annotationStore; const selected = as?.selected; return { store, annotationStore: as, selected: as?.selected, createdBy: selected?.user ?? { email: selected?.createdBy }, createdDate: selected?.createdDate, history: as?.history, selectedHistory: as?.selectedHistory, }; }); const DraftState: FC<{ annotation: any; inline?: boolean; isSelected?: boolean; }> = observer(({ annotation, inline, isSelected }: { annotation: any; inline?: boolean; isSelected?: boolean }) => { const hasChanges = annotation.history.hasChanges; const store = annotation.list; // @todo weird name const infoIsHidden = store.store.hasInterface("annotations:hide-info"); const hiddenUser = infoIsHidden ? { email: "Me" } : null; const [hasUnsavedChanges, setChanges] = useState(false); // turn it on when changes just made; off when they we saved useEffect(() => { setChanges(true); }, [annotation.history.history.length]); useEffect(() => { setChanges(false); }, [annotation.draftSaved]); if (!hasChanges && !annotation.versions.draft) return null; return (
) : hasUnsavedChanges ? (
) : hasChanges ? (
) : null } inline={inline} comment="" acceptedState="draft_created" selected={isSelected} hideInfo={infoIsHidden} onClick={() => { store.selectHistory(null); annotation.toggleDraft(true); }} /> ); }); const AnnotationHistoryComponent: FC = ({ store, annotationStore, selectedHistory, history, enabled = true, inline = false, showEmptyState = true, sectionHeader, renderEmptyState, }) => { const annotation = annotationStore.selected; const lastItem = history?.length ? history[0] : null; const hasChanges = annotation.history.hasChanges; const infoIsHidden = annotationStore.store.hasInterface("annotations:hide-info"); const currentUser = window.APP_SETTINGS?.user; // if user makes changes at the first time there are no draft yet const isDraftSelected = !annotationStore.selectedHistory && (annotation.draftSelected || (!annotation.versions.draft && hasChanges)); // Determine if the empty state should be shown const hasDraft = annotation?.versions?.draft; const hasHistory = history && history.length > 0; const shouldShowEmptyState = showEmptyState && !hasChanges && !hasDraft && !hasHistory; // Default empty state component const defaultEmptyState = ( } header="View annotation activity" description={<>See a log of user actions for this annotation} /> ); // If we should show empty state, render it if (shouldShowEmptyState) { return (
{sectionHeader && (
{sectionHeader}
)} {renderEmptyState ? renderEmptyState() : defaultEmptyState}
); } return (
{sectionHeader && (
{sectionHeader}
)} {enabled && history.length > 0 && history.map((item: any) => { const { id, user, createdDate } = item; const isLastItem = lastItem?.id === item.id; const isSelected = isLastItem && !selectedHistory ? !isDraftSelected : selectedHistory?.id === item.id; const hiddenUser = infoIsHidden ? { email: currentUser?.id === user.id ? "Me" : "User" } : null; const isStub = !!item.is_stub; const disabled = !isStub && item.results.length === 0; return ( { if (hasChanges) { annotation.saveDraftImmediately(); // wait for draft to be saved before switching to history await when(() => !annotation.isDraftSaving); } if (isLastItem || isSelected) { // last history state and draft are actual annotation, not from history // and if user clicks on already selected item we should switch to last state annotationStore.selectHistory(null); // if user clicks on last history state we should disable draft to see submitted state annotation.toggleDraft(isSelected); } else if (isStub) { // Stub item: host (e.g. LSE) fetches full item and calls back; then we hydrate and select. // item.pk is the numeric history row id (preserved in HistoryItem preProcessSnapshot); item.id is the MST guid. const historyPk = item.pk; if (historyPk == null) return; getEnv(store).events.invoke("hydrateHistoryItem", historyPk, (fullItem: any) => { if (fullItem && store.hydrateHistoryItem) { store.hydrateHistoryItem(historyPk, fullItem); } }); } else { annotationStore.selectHistory(item); } }} /> ); })}
); }; const HistoryItemComponent: FC<{ entity?: any; user: any; date: string | number; extra?: any; comment: string; acceptedState: HistoryItemType; selected?: boolean; disabled?: boolean; inline?: boolean; hideInfo?: boolean; onClick: any; }> = ({ entity, user, date, extra, comment, acceptedState, selected = false, disabled = false, inline = false, hideInfo: infoIsHidden, onClick, }) => { const isPrediction = entity?.type === "prediction"; const reason = useMemo(() => { switch (acceptedState) { case "accepted": return "Accepted"; case "rejected": return "Rejected"; case "fixed_and_accepted": return "Fixed"; case "updated": return "Updated"; case "submitted": return "Submitted"; case "prediction": return "From prediction"; case "imported": return "Imported"; case "skipped": return "Skipped"; case "draft_created": return "Draft"; case "deleted_review": return "Review deleted"; case "propagated_annotation": return "Propagated"; default: return null; } }, []); const handleClick = useCallback( (e) => { if (disabled) return; onClick(e); }, [onClick, disabled], ); return (
{isPrediction && } {isPrediction ? entity.createdBy : userDisplayName(user)} {!infoIsHidden && ( {extra &&
{extra}
} {date && (
{humanDateDiff(date)}
)}
)}
{(reason || comment) && ( {acceptedState && } )}
); }; const HistoryComment: FC<{ reason: string | null; comment: string; }> = ({ reason, comment }) => { const [collapsed, setCollapsed] = useState(false); const [collapsible, setCollapsible] = useState(false); const commentRef = useRef(); useLayoutEffect(() => { if (commentRef.current) { const { clientHeight } = commentRef.current; // 3 lines of text 22px height each const heightExceeded = clientHeight > 66; setCollapsible(heightExceeded); setCollapsed(heightExceeded); } }, []); return (
{comment}
{collapsible && (
{ e.stopPropagation(); setCollapsed((v) => !v); }} > {collapsed ? "Show more" : "Show less"}
)}
); }; const HistoryIcon: FC<{ type: HistoryItemType }> = ({ type }) => { const icon = useMemo(() => { switch (type) { case "submitted": return ; case "updated": return ; case "draft_created": return ; case "accepted": return ; case "rejected": return ; case "fixed_and_accepted": return ; case "prediction": return ; case "imported": return ; case "skipped": return ; case "deleted_review": return ; case "propagated_annotation": return ; default: return null; } }, [type]); return icon &&
{icon}
; }; const HistoryItem = observer(HistoryItemComponent); HistoryItem.displayName = "HistoryItem"; export const AnnotationHistory = injector(observer(AnnotationHistoryComponent)); AnnotationHistory.displayName = "AnnotationHistory";