Skip to content

Commit 35bfdbd

Browse files
add legend component (#27)
* add legend component * add lucide-react and prettier ignore pnpm-lock * extract year colour code to file * implement requested changes for ExplorerLegend * minor syntax fixes ---------
1 parent 0f58d92 commit 35bfdbd

4 files changed

Lines changed: 57 additions & 13 deletions

File tree

src/components/CourseNode.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,14 @@
11
import { Handle, Position, type Node, type NodeProps } from '@xyflow/react';
22
import { useExplorerStore } from '@/store/explorerStore';
3+
import { yearColour } from '@/lib/yearColour';
34

45
export type CourseNodeData = { code: string; title: string };
56
export type CourseNodeType = Node<CourseNodeData>;
67

78
export default function CourseNode({ data }: NodeProps<CourseNodeType>) {
89
const { selectedCourse, highlightedSet } = useExplorerStore();
910
const dimmed = selectedCourse !== null && !highlightedSet.has(data.code);
10-
11-
const year = data.code.match(/\d/)?.[0];
12-
13-
const borderColour =
14-
year === '1'
15-
? 'border-green-500'
16-
: year === '2'
17-
? 'border-blue-500'
18-
: year === '3'
19-
? 'border-yellow-500'
20-
: year === '4'
21-
? 'border-red-500'
22-
: 'border-gray-300';
11+
const borderColour = yearColour(data.code);
2312

2413
return (
2514
<div

src/components/ExplorerLegend.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useState } from 'react';
2+
3+
import { YEAR_COLOUR, YEAR_COLOUR_DEFAULT } from '@/lib/yearColour';
4+
import { ChevronUp, ChevronDown } from 'lucide-react';
5+
6+
export default function ExplorerLegend() {
7+
const [open, setOpen] = useState(true);
8+
const rowClass = 'flex flex-row gap-2';
9+
const boxClass = 'w-9 h-4 border-2 self-center rounded';
10+
11+
return (
12+
<div className="text-sm absolute left-4 top-16 w-72 bg-white border border-gray-300 px-3 py-2 rounded-lg shadow-md z-1">
13+
<button
14+
type="button"
15+
onClick={() => setOpen(!open)}
16+
aria-expanded={open}
17+
className="flex w-full items-center gap-2"
18+
>
19+
{open ? <ChevronUp size={16} /> : <ChevronDown size={16} />}
20+
<span className="flex-1 text-left font-semibold">Legend</span>
21+
</button>
22+
23+
{open && (
24+
<div className="flex flex-col gap-1 border-t border-gray-300 pt-1 mt-2">
25+
{Object.entries(YEAR_COLOUR).map(([key, colour]) => (
26+
<div className={rowClass} key={key}>
27+
<div className={`${boxClass} ${colour}`}></div>
28+
<p>{`Year ${key} (${key}000-level)`}</p>
29+
</div>
30+
))}
31+
32+
<div className={rowClass}>
33+
<div className={`${boxClass} ${YEAR_COLOUR_DEFAULT}`}></div>
34+
<p>Other</p>
35+
</div>
36+
</div>
37+
)}
38+
</div>
39+
);
40+
}

src/lib/yearColour.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const YEAR_COLOUR_DEFAULT = 'border-gray-300';
2+
3+
export const YEAR_COLOUR: Record<string, string> = {
4+
'1': 'border-green-500',
5+
'2': 'border-blue-500',
6+
'3': 'border-yellow-500',
7+
'4': 'border-red-500',
8+
};
9+
10+
export function yearColour(code: string): string {
11+
const digit = code.match(/\d/)?.[0];
12+
return (digit && YEAR_COLOUR[digit]) ?? YEAR_COLOUR_DEFAULT;
13+
}

src/pages/Explorer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import CourseNode from '@/components/CourseNode';
2525
import type { CourseNodeData } from '@/components/CourseNode';
2626
import CourseDetailPanel from '@/components/CourseDetailPanel';
2727
import ExplorerSearch from '@/components/ExplorerSearch';
28+
import ExplorerLegend from '@/components/ExplorerLegend';
2829

2930
const NODE_W = 180;
3031
const NODE_H = 60;
@@ -108,6 +109,7 @@ export default function Explorer() {
108109
return (
109110
<div className="relative h-full w-full">
110111
<ExplorerSearch />
112+
<ExplorerLegend />
111113
<ReactFlow
112114
nodes={nodes}
113115
edges={edges}

0 commit comments

Comments
 (0)