1+ import { StorageManager } from './storage-manager.js' ;
2+
13// Recap interactions: year dropdown + navigation
24document . addEventListener ( 'DOMContentLoaded' , ( ) => {
35 const wrapper = document . getElementById ( 'yearSelectorWrapper' ) ;
@@ -37,7 +39,8 @@ document.addEventListener('DOMContentLoaded', () => {
3739 const Timeline = document . getElementById ( 'recapTimeline' ) ;
3840
3941 if ( sortToggle && Timeline ) {
40- let isNewestFirst = true ; // Default matches backend
42+ // Read from storage, default to true (Newest First)
43+ let isNewestFirst = StorageManager . get ( StorageManager . KEYS . RECAP_SORT_ORDER , true ) ;
4144
4245 // Icons
4346 // Newest First (Sort Descending): Lines + Arrow Down
@@ -50,40 +53,47 @@ document.addEventListener('DOMContentLoaded', () => {
5053 // Arrow: M18 18V6M15 9l3-3 3 3
5154 const iconOldest = `<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 7h5M4 12h8M4 17h8M18 18V6M15 9l3-3 3 3"></path>` ;
5255
53- sortToggle . addEventListener ( 'click' , ( ) => {
54- isNewestFirst = ! isNewestFirst ;
55-
56- // Update SVG icon
56+ // Function to update the button UI
57+ const updateUI = ( ) => {
5758 const svg = sortToggle . querySelector ( 'svg' ) ;
5859 if ( svg ) {
5960 svg . innerHTML = isNewestFirst ? iconNewest : iconOldest ;
6061 }
61-
62- // Update title
6362 sortToggle . title = isNewestFirst ? "Current: Newest First" : "Current: Oldest First" ;
63+ } ;
6464
65+ // Function to flip the DOM order
66+ const flipOrder = ( ) => {
6567 // 1. Reorder Month Groups
6668 const months = Array . from ( Timeline . querySelectorAll ( '.month-group' ) ) ;
6769 months . reverse ( ) . forEach ( month => Timeline . appendChild ( month ) ) ;
6870
6971 // 2. Reorder Items within each Month Group (keep header at top)
7072 months . forEach ( month => {
71- // The first child is the "month header" (.recap-event with month label)
72- // subsequent children are book items
73- // We only want to reverse the book items, keeping the header first.
7473 const children = Array . from ( month . children ) ;
7574 if ( children . length > 1 ) {
7675 const header = children [ 0 ] ; // first element is month title
7776 const items = children . slice ( 1 ) ; // the rest are books
7877
79- // Clear content
8078 month . innerHTML = '' ;
81- // Apppend header back
8279 month . appendChild ( header ) ;
83- // Append reversed items
8480 items . reverse ( ) . forEach ( item => month . appendChild ( item ) ) ;
8581 }
8682 } ) ;
83+ } ;
84+
85+ // Apply initial state if different from default (Newest First)
86+ if ( ! isNewestFirst ) {
87+ updateUI ( ) ;
88+ flipOrder ( ) ;
89+ }
90+
91+ sortToggle . addEventListener ( 'click' , ( ) => {
92+ isNewestFirst = ! isNewestFirst ;
93+ StorageManager . set ( StorageManager . KEYS . RECAP_SORT_ORDER , isNewestFirst ) ;
94+
95+ updateUI ( ) ;
96+ flipOrder ( ) ;
8797 } ) ;
8898 }
8999} ) ;
0 commit comments