-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
import { Composer } from 'grammy' | ||
import { makeButton } from '@telegum/grammy-buttons' | ||
import type { View } from '.' | ||
import views from '.' | ||
import type { Ctx } from '~/bot/context' | ||
import { Table } from '~/bot/utils/table' | ||
|
||
const VIEW_ID = 'semesters/summary' | ||
|
||
const BackButton = makeButton({ id: `${VIEW_ID}:back` }) | ||
|
||
export default { | ||
render: async (ctx) => { | ||
const summary = await ctx.domain.getSemestersSummary({ telegramId: ctx.from!.id }) | ||
|
||
const rows = [ | ||
['Semester', 'Hours', 'Test'], | ||
...summary.map(({ title, hoursTotal, fitnessTest }) => [ | ||
title.length > 8 | ||
? `${title.slice(0, 6)}...` | ||
: title, | ||
hoursTotal.toString(), | ||
`${fitnessTest.passed ? '✔' : '✘'} ${fitnessTest.pointsTotal}%`, | ||
]), | ||
] | ||
|
||
return ( | ||
<> | ||
<Table rows={rows} headingRow /> | ||
<keyboard> | ||
<BackButton>{ctx.t['Buttons.Back']}</BackButton> | ||
</keyboard> | ||
</> | ||
) | ||
}, | ||
middleware: () => { | ||
const composer = new Composer<Ctx>() | ||
|
||
composer | ||
.filter(BackButton.filter) | ||
.use(async (ctx) => { | ||
ctx.answerCallbackQuery() | ||
await ctx | ||
.edit(ctx.chat!.id, ctx.callbackQuery.message!.message_id) | ||
.to(await views.main.render(ctx, {})) | ||
}) | ||
|
||
return composer.middleware() | ||
}, | ||
} satisfies View<Ctx> as View<Ctx> |
This file contains 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,45 @@ | ||
import type { TgxElement } from '@telegum/tgx' | ||
|
||
export type TableProps = { | ||
rows: string[][] | ||
headingRow?: boolean | ||
} | ||
|
||
export function Table({ | ||
rows, | ||
headingRow = false, | ||
}: TableProps): TgxElement { | ||
if (rows.length === 0) { | ||
return <></> | ||
} | ||
|
||
const colCount = rows[0].length | ||
const colLengths = Array.from({ length: colCount }, () => 0) | ||
|
||
for (const row of rows) { | ||
for (let i = 0; i < colCount; i++) { | ||
colLengths[i] = Math.max(colLengths[i], row[i].length) | ||
} | ||
} | ||
|
||
const table = [] | ||
|
||
for (const row of rows) { | ||
const cells = row.map((cell, i) => { | ||
const padding = ' '.repeat(colLengths[i] - cell.length) | ||
return `${cell}${padding}` | ||
}) | ||
table.push(cells.join(' | ')) | ||
} | ||
|
||
if (headingRow) { | ||
const divider = colLengths.map(len => '-'.repeat(len)).join('-+-') | ||
table.splice(1, 0, divider) | ||
} | ||
|
||
return ( | ||
<> | ||
{table.map(row => (<code>{row}<br/></code>))} | ||
</> | ||
) | ||
} |
This file contains 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