Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/components/app-drawer-content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { alpha, type Theme } from '@mui/material/styles';
import { useTranslation } from '@/i18n/use-translation.ts';
import { routes } from '@/constants/routes.ts';
import { useDueReviewCount } from '@/features/flashcards/use-due-review-count.ts';
import type { CourseLevel } from '@/constants/courses/types.ts';
import {
isNavGroupActive,
Expand All @@ -26,6 +27,41 @@ import {

const NAV_ROW_PY = 1.5;

/** Flashcards due today, as a small pill after the menu label. Hidden at 0. */
function DueReviewBadge() {
const { t } = useTranslation();
const due = useDueReviewCount();

if (due === 0) {
return null;
}

return (
<Box
component="span"
aria-label={t('flashcards.reviewDueBadge', { count: due })}
sx={{
ml: 1,
px: 1,
minWidth: 24,
height: 24,
borderRadius: 12,
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0,
bgcolor: 'primary.main',
color: 'primary.contrastText',
fontSize: '0.875rem',
fontWeight: 600,
lineHeight: 1
}}
>
{due > 99 ? '99+' : due}
</Box>
);
}

function navDividerBorder(theme: Theme) {
return `1px solid ${alpha(
theme.palette.text.primary,
Expand Down Expand Up @@ -269,6 +305,7 @@ export function AppDrawerContent({ onNavigate }: AppDrawerContentProps) {
{GroupIcon && <GroupIcon fontSize="small" />}
</ListItemIcon>
<ListItemText primary={groupLabel} />
{group.path === routes.flashcards && <DueReviewBadge />}
</ListItemButton>
)}

Expand Down
2 changes: 2 additions & 0 deletions src/components/app-layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Footer } from '@/components/footer';
import { Header } from '@/components/header';
import { Menu } from '@/components/menu';
import { ScrollManager } from '@/components/scroll-manager';
import { DailyReviewPrompt } from '@/features/flashcards/daily-review-prompt.tsx';
import { loadJapaneseUiFont } from '@/theme/fonts.ts';
import { installRubyAwareCopyHandler } from '@/utils/ruby-selection.ts';

Expand Down Expand Up @@ -33,6 +34,7 @@ function AppLayout({ children }: AppLayoutProps) {
return (
<Box sx={{ display: 'flex', minHeight: '100svh', '@media print': { minHeight: 'auto' } }}>
<ScrollManager />
<DailyReviewPrompt />

<Header
drawerWidth={drawerWidth}
Expand Down
3 changes: 3 additions & 0 deletions src/constants/flashcards/srs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { storageKey } from '@/constants/site.ts';

export const FLASHCARD_REVIEWS_KEY = storageKey('FLASHCARD_REVIEWS');

/** Day the "cards due today" prompt was last dismissed. */
export const REVIEW_PROMPT_DAY_KEY = storageKey('REVIEW_PROMPT_DAY');

/** New cards a learner may start per day, across every collection. */
export const NEW_CARDS_PER_DAY = 10;

Expand Down
97 changes: 97 additions & 0 deletions src/features/flashcards/daily-review-prompt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use client';

import EventRepeatOutlined from '@mui/icons-material/EventRepeatOutlined';
import { Box, Button, Dialog, Stack, Typography } from '@mui/material';
import { alpha } from '@mui/material/styles';
import { LocaleLink } from '@/components/locale-link';
import { dayNumber, REVIEW_PROMPT_DAY_KEY } from '@/constants/flashcards/srs.ts';
import { routes } from '@/constants/routes.ts';
import { useDueReviewCount } from '@/features/flashcards/use-due-review-count.ts';
import { usePathname } from '@/i18n/navigation.tsx';
import { useTranslation } from '@/i18n/use-translation.ts';
import { usePersistentState } from '@/utils/use-persistent-state.ts';

function sanitizeDay(value: unknown): number {
return typeof value === 'number' && Number.isInteger(value) && value >= 0 ? value : 0;
}

/**
* Once a day, on the first page opened, say how many flashcards are due and
* offer to review them. Shown only when something is due, and never on the
* review page itself.
*/
export function DailyReviewPrompt() {
const { t } = useTranslation();
const pathname = usePathname();
const due = useDueReviewCount();
const today = dayNumber();
const [promptDay, setPromptDay] = usePersistentState(REVIEW_PROMPT_DAY_KEY, 0, sanitizeDay);
const open = due > 0 && promptDay !== today && pathname !== routes.flashcardReview;

const close = () => {
setPromptDay(today);
};

return (
<Dialog
open={open}
onClose={close}
fullWidth
maxWidth="xs"
aria-labelledby="daily-review-prompt-title"
aria-describedby="daily-review-prompt-count"
slotProps={{ paper: { sx: { borderRadius: 4, m: 2 } } }}
>
<Stack spacing={3} sx={{ p: { xs: 3, sm: 4 }, alignItems: 'center', textAlign: 'center' }}>
<Box
aria-hidden
sx={(theme) => ({
width: 72,
height: 72,
borderRadius: '50%',
display: 'grid',
placeItems: 'center',
color: 'primary.main',
bgcolor: alpha(theme.palette.primary.main, 0.1)
})}
>
<EventRepeatOutlined sx={{ fontSize: 36 }} />
</Box>

<Stack spacing={1} sx={{ alignItems: 'center' }}>
<Typography
id="daily-review-prompt-title"
component="h2"
variant="h5"
sx={{ fontWeight: 700 }}
>
{t('flashcards.reviewTitle')}
</Typography>
<Typography id="daily-review-prompt-count" variant="body1" color="text.secondary">
{t('flashcards.reviewPromptBody', { count: due })}
</Typography>
</Stack>

<Stack spacing={1} sx={{ width: '100%' }}>
<Button
variant="contained"
size="large"
component={LocaleLink}
to={routes.flashcardReview}
onClick={close}
sx={{ minHeight: 52, fontWeight: 700 }}
>
{t('flashcards.reviewStart')}
</Button>
<Button
size="large"
onClick={close}
sx={{ minHeight: 48, color: 'text.secondary', textTransform: 'none' }}
>
{t('flashcards.reviewLater')}
</Button>
</Stack>
</Stack>
</Dialog>
);
}
11 changes: 11 additions & 0 deletions src/features/flashcards/use-due-review-count.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client';

import { dayNumber, dueCardIds } from '@/constants/flashcards/srs.ts';
import { useFlashcardReviews } from '@/features/flashcards/use-flashcard-reviews.ts';

/** Cards due for review today. 0 during server rendering and hydration. */
export function useDueReviewCount() {
const { store } = useFlashcardReviews();

return dueCardIds(store, dayNumber()).length;
}
4 changes: 4 additions & 0 deletions src/i18n/messages/en/flashcards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export const flashcards = {
reviewHowTo:
'Look at the front and recall the answer. Tap the card to check it. If you got it, choose Got it: the card comes back after 3, 8, 20, 50... days. If not, choose Not yet: the card comes back tomorrow. For a card you know well, choose Know it well: it comes back after 6 months. Keys 1, 2, 3 pick a button.',
missesQueued: 'Cards you got wrong come back tomorrow in',
reviewLater: 'Later',
reviewDueBadge: '{count, plural, one {# card due today} other {# cards due today}}',
reviewPromptBody:
'{count, plural, one {# card is due today. A quick review keeps it fresh!} other {# cards are due today. A quick review keeps them fresh!}}',
reviewDueToday:
'{count, plural, =0 {No cards due today.} one {# card due today.} other {# cards due today.}}',
reviewStart: 'Review now',
Expand Down
3 changes: 3 additions & 0 deletions src/i18n/messages/vi/flashcards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export const flashcards = {
reviewHowTo:
'Nhìn mặt trước và tự nhớ đáp án. Bấm vào card để lật xem đáp án. Nếu nghĩ ra đúng thì chọn Đã nhớ, card sẽ quay lại sau 3, 8, 20, 50... ngày. Nếu không thì chọn Chưa nhớ, card sẽ quay lại vào ngày mai. Card đã thuộc lòng thì chọn Đã nhớ kĩ, card sẽ quay lại sau 6 tháng. Phím 1, 2, 3 để chọn nhanh.',
missesQueued: 'Các card trả lời sai sẽ được ôn lại vào ngày mai trong',
reviewLater: 'Để sau',
reviewDueBadge: '{count} card cần ôn hôm nay',
reviewPromptBody: 'Hôm nay có {count} card cần ôn. Ôn một chút để không quên nhé!',
reviewDueToday:
'{count, plural, =0 {Hôm nay không có card nào cần ôn.} other {Hôm nay có # card cần ôn.}}',
reviewStart: 'Ôn ngay',
Expand Down
Loading