@@ -2,15 +2,18 @@ use futures::future::{join_all, OptionFuture};
22use github_api:: models:: milestone:: OpenOrClosed ;
33use jiff:: Timestamp ;
44use leptos:: prelude:: * ;
5+ use macros:: zwang_url;
6+ use crate :: app:: routing:: * ;
57use shared:: {
68 avail:: Avail ,
9+ sync_engine:: optimistic:: db:: MaybeOptimistic ,
710 types:: {
811 issue:: { self , Issue } ,
912 issue_comment:: { IssueComment , IssueIdIndex } ,
1013 user:: User ,
1114 } ,
1215} ;
13- use zwang_router:: { ArgFromParent , RouteParams } ;
16+ use zwang_router:: { set_pathname_untracked , ArgFromParent , RouteParams } ;
1417
1518use crate :: {
1619 app:: {
@@ -39,34 +42,76 @@ pub fn OneIssue(
3942 repo_name,
4043 } ) : RouteParams < ParamsIssueNumberOwnerNameRepoName > ,
4144) -> impl IntoView {
45+ let prev_optimistic_id = RwSignal :: new ( None ) ;
4246 let issue_number = move || issue_number. get ( ) . parse :: < i64 > ( ) ;
4347 move || {
4448 let sync_engine = use_sync_engine ( ) ;
4549 let issue_number = match issue_number ( ) {
4650 Ok ( i) => i,
4751 Err ( _) => return view ! { <NotFound /> } . into_any ( ) ,
4852 } ;
53+ let sync_engine2 = sync_engine. clone ( ) ;
4954 let issue_and_user = sync_engine. idb_signal (
5055 move |txn| txn. with_store :: < User > ( ) . with_store :: < Issue > ( ) . build ( ) ,
51- move |txn| async move {
52- let issue = txn
53- . object_store :: < Issue > ( ) ?
54- . index :: < issue:: RepositoryIdIndex > ( ) ?
55- . get_all_optimistically ( Some ( & repository_page_context. read ( ) . repository . id ) )
56- . await ?
57- . into_iter ( )
58- . find ( move |i| i. number == issue_number) ;
56+ move |txn| {
57+ let sync_engine2 = sync_engine2. clone ( ) ;
58+ async move {
59+ let issue = txn
60+ . object_store :: < Issue > ( ) ?
61+ . index :: < issue:: RepositoryIdIndex > ( ) ?
62+ . get_all_optimistically ( Some ( & repository_page_context. read ( ) . repository . id ) )
63+ . await ?
64+ . into_iter ( )
65+ . find ( move |i| i. number == issue_number) ;
5966
60- Ok ( if let Some ( issue) = issue {
61- let user = if let Avail :: Yes ( Some ( user_id) ) = issue. user_id {
62- txn. object_store :: < User > ( ) ?. get_optimistically ( & user_id) . await ?
67+ let issue = if let Some ( issue) = issue {
68+ Some ( issue)
6369 } else {
64- None
70+ if let Some ( prev_optimistic_id) = prev_optimistic_id. get_untracked ( ) {
71+ if let Some ( realistic_id) = sync_engine2
72+ . db
73+ . get_optimistic_to_realistic_for_creations :: < Issue > (
74+ & prev_optimistic_id,
75+ )
76+ {
77+ let issue = txn. object_store :: < Issue > ( ) ?
78+ . get ( & realistic_id)
79+ . await ?
80+ . map ( |issue| MaybeOptimistic :: new ( issue, false ) ) ;
81+
82+ if let Some ( issue) = & issue {
83+ set_pathname_untracked ( & zwang_url ! ( "/owner_name={owner_name.get()}/repo_name={repo_name.get()}/issues/issue_number={issue.number.to_string()}" ) ) ;
84+ }
85+
86+ issue
87+ } else {
88+ None
89+ }
90+ } else {
91+ None
92+ }
6593 } ;
66- Some ( ( issue, user) )
67- } else {
68- None
69- } )
94+
95+ Ok ( if let Some ( issue) = issue {
96+ if issue. is_optimistic {
97+ * prev_optimistic_id. write_untracked ( ) = Some ( issue. id ) ;
98+ } else {
99+ * prev_optimistic_id. write_untracked ( ) = None ;
100+ }
101+
102+ let user = if let Avail :: Yes ( Some ( user_id) ) = issue. user_id {
103+ txn. object_store :: < User > ( ) ?
104+ . get_optimistically ( & user_id)
105+ . await ?
106+ } else {
107+ None
108+ } ;
109+ Some ( ( issue, user) )
110+ } else {
111+ * prev_optimistic_id. write_untracked ( ) = None ;
112+ None
113+ } )
114+ }
70115 } ,
71116 ) ;
72117
0 commit comments