@@ -263,6 +263,81 @@ pub fn LibraryEditor(
263263 }
264264 } ;
265265
266+ // A copy of the list being edited, named after it: last week's set as
267+ // the start of this week's.
268+ let duplicate = {
269+ let library = library. clone ( ) ;
270+ move |( ) | {
271+ let Some ( id) = selected ( ) else { return } ;
272+ let Some ( from_list) = lists. peek ( ) . iter ( ) . find ( |l| l. id == id) . cloned ( ) else {
273+ return ;
274+ } ;
275+ let title = format ! ( "{} (copy)" , from_list. title) ;
276+ let library = library. clone ( ) ;
277+ spawn ( async move {
278+ match off_thread ( move || {
279+ with_library ( & library, |l| async move {
280+ l. duplicate_list ( & from_list, & title) . await
281+ } )
282+ } )
283+ . await
284+ {
285+ Some ( Ok ( copy) ) => {
286+ let id = copy. id . clone ( ) ;
287+ let title = copy. title . clone ( ) ;
288+ lists. write ( ) . push ( copy) ;
289+ selected. set ( Some ( id) ) ;
290+ renaming. set ( Some ( title) ) ;
291+ deleting. set ( false ) ;
292+ }
293+ Some ( Err ( e) ) => problem. set ( Some ( brief ( & e) ) ) ,
294+ None => { }
295+ }
296+ } ) ;
297+ }
298+ } ;
299+ // A song the library does not have yet, made from what was searched for
300+ // and put straight in the list being edited.
301+ let make_song = {
302+ let library = library. clone ( ) ;
303+ let edit = edit. clone ( ) ;
304+ move |title : String | {
305+ let title = title. trim ( ) . to_owned ( ) ;
306+ if title. is_empty ( ) {
307+ return ;
308+ }
309+ let library = library. clone ( ) ;
310+ let mut edit = edit. clone ( ) ;
311+ spawn ( async move {
312+ match off_thread ( move || {
313+ with_library (
314+ & library,
315+ |l| async move { l. create_song ( & title, "" , & [ ] ) . await } ,
316+ )
317+ } )
318+ . await
319+ {
320+ Some ( Ok ( song) ) => {
321+ if let Load :: Ready ( all) = & mut * songs. write ( ) {
322+ all. push ( ( song. clone ( ) , false ) ) ;
323+ all. sort_by ( |a, b| {
324+ a. 0 . title . to_lowercase ( ) . cmp ( & b. 0 . title . to_lowercase ( ) )
325+ } ) ;
326+ }
327+ query. set ( String :: new ( ) ) ;
328+ edit ( & move |list : & mut Vec < LibrarySong > | {
329+ if !list. iter ( ) . any ( |s| s. slug == song. slug ) {
330+ list. push ( song. clone ( ) ) ;
331+ }
332+ } ) ;
333+ }
334+ Some ( Err ( e) ) => problem. set ( Some ( brief ( & e) ) ) ,
335+ None => { }
336+ }
337+ } ) ;
338+ }
339+ } ;
340+
266341 let all_lists = lists ( ) ;
267342 let current = selected ( ) . and_then ( |id| all_lists. iter ( ) . find ( |l| l. id == id) . cloned ( ) ) ;
268343 let in_current: Vec < String > = current
@@ -304,6 +379,14 @@ pub fn LibraryEditor(
304379 . filter ( |l| l. kind == ListKind :: Songs )
305380 . cloned ( )
306381 . collect ( ) ;
382+ // What was typed, and whether the library has a song called exactly that.
383+ let typed = query ( ) . trim ( ) . to_owned ( ) ;
384+ let exact = match & * songs. read ( ) {
385+ Load :: Ready ( all) => all
386+ . iter ( )
387+ . any ( |( s, _) | s. title . eq_ignore_ascii_case ( & typed) ) ,
388+ _ => true ,
389+ } ;
307390 let status = match ( problem ( ) , saving) {
308391 ( Some ( why) , _) => ( WARN , format ! ( "Not saved — {why}" ) ) ,
309392 ( None , true ) => ( DIM , "Saving…" . to_owned ( ) ) ,
@@ -421,6 +504,7 @@ pub fn LibraryEditor(
421504 "Play"
422505 }
423506 }
507+ Pill { label: "Duplicate" , primary: false , on_press: duplicate. clone( ) }
424508 Pill {
425509 label: if deleting( ) { "Tap again to delete" } else { "Delete" } ,
426510 primary: false ,
@@ -492,6 +576,21 @@ pub fn LibraryEditor(
492576 oninput: move |e| query. set( e. value( ) ) ,
493577 }
494578 }
579+ // Not in the library: made here, into the list.
580+ if !typed. is_empty( ) && !exact && current. is_some( ) {
581+ button {
582+ style: "display:flex; align-items:center; gap:10px; min-height:44px; padding:0 12px; border-radius:10px; \
583+ border:1px dashed {ACCENT}; background:transparent; color:{TEXT}; font-family:inherit; \
584+ font-size:14px; text-align:left; cursor:pointer;",
585+ onclick: {
586+ let make_song = make_song. clone( ) ;
587+ let typed = typed. clone( ) ;
588+ move |_| make_song( typed. clone( ) )
589+ } ,
590+ Plus { size: 16 , color: ACCENT }
591+ span { style: "flex:1; min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;" , "New song \u{201c} {typed}\u{201d} " }
592+ }
593+ }
495594 match source {
496595 Load :: Waiting => rsx! { span { style: "font-size:13px; color:{DIM}; padding:6px 4px;" , "Reading the library…" } } ,
497596 Load :: Failed ( why) => rsx! { span { style: "font-size:13px; color:{WARN}; padding:6px 4px;" , "Could not read the songs — {why}" } } ,
@@ -704,3 +803,186 @@ fn IconButton(
704803 }
705804 }
706805}
806+
807+ /// A song of the library, made or changed: its title, key and writers —
808+ /// and, for one that is there already, deleting it. Whole-page, as the
809+ /// list editor is; `song` `None` makes a new one.
810+ #[ component]
811+ pub fn SongEditor (
812+ library : Library ,
813+ song : Option < LibrarySong > ,
814+ /// Done: saved, deleted, or left.
815+ on_done : EventHandler < ( ) > ,
816+ ) -> Element {
817+ let is_new = song. is_none ( ) ;
818+ let title = use_signal ( || song. as_ref ( ) . map ( |s| s. title . clone ( ) ) . unwrap_or_default ( ) ) ;
819+ let key = use_signal ( || song. as_ref ( ) . map ( |s| s. key . clone ( ) ) . unwrap_or_default ( ) ) ;
820+ let writers = use_signal ( || {
821+ song. as_ref ( )
822+ . map ( |s| s. writers . join ( ", " ) )
823+ . unwrap_or_default ( )
824+ } ) ;
825+ let mut busy = use_signal ( || false ) ;
826+ let mut deleting = use_signal ( || false ) ;
827+ let mut problem = use_signal ( || None :: < String > ) ;
828+
829+ let save = {
830+ let library = library. clone ( ) ;
831+ let song = song. clone ( ) ;
832+ move |( ) | {
833+ if busy ( ) {
834+ return ;
835+ }
836+ let name = title ( ) . trim ( ) . to_owned ( ) ;
837+ if name. is_empty ( ) {
838+ problem. set ( Some ( "A song needs a title." . to_owned ( ) ) ) ;
839+ return ;
840+ }
841+ let list: Vec < String > = writers ( )
842+ . split ( ',' )
843+ . map ( str:: trim)
844+ . filter ( |w| !w. is_empty ( ) )
845+ . map ( str:: to_owned)
846+ . collect ( ) ;
847+ let key_now = key ( ) . trim ( ) . to_owned ( ) ;
848+ let library = library. clone ( ) ;
849+ let was = song. clone ( ) ;
850+ busy. set ( true ) ;
851+ spawn ( async move {
852+ let outcome = off_thread ( move || {
853+ with_library ( & library, |l| async move {
854+ match was {
855+ Some ( was) => {
856+ l. update_song ( & LibrarySong {
857+ title : name,
858+ key : key_now,
859+ writers : list,
860+ ..was
861+ } )
862+ . await
863+ }
864+ None => l. create_song ( & name, & key_now, & list) . await . map ( drop) ,
865+ }
866+ } )
867+ } )
868+ . await ;
869+ busy. set ( false ) ;
870+ match outcome {
871+ Some ( Ok ( ( ) ) ) => on_done. call ( ( ) ) ,
872+ Some ( Err ( e) ) => problem. set ( Some ( brief ( & e) ) ) ,
873+ None => { }
874+ }
875+ } ) ;
876+ }
877+ } ;
878+ let delete = {
879+ let library = library. clone ( ) ;
880+ let song = song. clone ( ) ;
881+ move |( ) | {
882+ let Some ( song) = song. clone ( ) else { return } ;
883+ if !deleting ( ) {
884+ deleting. set ( true ) ;
885+ return ;
886+ }
887+ let library = library. clone ( ) ;
888+ busy. set ( true ) ;
889+ spawn ( async move {
890+ let slug = song. slug . clone ( ) ;
891+ let outcome = off_thread ( move || {
892+ with_library ( & library, |l| async move { l. delete_song ( & slug) . await } )
893+ } )
894+ . await ;
895+ busy. set ( false ) ;
896+ match outcome {
897+ Some ( Ok ( ( ) ) ) => on_done. call ( ( ) ) ,
898+ Some ( Err ( e) ) => problem. set ( Some ( brief ( & e) ) ) ,
899+ None => { }
900+ }
901+ } ) ;
902+ }
903+ } ;
904+
905+ let field = |label : & ' static str , hint : & ' static str , value : Signal < String > | {
906+ let mut value = value;
907+ rsx ! {
908+ label {
909+ style: "display:flex; flex-direction:column; gap:6px;" ,
910+ span { style: "font-size:12px; font-weight:650; color:{DIM};" , "{label}" }
911+ div {
912+ style: "position:relative; height:44px;" ,
913+ if value( ) . is_empty( ) {
914+ span {
915+ style: "position:absolute; top:0; left:12px; height:44px; display:flex; align-items:center; \
916+ font-size:15px; color:#6b7280; pointer-events:none;",
917+ "{hint}"
918+ }
919+ }
920+ input {
921+ style: "position:absolute; top:0; left:0; width:100%; height:44px; box-sizing:border-box; padding:0 12px; \
922+ border-radius:10px; border:1px solid {RULE}; background:#101216; color:{TEXT}; \
923+ font-family:inherit; font-size:15px;",
924+ r#type: "text" ,
925+ value: "{value}" ,
926+ oninput: move |e| value. set( e. value( ) ) ,
927+ }
928+ }
929+ }
930+ }
931+ } ;
932+ let heading = if is_new { "New song" } else { "Song" } ;
933+ let save_label = if busy ( ) {
934+ "Saving…"
935+ } else if is_new {
936+ "Make song"
937+ } else {
938+ "Save"
939+ } ;
940+
941+ rsx ! {
942+ div {
943+ style: "position:absolute; inset:0; display:flex; flex-direction:column; background:{BG}; color:{TEXT}; \
944+ font-family:system-ui, -apple-system, sans-serif;",
945+ div {
946+ style: "flex:none; display:flex; align-items:center; gap:12px; padding:{TOP}px 16px 12px; \
947+ border-bottom:1px solid {RULE}; background:{BAR};",
948+ button {
949+ style: "height:36px; display:flex; align-items:center; gap:6px; padding:0 12px 0 8px; border-radius:9px; \
950+ border:1px solid {RULE}; background:#1c1e22; color:{TEXT}; font-family:inherit; font-size:14px; cursor:pointer;",
951+ onclick: move |_| on_done. call( ( ) ) ,
952+ ChevronLeft { size: 18 , color: TEXT }
953+ "Back"
954+ }
955+ span { style: "flex:1; min-width:0; font-size:17px; font-weight:700;" , "{heading}" }
956+ Pill { label: save_label, primary: true , on_press: save }
957+ }
958+ div {
959+ style: "flex:1; min-height:0; overflow-y:auto;" ,
960+ div {
961+ style: "box-sizing:border-box; width:100%; max-width:560px; padding:20px 20px 40px; \
962+ display:flex; flex-direction:column; gap:18px;",
963+ { field( "Title" , "What the song is called" , title) }
964+ { field( "Key" , "The key it is usually played in — G, Bb, F#m" , key) }
965+ { field( "Writers" , "Who wrote it, separated by commas" , writers) }
966+ if let Some ( why) = problem( ) {
967+ span { style: "font-size:13px; color:{WARN}; line-height:1.45;" , "{why}" }
968+ }
969+ if is_new {
970+ span {
971+ style: "font-size:13px; color:{DIM}; line-height:1.5;" ,
972+ "A new song can go in setlists straight away. It plays once its session is made — its tracks, chart and lyrics — from REAPER or the desktop app."
973+ }
974+ } else {
975+ div {
976+ style: "display:flex; margin-top:10px;" ,
977+ Pill {
978+ label: if deleting( ) { "Tap again to delete this song" } else { "Delete song" } ,
979+ primary: false ,
980+ on_press: delete,
981+ }
982+ }
983+ }
984+ }
985+ }
986+ }
987+ }
988+ }
0 commit comments