|
| 1 | +import { createContext, useContext, useState, type ReactNode } from "react"; |
| 2 | +import { |
| 3 | + Sheet, |
| 4 | + SheetContent, |
| 5 | + SheetHeader, |
| 6 | + SheetTitle, |
| 7 | +} from "@/components/ui/sheet"; |
| 8 | +import { Skeleton } from "@/components/ui/skeleton"; |
| 9 | +import { useNomenclature } from "@/hooks/use-nomenclature"; |
| 10 | + |
| 11 | +const NomenclatureDrawerContext = createContext< |
| 12 | + ((name: string) => void) | null |
| 13 | +>(null); |
| 14 | + |
| 15 | +function useNomenclatureDrawer() { |
| 16 | + const open = useContext(NomenclatureDrawerContext); |
| 17 | + if (!open) { |
| 18 | + throw new Error( |
| 19 | + "useNomenclatureDrawer must be used within a NomenclatureDrawerProvider", |
| 20 | + ); |
| 21 | + } |
| 22 | + return open; |
| 23 | +} |
| 24 | + |
| 25 | +function NomenclatureHeader({ name }: { name: string }) { |
| 26 | + const { data, isPending } = useNomenclature(name); |
| 27 | + |
| 28 | + if (isPending) { |
| 29 | + return ( |
| 30 | + <div className="space-y-2"> |
| 31 | + <Skeleton className="h-5 w-40" /> |
| 32 | + <Skeleton className="h-4 w-64" /> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + return ( |
| 38 | + <> |
| 39 | + <SheetTitle className="font-bold text-xl">{data?.title ?? name}</SheetTitle> |
| 40 | + {data?.description && ( |
| 41 | + <p className="text-sm text-muted-foreground">{data.description}</p> |
| 42 | + )} |
| 43 | + </> |
| 44 | + ); |
| 45 | +} |
| 46 | + |
| 47 | +function NomenclatureContent({ name }: { name: string }) { |
| 48 | + const { data, isPending, isError } = useNomenclature(name); |
| 49 | + |
| 50 | + if (isPending) { |
| 51 | + return ( |
| 52 | + <div className="space-y-2 p-4"> |
| 53 | + {Array.from({ length: 4 }).map((_, i) => ( |
| 54 | + <Skeleton key={i} className="h-6 w-full" /> |
| 55 | + ))} |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + if (isError) { |
| 61 | + return ( |
| 62 | + <p className="p-4 text-sm text-muted-foreground"> |
| 63 | + Impossible de charger la nomenclature. |
| 64 | + </p> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <table className="w-full text-sm"> |
| 70 | + <tbody> |
| 71 | + {data.oneOf?.map((option) => ( |
| 72 | + <tr key={option.const} className="border-b last:border-0"> |
| 73 | + <td className="w-px whitespace-nowrap p-4 align-top"> |
| 74 | + <span className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs text-muted-foreground"> |
| 75 | + {option.const} |
| 76 | + </span> |
| 77 | + </td> |
| 78 | + <td className="p-4 pl-0 align-top"> |
| 79 | + <p className="font-bold text-sm">{option.title}</p> |
| 80 | + <p className="italic text-xs">{option?.description}</p> |
| 81 | + </td> |
| 82 | + </tr> |
| 83 | + ))} |
| 84 | + </tbody> |
| 85 | + </table> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +export function NomenclatureDrawerProvider({ |
| 90 | + children, |
| 91 | +}: { |
| 92 | + children: ReactNode; |
| 93 | +}) { |
| 94 | + const [name, setName] = useState<string | null>(null); |
| 95 | + |
| 96 | + return ( |
| 97 | + <NomenclatureDrawerContext.Provider value={setName}> |
| 98 | + {children} |
| 99 | + <Sheet open={name !== null} onOpenChange={(open) => !open && setName(null)}> |
| 100 | + <SheetContent className={"w-full max-w-4xl!"}> |
| 101 | + <SheetHeader className="border-b">{name && <NomenclatureHeader name={name} />}</SheetHeader> |
| 102 | + <div className="min-h-0 flex-1 overflow-y-auto w-full"> |
| 103 | + {name && <NomenclatureContent name={name} />} |
| 104 | + </div> |
| 105 | + </SheetContent> |
| 106 | + </Sheet> |
| 107 | + </NomenclatureDrawerContext.Provider> |
| 108 | + ); |
| 109 | +} |
| 110 | + |
| 111 | +export function NomenclatureBadge({ name }: { name: string }) { |
| 112 | + const open = useNomenclatureDrawer(); |
| 113 | + |
| 114 | + return ( |
| 115 | + <button |
| 116 | + type="button" |
| 117 | + onClick={(e) => { |
| 118 | + e.stopPropagation(); |
| 119 | + open(name); |
| 120 | + }} |
| 121 | + className="rounded bg-primary px-1.5 py-0.5 font-mono text-xs text-primary-foreground hover:bg-primary/90" |
| 122 | + > |
| 123 | + {name} |
| 124 | + </button> |
| 125 | + ); |
| 126 | +} |
0 commit comments