Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I determine current page? #19

Open
canack opened this issue Jan 18, 2025 · 2 comments
Open

How can I determine current page? #19

canack opened this issue Jan 18, 2025 · 2 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@canack
Copy link
Member

canack commented Jan 18, 2025

How can I learn if I switch the tab? I want to trigger a message every tab switch.

Copy link

🤖 AI Assistant Analysis

To detect and trigger a message when switching tabs in the Skeleton library, you can leverage the IAMActivePage message and the IAMActivePageCmd() method. Here's a comprehensive guide:

Detecting Tab Switches

  1. Use IAMActivePage Message
    The IAMActivePage message is automatically triggered when a tab is switched. You can handle this in your model's Update method.
func (m *YourModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case skeleton.IAMActivePage:
        // This block is called every time the tab is switched
        fmt.Println("Tab switched to:", m.skeleton.GetActivePage())
        // Perform any custom actions when tab is switched
    }
    return m, nil
}

Practical Example

type YourModel struct {
    skeleton *skeleton.Skeleton
    currentTabKey string
}

func (m *YourModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case skeleton.IAMActivePage:
        newTabKey := m.skeleton.GetActivePage()
        if m.currentTabKey != newTabKey {
            fmt.Printf("Switched from %s to %s\n", m.currentTabKey, newTabKey)
            m.currentTabKey = newTabKey
            
            // Optional: Perform tab-specific initialization
            switch newTabKey {
            case "first_tab":
                // Do something specific for first tab
            case "second_tab":
                // Do something specific for second tab
            }
        }
    }
    return m, nil
}

Advanced Tab Switch Handling

  1. Custom Tab Switch Tracking

    • Track the previous and current tab
    • Perform conditional logic based on tab switches
  2. Logging and Analytics

    • Log tab switch events
    • Track user navigation patterns

Best Practices

  • Always check if the tab has actually changed
  • Use GetActivePage() to get the current tab's key
  • Implement lightweight, non-blocking operations in tab switch handlers

Triggering Custom Commands

If you want to trigger a custom command on tab switch:

func (m *YourModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case skeleton.IAMActivePage:
        return m, tea.Batch(
            m.loadTabData(),
            m.updateTabMetrics(),
        )
    }
    return m, nil
}

func (m *YourModel) loadTabData() tea.Cmd {
    return func() tea.Msg {
        // Perform async data loading for the new tab
        return nil
    }
}

Limitations and Considerations

  • The IAMActivePage message is triggered after the tab switch
  • Ensure your handlers are efficient to maintain UI responsiveness

This analysis was performed by Issue Assistant. If you have any questions, please contact the repository maintainers.

@github-actions github-actions bot added the enhancement New feature or request label Jan 18, 2025
Copy link

🏷️ AI Label Analysis

I've added the following labels to this issue:

  • question
  • enhancement

Explanation:
This issue is clearly a question about how to implement a specific functionality (detecting tab switches), which makes 'question' the most appropriate primary label. There's also an element of feature request or potential enhancement, as the user is seeking guidance on implementing a new feature, hence the 'enhancement' label with a slightly lower confidence score.


This label analysis was performed by Issue Assistant. If you have any questions, please contact the repository maintainers.

@github-actions github-actions bot added the question Further information is requested label Jan 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant