-
Notifications
You must be signed in to change notification settings - Fork 1
feat: export substitutions #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KoZsombat
wants to merge
6
commits into
main
Choose a base branch
from
admin-subs-export
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5c6fe1a
feat: export substitutions
KoZsombat b8789ca
fix: error catch, simplifying
KoZsombat 385dd4b
fix: row unified, translation
KoZsombat d45ae9b
fix: neutralize CSV formula injection in substitution export
KoZsombat efef02e
fix: prevent TS2345 error in CSV formula injection guard
KoZsombat e54aa26
fix: no font file, type infer
KoZsombat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 219 additions & 0 deletions
219
apps/iris/src/components/admin/substitution-export-dialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| import { pdf } from '@react-pdf/renderer'; | ||
| import dayjs from 'dayjs'; | ||
| import type { InferResponseType } from 'hono/client'; | ||
| import { Loader2Icon } from 'lucide-react'; | ||
| import { useState } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { toast } from 'sonner'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { DatePicker } from '@/components/ui/date-picker'; | ||
| import { | ||
| Dialog, | ||
| DialogContent, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from '@/components/ui/dialog'; | ||
| import { Label } from '@/components/ui/label'; | ||
| import { | ||
| Select, | ||
| SelectContent, | ||
| SelectItem, | ||
| SelectTrigger, | ||
| SelectValue, | ||
| } from '@/components/ui/select'; | ||
| import type { api } from '@/utils/hc'; | ||
| import { | ||
| type SubstitutionExportRow, | ||
| SubstitutionPDF, | ||
| } from './substitution-pdf'; | ||
|
|
||
| type SubstitutionItem = NonNullable< | ||
| InferResponseType<typeof api.timetable.substitutions.$get>['data'] | ||
| >[number]; | ||
|
|
||
| type Props = { | ||
| open: boolean; | ||
| onOpenChange: (open: boolean) => void; | ||
| substitutions: SubstitutionItem[]; | ||
| }; | ||
|
|
||
| function buildRows( | ||
| substitutions: SubstitutionItem[], | ||
| date: Date | ||
| ): SubstitutionExportRow[] { | ||
| const target = dayjs(date).format('YYYY-MM-DD'); | ||
| const rows: SubstitutionExportRow[] = []; | ||
|
|
||
| for (const sub of substitutions) { | ||
| if (dayjs(sub.substitution.date).format('YYYY-MM-DD') !== target) { | ||
| continue; | ||
| } | ||
| const substituteTeacher = sub.teacher | ||
| ? `${sub.teacher.firstName} ${sub.teacher.lastName}` | ||
| : ''; | ||
|
|
||
| for (const lesson of sub.lessons) { | ||
| if (!lesson) { | ||
| continue; | ||
| } | ||
| const missingTeacher = (lesson.teachers ?? []) | ||
| .map((t) => t.name) | ||
| .join(', '); | ||
| const cohorts = (lesson.cohorts ?? []).join(', '); | ||
| const period = lesson.period ? `${lesson.period.period}.` : '?'; | ||
| rows.push({ cohorts, missingTeacher, period, substituteTeacher }); | ||
| } | ||
| } | ||
|
|
||
| return rows; | ||
| } | ||
|
|
||
| function downloadCsv( | ||
| rows: SubstitutionExportRow[], | ||
| filename: string, | ||
| labels: { | ||
| missingTeacher: string; | ||
| substituteTeacher: string; | ||
| class: string; | ||
| period: string; | ||
| } | ||
| ) { | ||
| const dangerous = ['=', '+', '-', '@', '\t', '\r']; | ||
| const escapeS = (v: string) => { | ||
| const safe = dangerous.some((c) => v.startsWith(c)) ? `'${v}` : v; | ||
| return safe.includes(';') || safe.includes('"') || safe.includes('\n') | ||
| ? `"${safe.replace(/"/g, '""')}"` | ||
| : safe; | ||
| }; | ||
|
|
||
| const lines = [ | ||
| [ | ||
| labels.missingTeacher, | ||
| labels.substituteTeacher, | ||
| labels.class, | ||
| labels.period, | ||
| ] | ||
| .map(escapeS) | ||
| .join(';'), | ||
| ...rows.map((r) => | ||
| [r.missingTeacher, r.substituteTeacher, r.cohorts, r.period] | ||
| .map(escapeS) | ||
| .join(';') | ||
| ), | ||
| ]; | ||
|
|
||
| // BOM for Excel UTF-8 recognition | ||
| const bom = ''; | ||
| const blob = new Blob([bom + lines.join('\r\n')], { | ||
| type: 'text/csv;charset=utf-8;', | ||
| }); | ||
| const url = URL.createObjectURL(blob); | ||
| const a = document.createElement('a'); | ||
| a.href = url; | ||
| a.download = filename; | ||
| a.click(); | ||
| URL.revokeObjectURL(url); | ||
| } | ||
|
|
||
| export function SubstitutionExportDialog({ | ||
| open, | ||
| onOpenChange, | ||
| substitutions, | ||
| }: Props) { | ||
| const { i18n, t } = useTranslation(); | ||
| const [date, setDate] = useState<Date>(new Date()); | ||
| const [format, setFormat] = useState<'pdf' | 'csv'>('pdf'); | ||
| const [loading, setLoading] = useState(false); | ||
|
|
||
| const labels = { | ||
| class: t('substitution.exportClass'), | ||
| missingTeacher: t('substitution.exportAbsentTeacher'), | ||
| noSubstitutions: t('substitution.noSubstitutions'), | ||
| period: t('substitution.period'), | ||
| substituteTeacher: t('substitution.substituteTeacher'), | ||
| }; | ||
|
|
||
| const handleExport = async () => { | ||
| setLoading(true); | ||
| try { | ||
| const rows = buildRows(substitutions, date); | ||
| const dateLabel = new Intl.DateTimeFormat( | ||
| i18n.language === 'hu' ? 'hu-HU' : 'en-US' | ||
| ).format(date); | ||
| const isoDate = dayjs(date).format('YYYY-MM-DD'); | ||
|
|
||
| if (format === 'pdf') { | ||
| const blob = await pdf( | ||
| <SubstitutionPDF date={dateLabel} labels={labels} rows={rows} /> | ||
| ).toBlob(); | ||
| const url = URL.createObjectURL(blob); | ||
| window.open(url, '_blank'); | ||
| } else { | ||
| downloadCsv(rows, `substitutions-${isoDate}.csv`, labels); | ||
| } | ||
|
|
||
| onOpenChange(false); | ||
| } catch (error) { | ||
| toast.error( | ||
| t('error.generic', { | ||
| message: error instanceof Error ? error.message : 'Export failed', | ||
| }) | ||
| ); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| return ( | ||
| <Dialog onOpenChange={onOpenChange} open={open}> | ||
| <DialogContent showCloseButton={!loading}> | ||
| <DialogHeader> | ||
| <DialogTitle>{t('substitution.exportTitle')}</DialogTitle> | ||
| </DialogHeader> | ||
|
|
||
| <div className="space-y-4 py-2"> | ||
| <div className="space-y-2"> | ||
| <Label>{t('substitution.date')}</Label> | ||
| <DatePicker | ||
| date={date} | ||
| disabled={loading} | ||
| onDateChange={(d) => d && setDate(d)} | ||
| placeholder={t('substitution.datePlaceholder')} | ||
| /> | ||
| </div> | ||
|
|
||
| <div className="space-y-2"> | ||
| <Label>{t('substitution.exportFormat')}</Label> | ||
| <Select | ||
| disabled={loading} | ||
| onValueChange={(v) => setFormat(v as 'pdf' | 'csv')} | ||
| value={format} | ||
| > | ||
| <SelectTrigger className="w-full"> | ||
| <SelectValue /> | ||
| </SelectTrigger> | ||
| <SelectContent> | ||
| <SelectItem value="pdf">PDF</SelectItem> | ||
| <SelectItem value="csv">CSV</SelectItem> | ||
| </SelectContent> | ||
| </Select> | ||
| </div> | ||
| </div> | ||
|
|
||
| <DialogFooter showCloseButton={!loading}> | ||
| <Button disabled={loading} onClick={handleExport}> | ||
| {loading ? ( | ||
| <> | ||
| <Loader2Icon className="animate-spin" /> | ||
| {t('substitution.exporting')} | ||
| </> | ||
| ) : ( | ||
| t('substitution.export') | ||
| )} | ||
| </Button> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </Dialog> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import notoSansRegular from '@expo-google-fonts/noto-sans/400Regular/NotoSans_400Regular.ttf?url'; | ||
| import notoSansBold from '@expo-google-fonts/noto-sans/700Bold/NotoSans_700Bold.ttf?url'; | ||
| import { | ||
| Document, | ||
| Font, | ||
| Page, | ||
| StyleSheet, | ||
| Text, | ||
| View, | ||
| } from '@react-pdf/renderer'; | ||
|
|
||
| Font.register({ | ||
| family: 'NotoSans', | ||
| fonts: [ | ||
| { fontWeight: 400, src: notoSansRegular }, | ||
| { fontWeight: 700, src: notoSansBold }, | ||
| ], | ||
| }); | ||
|
|
||
| // Prevent automatic hyphenation so column headers stay on one line | ||
| Font.registerHyphenationCallback((word) => [word]); | ||
|
|
||
| export type SubstitutionExportRow = { | ||
| missingTeacher: string; | ||
| substituteTeacher: string; | ||
| cohorts: string; | ||
| period: string; | ||
| }; | ||
|
|
||
| type Labels = { | ||
| missingTeacher: string; | ||
| substituteTeacher: string; | ||
| class: string; | ||
| period: string; | ||
| noSubstitutions: string; | ||
| }; | ||
|
|
||
| type Props = { | ||
| rows: SubstitutionExportRow[]; | ||
| date: string; | ||
| labels: Labels; | ||
| }; | ||
|
|
||
| const FONT_STACK = 'NotoSans'; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| bold: { | ||
| fontWeight: 700, | ||
| }, | ||
| cell: { | ||
| flex: 1, | ||
| padding: '6 8', | ||
| }, | ||
| dash: { | ||
| color: '#9ca3af', | ||
| }, | ||
| header: { | ||
| fontSize: 14, | ||
| fontWeight: 700, | ||
| marginBottom: 16, | ||
| }, | ||
| headerCell: { | ||
| flex: 1, | ||
| fontWeight: 700, | ||
| padding: '6 8', | ||
| }, | ||
| headerPeriodCell: { | ||
| flexShrink: 0, | ||
| fontWeight: 700, | ||
| padding: '6 8', | ||
| width: 80, | ||
| }, | ||
| page: { | ||
| fontFamily: FONT_STACK, | ||
| fontSize: 10, | ||
| padding: 32, | ||
| }, | ||
| periodCell: { | ||
| flexShrink: 0, | ||
| padding: '6 8', | ||
| width: 80, | ||
| }, | ||
| table: { | ||
| width: '100%', | ||
| }, | ||
| tableHeaderRow: { | ||
| borderBottomColor: '#111827', | ||
| borderBottomWidth: 2, | ||
| flexDirection: 'row', | ||
| marginBottom: 2, | ||
| }, | ||
| tableRow: { | ||
| borderBottomColor: '#e5e7eb', | ||
| borderBottomWidth: 1, | ||
| flexDirection: 'row', | ||
| }, | ||
| }); | ||
|
|
||
| export function SubstitutionPDF({ rows, date, labels }: Props) { | ||
| return ( | ||
| <Document> | ||
| <Page size="A4" style={styles.page}> | ||
| <Text style={styles.header}>{date}</Text> | ||
| <View style={styles.table}> | ||
| <View style={styles.tableHeaderRow}> | ||
| <Text style={styles.headerCell}>{labels.missingTeacher}</Text> | ||
| <Text style={styles.headerCell}>{labels.substituteTeacher}</Text> | ||
| <Text style={styles.headerCell}>{labels.class}</Text> | ||
| <Text style={styles.headerPeriodCell}>{labels.period}</Text> | ||
| </View> | ||
| {rows.map((row, i) => ( | ||
| // biome-ignore lint/suspicious/noArrayIndexKey: rows are positional, no stable id | ||
| <View key={i} style={styles.tableRow}> | ||
| <Text style={[styles.cell, styles.bold]}> | ||
| {row.missingTeacher} | ||
| </Text> | ||
| <Text style={[styles.cell, styles.bold]}> | ||
| {row.substituteTeacher || '-'} | ||
| </Text> | ||
| <Text style={styles.cell}>{row.cohorts || '-'}</Text> | ||
| <Text style={styles.periodCell}>{row.period}</Text> | ||
| </View> | ||
| ))} | ||
| {rows.length === 0 && ( | ||
| <View style={styles.tableRow}> | ||
| <Text style={[styles.cell, styles.dash, { width: '100%' }]}> | ||
| {labels.noSubstitutions} | ||
| </Text> | ||
| </View> | ||
| )} | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| </View> | ||
| </Page> | ||
| </Document> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: filcdev/filc
Length of output: 638
apps/iris/src/components/admin/substitution-export-dialog.tsx (162-167): use a localized fallback for non-Error export failures
error.genericalready exists in bothen/huand supportsmessageinterpolation ({{message}}).'Export failed'fallback and supply at(...)-based localized message instead (add a locale key if needed).🤖 Prompt for AI Agents