fido 0.1.4

A blazing-fast, keyboard-driven social platform for developers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use fido_types::{Post, User, UserProfile};

use ratatui::widgets::ListState;
use std::time::Instant;
use tui_textarea::TextArea;
use uuid::Uuid;

use crate::api::ApiClient;

/// Get platform-appropriate modifier key name for display
/// Returns "Cmd" on macOS, "Ctrl" on other platforms
#[cfg(target_os = "macos")]
pub fn get_modifier_key_name() -> &'static str {
    "Cmd"
}

#[cfg(not(target_os = "macos"))]
pub fn get_modifier_key_name() -> &'static str {
    "Ctrl"
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InputMode {
    Navigation, // Browsing content, shortcuts active
    Typing,     // In text input, shortcuts disabled
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SettingsField {
    ColorScheme,
    SortOrder,
    MaxPosts,
}

/// Composer mode - determines what type of content is being composed
#[derive(Debug, Clone)]
pub enum ComposerMode {
    NewPost,
    Reply {
        parent_post_id: Uuid,
        parent_author: String,
        parent_content: String,
    },
    EditPost {
        post_id: Uuid,
    },
    EditBio,
}

/// Unified composer state using tui-textarea
pub struct ComposerState {
    pub mode: Option<ComposerMode>,
    pub textarea: TextArea<'static>,
    pub max_chars: usize,
}

impl ComposerState {
    pub fn new() -> Self {
        let mut textarea = TextArea::default();
        // Enable hard tab indent for better wrapping behavior
        textarea.set_hard_tab_indent(true);
        Self {
            mode: None,
            textarea,
            max_chars: 280,
        }
    }

    pub fn is_open(&self) -> bool {
        self.mode.is_some()
    }

    pub fn get_content(&self) -> String {
        self.textarea.lines().join("\n")
    }

    pub fn char_count(&self) -> usize {
        crate::emoji::count_characters(&self.get_content())
    }
}

/// Social connections modal state
pub struct FriendsState {
    pub show_friends_modal: bool,
    pub selected_tab: SocialTab,
    pub following: Vec<UserInfo>,
    pub followers: Vec<UserInfo>,
    pub mutual_friends: Vec<UserInfo>,
    pub selected_index: usize,
    pub search_query: String,
    pub search_mode: bool,
    pub error: Option<String>,
    pub loading: bool,
    pub return_to_modal_after_profile: bool, // Flag to reopen modal after viewing profile
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SocialTab {
    Following,
    Followers,
    MutualFriends,
}

/// User information for social lists
#[derive(Debug, Clone)]
pub struct UserInfo {
    pub id: String,
    pub username: String,
    pub follower_count: usize,
    pub following_count: usize,
}

/// Hashtags modal state
pub struct HashtagsState {
    pub hashtags: Vec<String>,
    pub show_hashtags_modal: bool,
    pub show_add_hashtag_input: bool,
    pub add_hashtag_name: String,
    pub selected_hashtag: usize,
    pub error: Option<String>,
    pub loading: bool,
    pub show_unfollow_confirmation: bool,
    pub hashtag_to_unfollow: Option<String>,
}

/// User search modal state
pub struct UserSearchState {
    pub show_modal: bool,
    pub search_query: String,
    pub search_results: Vec<UserSearchResult>,
    pub selected_index: usize,
    pub loading: bool,
    pub error: Option<String>,
}

#[derive(Debug, Clone)]
pub struct UserSearchResult {
    pub id: String,
    pub username: String,
}

/// Main application state
pub struct App {
    pub running: bool,
    pub current_screen: Screen,
    pub api_client: ApiClient,
    pub auth_state: AuthState,
    pub current_tab: Tab,
    pub posts_state: PostsState,
    pub profile_state: ProfileState,
    pub dms_state: DMsState,
    pub settings_state: SettingsState,
    pub post_detail_state: Option<PostDetailState>,
    pub viewing_post_detail: bool,
    pub config_manager: crate::config::ConfigManager,
    pub instance_id: String,
    pub show_help: bool,
    pub input_mode: InputMode,
    pub composer_state: ComposerState,
    pub friends_state: FriendsState,
    pub hashtags_state: HashtagsState,
    pub user_search_state: UserSearchState,
    pub user_profile_view: Option<UserProfileViewState>,
    pub log_config: crate::logging::LogConfig,
}

/// Settings tab state
pub struct SettingsState {
    pub config: Option<fido_types::UserConfig>,
    pub original_config: Option<fido_types::UserConfig>,
    pub original_max_posts_input: String,
    pub loading: bool,
    pub error: Option<String>,
    pub selected_field: SettingsField,
    pub max_posts_input: String,
    pub has_unsaved_changes: bool,
    pub show_save_confirmation: bool,
    pub pending_tab: Option<Tab>,
}

/// DMs tab state
pub struct DMsState {
    pub conversations: Vec<Conversation>,
    pub selected_conversation_index: Option<usize>, // None = no conversation selected
    pub messages: Vec<fido_types::DirectMessage>,
    pub loading: bool,
    pub error: Option<String>,
    pub message_input: String, // Deprecated - kept for compatibility, use message_textarea instead
    pub message_textarea: TextArea<'static>, // TextArea for message input
    pub messages_scroll_offset: usize, // Scroll offset for message history
    pub show_new_conversation_modal: bool,
    pub new_conversation_username: String,
    pub pending_conversation_username: Option<String>, // Username for new conversation not yet created
    pub unread_counts: std::collections::HashMap<uuid::Uuid, usize>, // user_id -> unread count
    pub current_conversation_user: Option<uuid::Uuid>, // Track open conversation
    pub needs_message_load: bool,                      // Flag to trigger message loading
    /// Show DM error modal with friend suggestions
    pub show_dm_error_modal: bool,
    /// Error message to display in the modal
    pub dm_error_message: String,
    /// Username that failed when attempting to start a conversation
    pub failed_username: Option<String>,
    /// Mutual friends available for DMs (full user info with stats)
    pub available_mutual_friends: Vec<UserInfo>,
    /// Selected index in new conversation modal
    pub new_conversation_selected_index: usize,
    /// Search mode for new conversation modal
    pub new_conversation_search_mode: bool,
    /// Search query for new conversation modal
    pub new_conversation_search_query: String,
}

/// Conversation summary
#[derive(Debug, Clone)]
pub struct Conversation {
    pub other_user_id: uuid::Uuid,
    pub other_username: String,
    pub last_message: String,
    pub last_message_time: chrono::DateTime<chrono::Utc>,
    pub unread_count: i32,
}

/// Profile tab state (for viewing own profile)
pub struct ProfileState {
    pub profile: Option<UserProfile>,
    pub user_posts: Vec<Post>,
    pub list_state: ListState,
    pub loading: bool,
    pub error: Option<String>,
    pub show_edit_bio_modal: bool,
    pub edit_bio_content: String,
    pub edit_bio_cursor_position: usize,
}

/// User profile view state (for viewing other users' profiles)
pub struct UserProfileViewState {
    pub user_id: String,
    pub username: String,
    pub bio: Option<String>,
    pub join_date: String,
    pub follower_count: usize,
    pub following_count: usize,
    pub post_count: usize,
    pub relationship: RelationshipStatus,
    pub loading: bool,
    pub error: Option<String>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum RelationshipStatus {
    Self_,
    MutualFriends,
    Following,
    FollowsYou,
    None,
}

/// Filter type for posts
#[derive(Debug, Clone, PartialEq)]
pub enum PostFilter {
    All,
    Hashtag(String),
    User(String),
    Multi {
        hashtags: Vec<String>,
        users: Vec<String>,
    },
}

impl PostFilter {
    /// Convert to UserPreferences format for saving
    pub fn to_preferences(&self) -> crate::config::UserPreferences {
        match self {
            PostFilter::All => crate::config::UserPreferences {
                filter_type: "all".to_string(),
                filter_hashtag: None,
                filter_user: None,
                filter_hashtags: Vec::new(),
                filter_users: Vec::new(),
            },
            PostFilter::Hashtag(tag) => crate::config::UserPreferences {
                filter_type: "hashtag".to_string(),
                filter_hashtag: Some(tag.clone()),
                filter_user: None,
                filter_hashtags: Vec::new(),
                filter_users: Vec::new(),
            },
            PostFilter::User(user) => crate::config::UserPreferences {
                filter_type: "user".to_string(),
                filter_hashtag: None,
                filter_user: Some(user.clone()),
                filter_hashtags: Vec::new(),
                filter_users: Vec::new(),
            },
            PostFilter::Multi { hashtags, users } => crate::config::UserPreferences {
                filter_type: "multi".to_string(),
                filter_hashtag: None,
                filter_user: None,
                filter_hashtags: hashtags.clone(),
                filter_users: users.clone(),
            },
        }
    }

    /// Create from UserPreferences
    pub fn from_preferences(prefs: &crate::config::UserPreferences) -> Self {
        match prefs.filter_type.as_str() {
            "hashtag" => {
                if let Some(tag) = &prefs.filter_hashtag {
                    PostFilter::Hashtag(tag.clone())
                } else {
                    PostFilter::All
                }
            }
            "user" => {
                if let Some(user) = &prefs.filter_user {
                    PostFilter::User(user.clone())
                } else {
                    PostFilter::All
                }
            }
            "multi" => PostFilter::Multi {
                hashtags: prefs.filter_hashtags.clone(),
                users: prefs.filter_users.clone(),
            },
            _ => PostFilter::All,
        }
    }
}

/// Posts tab state
pub struct PostsState {
    pub posts: Vec<Post>,
    pub list_state: ListState,
    pub loading: bool,
    pub error: Option<String>,
    pub message: Option<(String, Instant)>, // (message, timestamp) - auto-clears after 3 seconds
    pub show_new_post_modal: bool,
    pub new_post_content: String,
    /// Flag to trigger actual load after UI renders loading state
    pub pending_load: bool,
    /// Current filter applied to posts
    pub current_filter: PostFilter,
    /// Show filter modal
    pub show_filter_modal: bool,
    /// Filter modal state
    pub filter_modal_state: FilterModalState,
    /// Current sort order display
    pub sort_order: String,
    /// Track if at end of feed (for "End of Feed" indicator)
    pub at_end_of_feed: bool,
}

impl PostsState {
    /// Calculate how many items appear before posts in the rendered list
    /// This is used to convert between post indices and list indices
    pub fn items_before_posts(&self) -> usize {
        let mut count = 0;

        // Loading spinner
        if self.loading && !self.posts.is_empty() {
            count += 1;
        }

        count
    }

    /// Convert a post index to a list index
    pub fn post_index_to_list_index(&self, post_index: usize) -> usize {
        post_index + self.items_before_posts()
    }

    /// Convert a list index to a post index (returns None if list index points to a non-post item)
    pub fn list_index_to_post_index(&self, list_index: usize) -> Option<usize> {
        let offset = self.items_before_posts();
        if list_index >= offset {
            Some(list_index - offset)
        } else {
            None
        }
    }
}

/// Filter modal state
pub struct FilterModalState {
    pub selected_tab: FilterTab,
    pub hashtag_list: Vec<String>,
    pub user_list: Vec<String>,
    pub selected_index: usize,
    pub search_input: String,
    pub search_mode: bool,
    pub search_results: Vec<String>,
    /// Checked hashtags for multi-select
    pub checked_hashtags: Vec<String>,
    /// Checked users for multi-select
    pub checked_users: Vec<String>,
    /// Show add hashtag input in hashtags tab
    pub show_add_hashtag_input: bool,
    /// Input for adding new hashtag
    pub add_hashtag_input: String,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FilterTab {
    All,
    Hashtags,
    Users,
}

/// Post detail view state
pub struct PostDetailState {
    pub post: Option<Post>,
    pub replies: Vec<Post>,
    pub reply_list_state: ListState,
    pub loading: bool,
    pub error: Option<String>,
    pub message: Option<(String, Instant)>, // (message, timestamp) - auto-clears after 3 seconds
    pub show_reply_composer: bool,
    pub reply_content: String,
    pub show_delete_confirmation: bool,
    pub previous_feed_position: Option<usize>,
    /// Track which posts are expanded (post_id -> is_expanded)
    pub expanded_posts: std::collections::HashMap<Uuid, bool>,
    /// Show full post modal
    pub show_full_post_modal: bool,
    /// Post ID for full post modal
    pub full_post_modal_id: Option<Uuid>,
    /// Modal-specific list state for nested reply navigation
    pub modal_list_state: ListState,
    /// Track expansion state within modal (separate from main view)
    pub modal_expanded_posts: std::collections::HashMap<Uuid, bool>,
}

impl PostDetailState {
    /// Get direct replies (replies that are not nested under other replies).
    ///
    /// Direct replies are those whose parent_post_id is not in the replies list,
    /// meaning they reply directly to the main post rather than to another reply.
    pub fn get_direct_replies(&self) -> Vec<&Post> {
        use std::collections::HashSet;

        // Build a set of all reply IDs for O(1) lookups
        let reply_ids: HashSet<Uuid> = self.replies.iter().map(|r| r.id).collect();

        // Filter replies whose parent is not in the reply list
        self.replies
            .iter()
            .filter(|reply| {
                reply
                    .parent_post_id
                    .map(|parent_id| !reply_ids.contains(&parent_id))
                    .unwrap_or(false)
            })
            .collect()
    }

    /// Get the post that should be deleted based on current selection.
    ///
    /// Returns the selected reply if one is selected and exists, otherwise returns the main post.
    /// Handles both main detail view and full post modal.
    pub fn get_deletable_post(&self) -> Option<&Post> {
        // If in full post modal, get the selected post from modal state
        if self.show_full_post_modal {
            if let Some(selected_idx) = self.modal_list_state.selected() {
                if selected_idx == 0 {
                    // First item is always the root post in modal
                    return self.full_post_modal_id.and_then(|id| {
                        if self.post.as_ref().map(|p| p.id) == Some(id) {
                            self.post.as_ref()
                        } else {
                            self.replies.iter().find(|r| r.id == id)
                        }
                    });
                } else {
                    // Get the flattened visible posts to find the selected one
                    if let Some(root_id) = self.full_post_modal_id {
                        let mut flattened_posts = Vec::new();
                        self.collect_visible_posts_for_modal(root_id, &mut flattened_posts);

                        if selected_idx > 0 && selected_idx <= flattened_posts.len() {
                            let post_id = flattened_posts[selected_idx - 1];
                            return self.replies.iter().find(|r| r.id == post_id);
                        }
                    }
                }
            }
            return None;
        }

        // Main detail view logic (existing)
        if self.replies.is_empty() {
            return self.post.as_ref();
        }

        if let Some(selected_idx) = self.reply_list_state.selected() {
            let direct_replies = self.get_direct_replies();
            if let Some(reply) = direct_replies.get(selected_idx) {
                return Some(reply);
            }
        }

        self.post.as_ref()
    }

    /// Helper to collect visible posts in modal (for deletion)
    fn collect_visible_posts_for_modal(&self, root_id: Uuid, result: &mut Vec<Uuid>) {
        use std::collections::HashMap;

        let mut children_map: HashMap<Uuid, Vec<Uuid>> = HashMap::new();
        for reply in &self.replies {
            if let Some(parent_id) = reply.parent_post_id {
                children_map
                    .entry(parent_id)
                    .or_default()
                    .push(reply.id);
            }
        }

        fn collect(
            post_id: &Uuid,
            children_map: &HashMap<Uuid, Vec<Uuid>>,
            expanded: &std::collections::HashMap<Uuid, bool>,
            result: &mut Vec<Uuid>,
        ) {
            if let Some(children) = children_map.get(post_id) {
                for child_id in children {
                    result.push(*child_id);
                    if expanded.get(child_id).copied().unwrap_or(false) {
                        collect(child_id, children_map, expanded, result);
                    }
                }
            }
        }

        collect(&root_id, &children_map, &self.modal_expanded_posts, result);
    }
}

/// Authentication state
pub struct AuthState {
    pub test_users: Vec<User>,
    pub selected_index: usize,
    pub loading: bool,
    pub error: Option<String>,
    pub current_user: Option<User>,
    pub show_github_option: bool,
    pub github_auth_in_progress: bool,
    pub github_device_code: Option<String>,
    pub github_user_code: Option<String>,
    pub github_verification_uri: Option<String>,
    pub github_poll_interval: Option<i64>,
    pub github_auth_start_time: Option<std::time::Instant>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Screen {
    Auth,
    Main,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Tab {
    Posts,
    DMs,
    Profile,
    Settings,
}

impl Tab {
    pub fn next(&self) -> Self {
        match self {
            Tab::Posts => Tab::DMs,
            Tab::DMs => Tab::Profile,
            Tab::Profile => Tab::Settings,
            Tab::Settings => Tab::Posts,
        }
    }

    pub fn previous(&self) -> Self {
        match self {
            Tab::Posts => Tab::Settings,
            Tab::DMs => Tab::Posts,
            Tab::Profile => Tab::DMs,
            Tab::Settings => Tab::Profile,
        }
    }
}