@@ -12,6 +12,7 @@ const DEBOUNCE_MS = 500;
1212
1313interface DraftData {
1414 codeAnnotations : CodeAnnotation [ ] ;
15+ viewedFiles ?: string [ ] ;
1516 ts : number ;
1617}
1718
@@ -28,22 +29,24 @@ function formatTimeAgo(ts: number): string {
2829
2930interface UseCodeAnnotationDraftOptions {
3031 annotations : CodeAnnotation [ ] ;
32+ viewedFiles : Set < string > ;
3133 isApiMode : boolean ;
3234 submitted : boolean ;
3335}
3436
3537interface UseCodeAnnotationDraftResult {
36- draftBanner : { count : number ; timeAgo : string } | null ;
37- restoreDraft : ( ) => CodeAnnotation [ ] ;
38+ draftBanner : { count : number ; viewedCount : number ; timeAgo : string } | null ;
39+ restoreDraft : ( ) => { annotations : CodeAnnotation [ ] ; viewedFiles : string [ ] } ;
3840 dismissDraft : ( ) => void ;
3941}
4042
4143export function useCodeAnnotationDraft ( {
4244 annotations,
45+ viewedFiles,
4346 isApiMode,
4447 submitted,
4548} : UseCodeAnnotationDraftOptions ) : UseCodeAnnotationDraftResult {
46- const [ draftBanner , setDraftBanner ] = useState < { count : number ; timeAgo : string } | null > ( null ) ;
49+ const [ draftBanner , setDraftBanner ] = useState < { count : number ; viewedCount : number ; timeAgo : string } | null > ( null ) ;
4750 const draftDataRef = useRef < DraftData | null > ( null ) ;
4851 const timerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
4952 const hasMountedRef = useRef ( false ) ;
@@ -58,11 +61,14 @@ export function useCodeAnnotationDraft({
5861 return res . json ( ) ;
5962 } )
6063 . then ( ( data : DraftData | null ) => {
61- if ( data ?. codeAnnotations && Array . isArray ( data . codeAnnotations ) && data . codeAnnotations . length > 0 ) {
64+ const annotationCount = Array . isArray ( data ?. codeAnnotations ) ? data . codeAnnotations . length : 0 ;
65+ const viewedCount = Array . isArray ( data ?. viewedFiles ) ? data . viewedFiles . length : 0 ;
66+ if ( annotationCount > 0 || viewedCount > 0 ) {
6267 draftDataRef . current = data ;
6368 setDraftBanner ( {
64- count : data . codeAnnotations . length ,
65- timeAgo : formatTimeAgo ( data . ts || 0 ) ,
69+ count : annotationCount ,
70+ viewedCount,
71+ timeAgo : formatTimeAgo ( data ?. ts || 0 ) ,
6672 } ) ;
6773 }
6874 hasMountedRef . current = true ;
@@ -72,17 +78,18 @@ export function useCodeAnnotationDraft({
7278 } ) ;
7379 } , [ isApiMode ] ) ;
7480
75- // Debounced auto-save on annotation changes
81+ // Debounced auto-save on annotation/viewed changes
7682 useEffect ( ( ) => {
7783 if ( ! isApiMode || submitted ) return ;
7884 if ( ! hasMountedRef . current ) return ;
79- if ( annotations . length === 0 ) return ;
85+ if ( annotations . length === 0 && viewedFiles . size === 0 ) return ;
8086
8187 if ( timerRef . current ) clearTimeout ( timerRef . current ) ;
8288
8389 timerRef . current = setTimeout ( ( ) => {
8490 const payload : DraftData = {
8591 codeAnnotations : annotations ,
92+ viewedFiles : [ ...viewedFiles ] ,
8693 ts : Date . now ( ) ,
8794 } ;
8895
@@ -98,13 +105,16 @@ export function useCodeAnnotationDraft({
98105 return ( ) => {
99106 if ( timerRef . current ) clearTimeout ( timerRef . current ) ;
100107 } ;
101- } , [ annotations , isApiMode , submitted ] ) ;
108+ } , [ annotations , viewedFiles , isApiMode , submitted ] ) ;
102109
103110 const restoreDraft = useCallback ( ( ) => {
104111 const data = draftDataRef . current ;
105112 setDraftBanner ( null ) ;
106113 draftDataRef . current = null ;
107- return data ?. codeAnnotations ?? [ ] ;
114+ return {
115+ annotations : data ?. codeAnnotations ?? [ ] ,
116+ viewedFiles : data ?. viewedFiles ?? [ ] ,
117+ } ;
108118 } , [ ] ) ;
109119
110120 const dismissDraft = useCallback ( ( ) => {
0 commit comments