okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e82c70698
main
1/** 2* Which copy of a file the sidebar shows (#5180 follow-up: the preview kept a file's first content after the file changed). 3* 4* A preview tab can come from two places: the person (or a finished run) opening a file, which reads it from the host now, and the 5* message the file was made in, which carries the content it had then. They share one id (`file:<path>`). The rules, as pure functions: 6* - opening a file replaces the tab in place when the content differs, and keeps the very same array when it does not; 7* - a message's artifact only adds a tab that is missing, or replaces one that is OLDER than the message and different; it never 8* overwrites a copy that was read later; 9* - both return the array they were given when nothing changed, so no state update, no render and no effect follows: there is no loop to 10* start however often they are called (the same artifacts are handed over again whenever a message arrives). 11* The panel reloads when `previewVersion` changes, and only then. 12*/ 13import type { PreviewArtifact } from './artifacts' ; 14import { textRevision } from './preview-render' ; 15 16/** What identifies the content of a preview: the host's version for a file (modified time and size), else a hash of the text, else the address. */ 17export function previewVersion ( a :Pick < PreviewArtifact , 'version' | 'content' | 'url' >) :string { 18if ( a . content !== undefined ) 19return 'c' + textRevision ( a . content ); 20return a . version ?'v' + a . version :'u' + ( a . url ?? '' ); 21} 22 23const sameContent = ( a :PreviewArtifact , b :PreviewArtifact ) => a . kind === b . kind && a . title === b . title && previewVersion ( a ) === previewVersion ( b ); 24 25/** A file was opened (read from the host just now): it takes the place of its tab when it differs. */ 26export function openItem ( old :PreviewArtifact [], item :PreviewArtifact ) :PreviewArtifact [] { 27const at = old . findIndex ( a => a . id === item . id ); 28if ( at < 0 ) 29return [ ...old , item ]; 30if ( sameContent ( old [ at ], item )) 31return old ; 32const next = old . slice (); 33next [ at ] = item ; 34return next ; 35} 36 37/** 38* The artifacts the messages carry, merged into the tabs. `items` are in message order, each stamped with the time of its message (`at`); 39* `closed` are the ids the person closed. 40*/ 41export function syncArtifacts ( old :PreviewArtifact [], items :PreviewArtifact [], closed :readonly string [] | undefined = []) :PreviewArtifact [] { 42let next = old ; 43const edit = () => { if ( next === old ) next = old . slice (); }; 44for ( const item of items ) { 45const at = next . findIndex ( a => a . id === item . id ); 46if ( closed ?. includes ( item . id )) { 47if ( at >= 0 ) { edit (); next . splice ( at , 1 ); } 48continue ; 49} 50if ( at < 0 ) { 51edit (); 52next . push ( item ); 53continue ; 54} 55const have = next [ at ]; 56// only a message newer than the copy on show, with different content, replaces it 57if ( item . at !== undefined && have . at !== undefined && item . at > have . at && ! sameContent ( have , item )) { 58edit (); 59next [ at ] = item ; 60} 61} 62return next ; 63} 64 65/** The message artifacts of a session, in order and de-duplicated by id (the last of an id wins), each stamped with its message's time. */ 66export function messageArtifacts ( messages :ReadonlyArray <{ at ?:string ; artifacts ?:PreviewArtifact [] }> | undefined ) :PreviewArtifact [] { 67const byId = new Map < string , PreviewArtifact >(); 68for ( const m of messages ?? []) { 69const at = m . at ?Date . parse ( m . at ) :NaN ; 70for ( const a of m . artifacts ?? []) 71byId . set ( a . id , Number . isFinite ( at ) && a . at === undefined ?{ ...a , at} :a ); 72} 73return [ ...byId . values ()]; 74}