Skip to content

Commit

Permalink
desc mission history, add table filters
Browse files Browse the repository at this point in the history
  • Loading branch information
danreeves committed Oct 10, 2023
1 parent 848811f commit 42cd662
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/data/missionInstances.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function getMissionHistory() {
return await prisma.missionInstance.findMany({
where: { start: { gte: dayAgoString } },
orderBy: {
start: "asc",
start: "desc",
},
})
}
Expand Down
166 changes: 150 additions & 16 deletions app/routes/mission-board.history.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Link, useLoaderData } from "@remix-run/react"
import { Link, useLoaderData, useSearchParams } from "@remix-run/react"
import type { LoaderArgs } from "@remix-run/server-runtime"
import { json } from "@remix-run/server-runtime"
import { getMissionHistory } from "~/data/missionInstances.server"

Expand All @@ -20,11 +21,76 @@ import { t } from "~/data/localization.server"
import { Img } from "~/components/Img"
import { Button } from "~/components/ui/button"
import { sideObjectiveToType } from "~/components/Mission"
import { ChevronDown } from "lucide-react"
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuTrigger,
} from "~/components/ui/dropdown-menu"

export async function loader({ request }: LoaderArgs) {
let searchParams = new URL(request.url).searchParams
let circumstanceFilter = searchParams.getAll("circumstance")
let sideMissionFilter = searchParams.getAll("sideMission")
let challengeFilter = searchParams.getAll("challenge")
let mapFilter = searchParams.getAll("map")

export async function loader() {
let rawMissions = await getMissionHistory()

let missions = rawMissions.map((mission) => {
let circumstances = Array.from(
new Set(rawMissions.map((mission) => mission.circumstance)),
)
.map((circumstance) => {
const template = CircumstanceTemplates[circumstance]
if (template) {
return {
value: circumstance,
label: t(template.display_name),
}
}
return false
})
.filter(Boolean)
.sort((a, b) => (a.label > b.label ? 1 : -1))

let maps = Array.from(new Set(rawMissions.map((mission) => mission.map)))
.map((map) => {
const template = getMissionTemplate(map)
if (template) {
return {
value: map,
label: t(template.display_name),
}
}
return false
})
.filter(Boolean)
.sort((a, b) => (a.label > b.label ? 1 : -1))

let filteredMissions = rawMissions
.filter((mission) => {
if (
!circumstanceFilter.length &&
!sideMissionFilter.length &&
!mapFilter.length
)
return true

return (
circumstanceFilter.includes(mission.circumstance) ||
sideMissionFilter.includes(
sideObjectiveToType(mission.sideMission ?? ""),
) ||
mapFilter.includes(mission.map)
)
})
.filter((mission) => {
if (!challengeFilter.length) return true
return challengeFilter.includes(mission.challenge.toString())
})

let missions = filteredMissions.map((mission) => {
const circumstance = CircumstanceTemplates[mission.circumstance]
const template = getMissionTemplate(mission.map)

Expand All @@ -45,7 +111,7 @@ export async function loader() {
}
})

return json({ missions })
return json({ missions, circumstances, maps })
}

type Column = (data: {
Expand All @@ -59,15 +125,6 @@ type Column = (data: {
sideMission: string | null
}) => ReactNode

let headers = [
"Difficulty",
"Map",
"Circumstance",
"Side objective",
"Started at",
"ID",
]

let columns: Column[] = [
({ category, challenge }) => (
<div className=" flex h-6 w-full justify-center gap-[2px]">
Expand Down Expand Up @@ -143,8 +200,85 @@ let columns: Column[] = [
),
]

function SearchParamsDropdownMenu({
label,
param,
items,
}: {
label: string
param: string
items: { label: string; value: string }[]
}) {
const [searchParams, setSearchParams] = useSearchParams()

return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="ml-auto">
{label} <ChevronDown className="ml-2 h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{items.map((item) => {
return (
<DropdownMenuCheckboxItem
key={item.value}
className="capitalize"
checked={searchParams.getAll(param).includes(item.value)}
onCheckedChange={(checked) => {
if (checked) {
searchParams.append(param, item.value)
} else {
searchParams.delete(param, item.value)
}
setSearchParams(searchParams.toString())
}}
>
{item.label}
</DropdownMenuCheckboxItem>
)
})}
</DropdownMenuContent>
</DropdownMenu>
)
}

export default function MissionHistory() {
const { missions } = useLoaderData<typeof loader>()
const { missions, circumstances, maps } = useLoaderData<typeof loader>()

let headers = [
<SearchParamsDropdownMenu
key="challenge"
param="challenge"
label="Difficulty"
items={[
{ label: "Damnation", value: "5" },
{ label: "Heresy", value: "4" },
{ label: "Malice", value: "3" },
{ label: "Uprising", value: "2" },
{ label: "Sedition", value: "1" },
]}
/>,
<SearchParamsDropdownMenu key="map" param="map" label="Map" items={maps} />,
<SearchParamsDropdownMenu
key="circumstance"
param="circumstance"
label="Circumstance"
items={circumstances}
/>,
<SearchParamsDropdownMenu
key="sideMission"
param="sideMission"
label="Side objective"
items={[
{ label: "Scriptures", value: "scripture" },
{ label: "Grimoire", value: "grimoire" },
]}
/>,
"Started at",
"ID",
]

return (
<div className="p-4 overflow-y-scroll">
<div className="max-w-7xl mx-auto mb-4 flex flex-row-reverse p-6">
Expand All @@ -156,8 +290,8 @@ export default function MissionHistory() {
<Table>
<TableHeader>
<TableRow>
{headers.map((col) => (
<TableHead key={col}>{col}</TableHead>
{headers.map((col, i) => (
<TableHead key={i}>{col}</TableHead>
))}
</TableRow>
</TableHeader>
Expand Down

0 comments on commit 42cd662

Please sign in to comment.