Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 1x 12x 1x | // src/components/admin/AdminProvidedDocumentsSection.tsx
//
// Admin surface for providing documents TO an investor (FSC-57). Lists the
// documents the admin has attached for this investor and offers an upload
// dialog. The investor sees these on their Documents page under "From
// FlowState".
import {type ChangeEvent, type ReactElement, useState} from 'react';
import {
Alert,
Box,
Button,
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
IconButton,
Paper,
Skeleton,
Snackbar,
Stack,
TextField,
Tooltip,
Typography
} from '@mui/material';
import {
CloudUpload as UploadIcon,
Delete as DeleteIcon,
Description as FileIcon,
Download as DownloadIcon
} from '@mui/icons-material';
import {
useAdminDownloadProvidedDocumentMutation,
useDeleteProvidedDocumentMutation,
useListInvestorProvidedDocumentsQuery,
useProvideDocumentMutation
} from '../../services/adminDocumentsApi';
import type {ProvidedDocument} from '../../services/providedDocumentsApi';
import {formatBytes} from '../../services/uploadsApi';
import {downloadBlob} from '../../utils/downloadBlob';
interface SnackbarState {
open: boolean;
message: string;
severity: 'success' | 'error';
}
interface AdminProvidedDocumentsSectionProps {
investorId: number;
}
function formatDate(dateStr: string): string {
return new Date(dateStr).toLocaleDateString('en-US', {year: 'numeric', month: 'short', day: 'numeric'});
}
// ============================================================================
// Provide dialog
// ============================================================================
interface ProvideDialogProps {
investorId: number;
onClose: () => void;
onSuccess: (message: string) => void;
onError: (message: string) => void;
}
function ProvideDialog({investorId, onClose, onSuccess, onError}: ProvideDialogProps): ReactElement {
const [provideDocument, {isLoading}] = useProvideDocumentMutation();
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [file, setFile] = useState<File | null>(null);
const [error, setError] = useState<string | null>(null);
const handleFileChange = (event: ChangeEvent<HTMLInputElement>): void => {
setFile(event.target.files?.[0] ?? null);
setError(null);
};
const handleSubmit = (): void => {
if (title.trim() === '' || file === null) {
setError('Enter a title and choose a file.');
return;
}
provideDocument({investorId, title: title.trim(), description: description.trim(), file})
.unwrap()
.then(() => {
onSuccess('Document provided to investor.');
onClose();
})
.catch((err: unknown) => {
const message = extractErrorMessage(err) ?? 'Upload failed.';
onError(message);
setError(message);
});
};
return (
<Dialog open onClose={onClose} maxWidth="sm" fullWidth>
<DialogTitle>Provide a Document</DialogTitle>
<DialogContent>
<DialogContentText sx={{mb: 2}}>
Attach a document for this investor to view and download. They'll see it under
"From FlowState" on their Documents page.
</DialogContentText>
<Stack spacing={2}>
<TextField
label="Title"
value={title}
onChange={(e) => setTitle(e.target.value)}
fullWidth
required
autoFocus
/>
<TextField
label="Description (optional)"
value={description}
onChange={(e) => setDescription(e.target.value)}
fullWidth
multiline
minRows={2}
/>
<Box>
<Button variant="outlined" component="label" startIcon={<UploadIcon/>}>
{file === null ? 'Choose file' : 'Replace file'}
<input type="file" hidden onChange={handleFileChange}/>
</Button>
{file !== null && (
<Typography variant="body2" sx={{mt: 1}}>
<strong>{file.name}</strong> · {formatBytes(file.size)}
</Typography>
)}
</Box>
{error !== null && <Alert severity="error">{error}</Alert>}
</Stack>
</DialogContent>
<DialogActions>
<Button onClick={onClose} disabled={isLoading}>Cancel</Button>
<Button
variant="contained"
onClick={handleSubmit}
disabled={isLoading || title.trim() === '' || file === null}
>
{isLoading ? 'Uploading…' : 'Provide'}
</Button>
</DialogActions>
</Dialog>
);
}
// ============================================================================
// Section
// ============================================================================
export default function AdminProvidedDocumentsSection({investorId}: AdminProvidedDocumentsSectionProps): ReactElement {
const {data, isLoading, isError} = useListInvestorProvidedDocumentsQuery(investorId);
const [downloadDocument, {isLoading: isDownloading}] = useAdminDownloadProvidedDocumentMutation();
const [deleteDocument] = useDeleteProvidedDocumentMutation();
const [dialogOpen, setDialogOpen] = useState(false);
const [snackbar, setSnackbar] = useState<SnackbarState>({open: false, message: '', severity: 'success'});
const documents = data?.documents ?? [];
const handleDownload = (doc: ProvidedDocument): void => {
downloadDocument({investorId, providedDocumentId: doc.providedDocumentId})
.unwrap()
.then((blob) => {
downloadBlob(blob, doc.originalFilename);
})
.catch(() => {
setSnackbar({open: true, message: 'Failed to download document.', severity: 'error'});
});
};
const handleDelete = (doc: ProvidedDocument): void => {
deleteDocument({investorId, providedDocumentId: doc.providedDocumentId})
.unwrap()
.then(() => {
setSnackbar({open: true, message: 'Document removed.', severity: 'success'});
})
.catch(() => {
setSnackbar({open: true, message: 'Failed to remove document.', severity: 'error'});
});
};
return (
<Box>
<Stack direction="row" sx={{justifyContent: 'space-between', alignItems: 'center', mb: 1.5}}>
<Typography variant="body2" sx={{color: 'text.secondary'}}>
Documents you've provided to this investor.
</Typography>
<Button
variant="contained"
size="small"
startIcon={<UploadIcon/>}
onClick={() => setDialogOpen(true)}
>
Provide Document
</Button>
</Stack>
{isLoading ? (
<Stack spacing={1.5}>
{[1, 2].map((i) => <Skeleton key={i} variant="rounded" height={72}/>)}
</Stack>
) : isError ? (
<Alert severity="error">Unable to load provided documents.</Alert>
) : documents.length === 0 ? (
<Typography variant="body2" sx={{color: 'text.secondary'}}>
You haven't provided any documents to this investor yet.
</Typography>
) : (
<Stack spacing={1.5}>
{documents.map((doc) => (
<Paper key={doc.providedDocumentId} variant="outlined" sx={{p: 2, borderRadius: 2}}>
<Stack direction={{xs: 'column', sm: 'row'}} spacing={2} sx={{alignItems: {sm: 'center'}}}>
<Box sx={{
width: 40,
height: 40,
borderRadius: 2,
backgroundColor: 'primary.light',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0
}}>
<FileIcon sx={{color: 'primary.main'}}/>
</Box>
<Box sx={{flexGrow: 1, minWidth: 0}}>
<Typography variant="body1" sx={{fontWeight: 600}}>{doc.title}</Typography>
{doc.description !== null && doc.description !== '' && (
<Typography variant="body2" sx={{color: 'text.secondary'}}>
{doc.description}
</Typography>
)}
<Stack direction="row" spacing={3} sx={{flexWrap: 'wrap', color: 'text.secondary', mt: 0.5}}>
<Typography variant="caption">{doc.originalFilename}</Typography>
<Typography variant="caption">{formatBytes(doc.sizeBytes)}</Typography>
<Typography variant="caption">Provided: {formatDate(doc.createdAt)}</Typography>
{doc.uploadedByName !== null && (
<Typography variant="caption">By: {doc.uploadedByName}</Typography>
)}
</Stack>
</Box>
<Stack direction="row" spacing={1} sx={{flexShrink: 0, alignItems: 'center'}}>
<Button
variant="outlined"
size="small"
startIcon={<DownloadIcon/>}
disabled={isDownloading}
onClick={() => {
handleDownload(doc);
}}
>
Download
</Button>
<Tooltip title="Remove">
<IconButton
size="small"
color="error"
aria-label="Remove document"
onClick={() => {
handleDelete(doc);
}}
>
<DeleteIcon fontSize="small"/>
</IconButton>
</Tooltip>
</Stack>
</Stack>
</Paper>
))}
</Stack>
)}
{dialogOpen && (
<ProvideDialog
investorId={investorId}
onClose={() => setDialogOpen(false)}
onSuccess={(message) => setSnackbar({open: true, message, severity: 'success'})}
onError={(message) => setSnackbar({open: true, message, severity: 'error'})}
/>
)}
<Snackbar
open={snackbar.open}
autoHideDuration={6000}
onClose={() => setSnackbar({...snackbar, open: false})}
>
<Alert
onClose={() => setSnackbar({...snackbar, open: false})}
severity={snackbar.severity}
sx={{width: '100%'}}
>
{snackbar.message}
</Alert>
</Snackbar>
</Box>
);
}
// ============================================================================
// Helpers
// ============================================================================
function extractErrorMessage(err: unknown): string | null {
if (typeof err !== 'object' || err === null) return null;
const maybe = err as {data?: {message?: string}};
if (typeof maybe.data?.message === 'string' && maybe.data.message !== '') {
return maybe.data.message;
}
return null;
}
|