11import { h , VNode } from "preact" ;
22import { useMemo , useState } from "preact/hooks" ;
3- import { Span } from "../annotate" ;
3+ import { ColorLabel , Span } from "../annotate" ;
44
55import TopBar from "./TopBar" ;
66import Highlightable from "./Highlightable" ;
7+ import { HIGHLIGHT_COLORS } from "./colors" ;
78
89interface Props {
910 docs : string [ ] ;
@@ -19,7 +20,7 @@ export default function Annotate({
1920 onUpdateSpans,
2021} : Props ) : VNode {
2122 const totalDocs = docs . length ;
22- const [ selectedLabel , setSelectedLabel ] = useState < string > ( "" ) ;
23+ const [ selectedLabel , setSelectedLabel ] = useState < ColorLabel > ( ) ;
2324 const [ docIndex , setDocIndex ] = useState < number > ( 0 ) ;
2425 const [ docSpans , setDocSpans ] = useState < Span [ ] [ ] > (
2526 initialSpans . length
@@ -31,7 +32,7 @@ export default function Annotate({
3132 return docs [ docIndex ] ;
3233 } , [ docIndex , docs ] ) ;
3334
34- const onChangeLabel = ( label : string ) => {
35+ const onChangeLabel = ( label : ColorLabel ) => {
3536 setSelectedLabel ( label ) ;
3637 } ;
3738
@@ -48,15 +49,25 @@ export default function Annotate({
4849
4950 const spans = docSpans [ docIndex ] || [ ] ;
5051
52+ const coloredLabels = useMemo ( ( ) => {
53+ const colors = Object . keys ( HIGHLIGHT_COLORS ) ;
54+ return labels . map ( ( text , index ) => ( {
55+ text,
56+ color : colors [ index % colors . length ] ,
57+ } ) ) ;
58+ } , [ labels ] ) ;
59+
60+ const activeLabel = selectedLabel || coloredLabels [ 0 ] ;
61+
5162 return h ( "div" , null , [
5263 h ( TopBar , {
53- selectedLabel,
54- labels,
64+ selectedLabel : activeLabel ,
65+ labels : coloredLabels ,
5566 totalDocs,
5667 docIndex,
5768 onChangeLabel,
5869 onChangeNav,
5970 } ) ,
60- h ( Highlightable , { text, selectedLabel, spans, onUpdate } ) ,
71+ h ( Highlightable , { text, selectedLabel : activeLabel , spans, onUpdate } ) ,
6172 ] ) ;
6273}
0 commit comments