Repository navigation
feat(reports): open new reports between dates, with limits on activations #1527
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
28abe59
feat(reports): mock New reports switch with dates and activation limits
sickerine b4cde5a
feat(reports): open new reports between dates, with limits on activat…
sickerine cdcb5e1
fix(reports): new reports settings from review
sickerine ae5888d
perf(reports): work out a limit's period start once, not per activation
sickerine 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| "use client"; | ||
|
|
||
| // What the Publish page says while new reports are closed (lib/reports/intake.ts): above the list, on a draft, on a | ||
| // new report, and as the reason beside a Next or Activate that waits. | ||
|
|
||
| import { createContext, useContext, useSyncExternalStore } from "react"; | ||
| import { useFormatter, useTranslations } from "next-intl"; | ||
| import { Ban, CalendarClock, FileText, Hourglass } from "@/components/icons"; | ||
| import { ListEmpty } from "@/components/evidence-libraries/list-controls"; | ||
| import { StatusChip } from "@/components/evidence-libraries/configuration/status-chip"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import type { IntakeState } from "@/lib/reports/intake"; | ||
| import { cn } from "@/lib/utils"; | ||
|
|
||
| type Closed = Exclude<IntakeState, { open: true }>; | ||
|
|
||
| // The clock, read every 30 seconds: a period that starts or ends while a page is open changes it there. | ||
| const TICK = 30_000; | ||
| function subscribeClock(listener: () => void) { | ||
| const timer = window.setInterval(listener, TICK); | ||
| return () => window.clearInterval(timer); | ||
| } | ||
| const tick = () => Math.floor(Date.now() / TICK) * TICK; | ||
|
|
||
| /** Now, to the last 30 seconds. */ | ||
| export function useNow(): number { | ||
| return useSyncExternalStore(subscribeClock, tick, tick); | ||
| } | ||
|
|
||
| /** Whether new reports are open on this page, worked out once where the page reads its settings (PublishFlow). */ | ||
| export const IntakeOpen = createContext<IntakeState>({ open: true }); | ||
| /** The reason a draft's Next waits, given to every Next of a draft (NextButton). Null while a draft can go on. */ | ||
| export const IntakeShut = createContext<string | null>(null); | ||
| export const useShutNext = () => useContext(IntakeShut); | ||
|
|
||
| const useWhen = () => { | ||
| const format = useFormatter(); | ||
| return (at: number) => format.dateTime(at, { weekday: "short", day: "numeric", month: "short", hour: "2-digit", minute: "2-digit" }); | ||
| }; | ||
|
|
||
| /** Why a new report can't start or a draft go on now, in a few words for beside the button; null while they can. `given` is for the page that provides it. */ | ||
| export function useIntakeShut(given?: IntakeState): string | null { | ||
| const t = useTranslations("publishReports.flow.intake.shut"); | ||
| const when = useWhen(); | ||
| const read = useContext(IntakeOpen); | ||
| const state = given ?? read; | ||
| if (state.open) return null; | ||
| if (state.why === "off") return t("off"); | ||
| if (state.why === "notYet") return t("notYet", { date: when(state.at) }); | ||
| if (state.why === "full" && state.reopensAt !== null) return t("next", { date: when(state.reopensAt) }); | ||
| return t("over"); | ||
| } | ||
|
|
||
| /** The title and words for a closed state, and the date its chip gives, if any. */ | ||
| function useIntakeWords(state: Closed) { | ||
| const t = useTranslations("publishReports.flow.intake"); | ||
| const when = useWhen(); | ||
| if (state.why === "off") return { icon: Ban, title: t("off.title"), body: t("off.body"), chip: null }; | ||
| if (state.why === "notYet") return { icon: CalendarClock, title: t("notYet.title"), body: t("notYet.body"), chip: t("opens", { date: when(state.at) }) }; | ||
| if (state.why === "ended") return { icon: Hourglass, title: t("over.title"), body: t("ended.body"), chip: t("closed", { date: when(state.at) }) }; | ||
| return { icon: Hourglass, title: t("over.title"), body: t(state.reopensAt === null ? "full.last" : "full.body"), chip: state.reopensAt === null ? null : t("next", { date: when(state.reopensAt) }) }; | ||
| } | ||
|
|
||
| /** The notice above the list or a draft: what is closed, why, and when it opens again. */ | ||
| export function IntakeNotice({ className }: { className?: string }) { | ||
| const state = useContext(IntakeOpen); | ||
| if (state.open) return null; | ||
| return <ClosedNotice state={state} className={className} />; | ||
| } | ||
|
|
||
| function ClosedNotice({ state, className }: { state: Closed; className?: string }) { | ||
| const { icon: Icon, title, body, chip } = useIntakeWords(state); | ||
| return ( | ||
| <div role="status" className={cn("flex flex-wrap items-center gap-x-4 gap-y-2.5 rounded-2xl bg-inset px-4 py-3", className)}> | ||
| <span className="flex min-w-0 flex-1 basis-64 items-start gap-3"> | ||
| <Icon className="mt-0.5 size-4 shrink-0 ink-warning" /> | ||
| <span className="min-w-0"> | ||
| <span className="block text-sm font-medium">{title}</span> | ||
| <span className="block text-sm text-muted-foreground">{body}</span> | ||
| </span> | ||
| </span> | ||
| {chip ? ( | ||
| <StatusChip tone="neutral" icon={CalendarClock} className="shrink-0"> | ||
| {chip} | ||
| </StatusChip> | ||
| ) : null} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| /** The Publish page's new report while new reports are closed: why, when they open again, and the way to the list. */ | ||
| export function IntakeClosedPage({ hasReports, onReports }: { hasReports: boolean; onReports: () => void }) { | ||
| const state = useContext(IntakeOpen); | ||
| if (state.open) return null; | ||
| return <ClosedPage state={state} hasReports={hasReports} onReports={onReports} />; | ||
| } | ||
|
|
||
| function ClosedPage({ state, hasReports, onReports }: { state: Closed; hasReports: boolean; onReports: () => void }) { | ||
| const t = useTranslations("publishReports.flow"); | ||
| const { icon, title, body, chip } = useIntakeWords(state); | ||
| return ( | ||
| <ListEmpty | ||
| icon={icon} | ||
| description={body} | ||
| action={ | ||
| <div className="flex flex-wrap items-center justify-center gap-2"> | ||
| {chip ? ( | ||
| <StatusChip tone="neutral" icon={CalendarClock}> | ||
| {chip} | ||
| </StatusChip> | ||
| ) : null} | ||
| {hasReports ? ( | ||
| <Button size="sm" variant="outline" onClick={onReports}> | ||
| <FileText /> {t("yourReports")} | ||
| </Button> | ||
| ) : null} | ||
| </div> | ||
| } | ||
| > | ||
| {title} | ||
| </ListEmpty> | ||
| ); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.