@@ -45,9 +45,15 @@ export interface PendingReviewCommentRange extends Pick<
4545 readonly rowId : string ;
4646}
4747
48+ /** The sheet, thread and section a selection was made in, and the diff it was made over. */
49+ export interface ReviewCommentSelectionScope {
50+ readonly key : string ;
51+ /** Compared by `===`: a git section's diff hash, or a turn's diff as answered. */
52+ readonly content : string | null ;
53+ }
54+
4855export interface ReviewCommentSelection {
49- /** Identifies the sheet, thread and section the selection was made in. */
50- readonly scope : string | null ;
56+ readonly scope : ReviewCommentSelectionScope | null ;
5157 readonly target : ReviewCommentTarget | null ;
5258 readonly pending : PendingReviewCommentRange | null ;
5359}
@@ -86,46 +92,64 @@ export function nextReviewSheetId(): number {
8692export function reviewCommentSelectionScope (
8793 sheetId : number ,
8894 threadKey : string | null ,
89- sectionId : string | null ,
90- ) : string | null {
91- return threadKey === null || sectionId === null
95+ section : { readonly id : string ; readonly content : string | null } | null ,
96+ ) : ReviewCommentSelectionScope | null {
97+ return threadKey === null || section === null
9298 ? null
93- : JSON . stringify ( [ sheetId , threadKey , sectionId ] ) ;
99+ : { key : JSON . stringify ( [ sheetId , threadKey , section . id ] ) , content : section . content } ;
100+ }
101+
102+ function isSameScope (
103+ a : ReviewCommentSelectionScope | null ,
104+ b : ReviewCommentSelectionScope | null ,
105+ ) : boolean {
106+ return a !== null && b !== null && a . key === b . key && a . content === b . content ;
94107}
95108
96- export function setReviewCommentTarget ( scope : string , target : ReviewCommentTarget ) {
109+ export function setReviewCommentTarget (
110+ scope : ReviewCommentSelectionScope ,
111+ target : ReviewCommentTarget ,
112+ ) {
97113 setSelection ( { scope, target, pending : null } ) ;
98114}
99115
100116/**
101117 * Keeps the pending range: when both ends are the same line the target is a single line, and
102118 * the range still waits for a different end.
103119 */
104- export function completeReviewCommentRange ( scope : string , target : ReviewCommentTarget ) {
120+ export function completeReviewCommentRange (
121+ scope : ReviewCommentSelectionScope ,
122+ target : ReviewCommentTarget ,
123+ ) {
105124 setSelection ( {
106125 scope,
107126 target,
108- pending : selection . scope === scope ? selection . pending : null ,
127+ pending : isSameScope ( selection . scope , scope ) ? selection . pending : null ,
109128 } ) ;
110129}
111130
112- export function startReviewCommentRange ( scope : string , pending : PendingReviewCommentRange ) {
131+ export function startReviewCommentRange (
132+ scope : ReviewCommentSelectionScope ,
133+ pending : PendingReviewCommentRange ,
134+ ) {
113135 setSelection ( { scope, target : null , pending } ) ;
114136}
115137
116138export function clearReviewCommentTarget ( ) {
117139 setSelection ( EMPTY_SELECTION ) ;
118140}
119141
120- /** A selection made in another sheet, thread or section reads as empty. */
142+ /** A selection made in another sheet, thread or section, or over another diff, reads as empty. */
121143export function selectReviewCommentSelection (
122144 current : ReviewCommentSelection ,
123- scope : string | null ,
145+ scope : ReviewCommentSelectionScope | null ,
124146) : ReviewCommentSelection {
125- return scope !== null && current . scope === scope ? current : EMPTY_SELECTION ;
147+ return isSameScope ( current . scope , scope ) ? current : EMPTY_SELECTION ;
126148}
127149
128- export function useReviewCommentSelection ( scope : string | null ) : ReviewCommentSelection {
150+ export function useReviewCommentSelection (
151+ scope : ReviewCommentSelectionScope | null ,
152+ ) : ReviewCommentSelection {
129153 return selectReviewCommentSelection (
130154 useSyncExternalStore (
131155 subscribeReviewCommentSelection ,
0 commit comments