|
| 1 | +import { useEffect } from "react"; |
| 2 | +import type { Consumer } from "../types"; |
| 3 | +import { Badge } from "./ui/badge"; |
| 4 | +import { Button } from "./ui/button"; |
| 5 | + |
| 6 | +type ExportDrawerProps = { |
| 7 | + isOpen: boolean; |
| 8 | + consumers: Consumer[]; |
| 9 | + selectedConsumerId: string; |
| 10 | + exporting: boolean; |
| 11 | + error?: string; |
| 12 | + onClose: () => void; |
| 13 | + onSelectConsumer: (consumerId: string) => void; |
| 14 | + onExport: () => void; |
| 15 | +}; |
| 16 | + |
| 17 | +export function ExportDrawer(props: Readonly<ExportDrawerProps>) { |
| 18 | + const { |
| 19 | + isOpen, |
| 20 | + consumers, |
| 21 | + selectedConsumerId, |
| 22 | + exporting, |
| 23 | + error, |
| 24 | + onClose, |
| 25 | + onSelectConsumer, |
| 26 | + onExport |
| 27 | + } = props; |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (!isOpen) { |
| 31 | + return; |
| 32 | + } |
| 33 | + function onKeyDown(event: KeyboardEvent) { |
| 34 | + if (event.key === "Escape") { |
| 35 | + onClose(); |
| 36 | + } |
| 37 | + } |
| 38 | + globalThis.addEventListener("keydown", onKeyDown); |
| 39 | + return () => { |
| 40 | + globalThis.removeEventListener("keydown", onKeyDown); |
| 41 | + }; |
| 42 | + }, [isOpen, onClose]); |
| 43 | + |
| 44 | + return ( |
| 45 | + <div |
| 46 | + className={`pointer-events-none fixed inset-0 z-40 transition-colors duration-300 ${ |
| 47 | + isOpen ? "bg-slate-900/30" : "bg-transparent" |
| 48 | + }`} |
| 49 | + aria-hidden={!isOpen} |
| 50 | + > |
| 51 | + <button |
| 52 | + type="button" |
| 53 | + className={`absolute inset-0 h-full w-full ${isOpen ? "pointer-events-auto" : "pointer-events-none"}`} |
| 54 | + onClick={onClose} |
| 55 | + aria-label="Close export panel" |
| 56 | + /> |
| 57 | + <dialog |
| 58 | + open={isOpen} |
| 59 | + className={`pointer-events-auto absolute right-0 top-0 h-full w-full max-w-xl transform border-l border-[var(--color-border)] bg-[var(--color-surface)] p-6 shadow-2xl transition-transform duration-300 ${ |
| 60 | + isOpen ? "translate-x-0" : "translate-x-full" |
| 61 | + }`} |
| 62 | + aria-label="Export learner record" |
| 63 | + > |
| 64 | + <div className="mb-4 flex items-start justify-between gap-4"> |
| 65 | + <div> |
| 66 | + <p className="text-xs font-semibold uppercase tracking-[0.16em] text-[var(--color-text-muted)]">Export</p> |
| 67 | + <h3 className="text-xl font-semibold text-[var(--color-text)]">Download Learner Record</h3> |
| 68 | + <p className="mt-1 text-sm text-[var(--color-text-muted)]"> |
| 69 | + Choose a partner profile and export a tailored learner record. |
| 70 | + </p> |
| 71 | + </div> |
| 72 | + <Button type="button" tone="secondary" onClick={onClose}> |
| 73 | + Close |
| 74 | + </Button> |
| 75 | + </div> |
| 76 | + |
| 77 | + <div className="space-y-3 overflow-y-auto pb-24"> |
| 78 | + {consumers.length === 0 && ( |
| 79 | + <div className="rounded-lg border border-dashed border-[var(--color-border)] bg-[var(--color-surface-subtle)] p-4 text-sm text-[var(--color-text-muted)]"> |
| 80 | + No partner profiles yet. Create one in Data Sharing to enable exports. |
| 81 | + </div> |
| 82 | + )} |
| 83 | + {consumers.map((consumer) => { |
| 84 | + const isSelected = consumer.consumerId === selectedConsumerId; |
| 85 | + return ( |
| 86 | + <button |
| 87 | + key={consumer.consumerId} |
| 88 | + type="button" |
| 89 | + onClick={() => onSelectConsumer(consumer.consumerId)} |
| 90 | + className={`w-full rounded-lg border p-4 text-left transition-all duration-150 ${ |
| 91 | + isSelected |
| 92 | + ? "border-[var(--color-brand)] bg-[var(--color-brand-soft)]/55" |
| 93 | + : "border-[var(--color-border)] bg-white hover:border-[var(--color-brand)]/50 hover:bg-[var(--color-surface-subtle)]" |
| 94 | + }`} |
| 95 | + > |
| 96 | + <div className="flex flex-wrap items-center gap-2"> |
| 97 | + <p className="font-medium text-[var(--color-text)]">{consumer.name}</p> |
| 98 | + <Badge>{consumer.policy.includeBadges ? "Badges included" : "Badges hidden"}</Badge> |
| 99 | + <Badge>{consumer.policy.includeGrades ? "Grades included" : "Grades hidden"}</Badge> |
| 100 | + </div> |
| 101 | + <p className="mt-2 text-xs text-[var(--color-text-muted)]"> |
| 102 | + {consumer.policy.allowedCourses.length > 0 |
| 103 | + ? `${consumer.policy.allowedCourses.length} course preference(s)` |
| 104 | + : "All courses available"} |
| 105 | + </p> |
| 106 | + <p className="mt-1 break-all text-xs text-[var(--color-text-muted)]">{consumer.didKey}</p> |
| 107 | + </button> |
| 108 | + ); |
| 109 | + })} |
| 110 | + {error && <p className="text-sm text-[var(--color-danger)]">{error}</p>} |
| 111 | + </div> |
| 112 | + |
| 113 | + <div className="absolute bottom-0 left-0 right-0 border-t border-[var(--color-border)] bg-[var(--color-surface)] p-6"> |
| 114 | + <Button |
| 115 | + type="button" |
| 116 | + onClick={onExport} |
| 117 | + disabled={!selectedConsumerId || exporting} |
| 118 | + className="w-full justify-center" |
| 119 | + > |
| 120 | + {exporting ? "Preparing download..." : "Download record"} |
| 121 | + </Button> |
| 122 | + </div> |
| 123 | + </dialog> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
0 commit comments