1use std::str::FromStr;
2use std::sync::OnceLock;
3
4use crate::stories::*;
5use anyhow::anyhow;
6use clap::builder::PossibleValue;
7use clap::ValueEnum;
8use gpui::{AnyView, VisualContext};
9use strum::{EnumIter, EnumString, IntoEnumIterator};
10use ui::prelude::*;
11
12#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)]
13#[strum(serialize_all = "snake_case")]
14pub enum ComponentStory {
15 AutoHeightEditor,
16 Avatar,
17 Button,
18 Checkbox,
19 CollabNotification,
20 ContextMenu,
21 Cursor,
22 Disclosure,
23 Focus,
24 Icon,
25 IconButton,
26 Keybinding,
27 Label,
28 List,
29 ListHeader,
30 ListItem,
31 OverflowScroll,
32 Scroll,
33 Tab,
34 TabBar,
35 ToggleButton,
36 Text,
37 ViewportUnits,
38 Picker,
39}
40
41impl ComponentStory {
42 pub fn story(&self, cx: &mut WindowContext) -> AnyView {
43 match self {
44 Self::AutoHeightEditor => AutoHeightEditorStory::new(cx).into(),
45 Self::Avatar => cx.new_view(|_| ui::AvatarStory).into(),
46 Self::Button => cx.new_view(|_| ui::ButtonStory).into(),
47 Self::Checkbox => cx.new_view(|_| ui::CheckboxStory).into(),
48 Self::CollabNotification => cx
49 .new_view(|_| collab_ui::notifications::CollabNotificationStory)
50 .into(),
51 Self::ContextMenu => cx.new_view(|_| ui::ContextMenuStory).into(),
52 Self::Cursor => cx.new_view(|_| crate::stories::CursorStory).into(),
53 Self::Disclosure => cx.new_view(|_| ui::DisclosureStory).into(),
54 Self::Focus => FocusStory::view(cx).into(),
55 Self::Icon => cx.new_view(|_| ui::IconStory).into(),
56 Self::IconButton => cx.new_view(|_| ui::IconButtonStory).into(),
57 Self::Keybinding => cx.new_view(|_| ui::KeybindingStory).into(),
58 Self::Label => cx.new_view(|_| ui::LabelStory).into(),
59 Self::List => cx.new_view(|_| ui::ListStory).into(),
60 Self::ListHeader => cx.new_view(|_| ui::ListHeaderStory).into(),
61 Self::ListItem => cx.new_view(|_| ui::ListItemStory).into(),
62 Self::OverflowScroll => cx.new_view(|_| crate::stories::OverflowScrollStory).into(),
63 Self::Scroll => ScrollStory::view(cx).into(),
64 Self::Text => TextStory::view(cx).into(),
65 Self::Tab => cx.new_view(|_| ui::TabStory).into(),
66 Self::TabBar => cx.new_view(|_| ui::TabBarStory).into(),
67 Self::ToggleButton => cx.new_view(|_| ui::ToggleButtonStory).into(),
68 Self::ViewportUnits => cx.new_view(|_| crate::stories::ViewportUnitsStory).into(),
69 Self::Picker => PickerStory::new(cx).into(),
70 }
71 }
72}
73
74#[derive(Debug, PartialEq, Eq, Clone, Copy)]
75pub enum StorySelector {
76 Component(ComponentStory),
77 KitchenSink,
78}
79
80impl FromStr for StorySelector {
81 type Err = anyhow::Error;
82
83 fn from_str(raw_story_name: &str) -> std::result::Result<Self, Self::Err> {
84 use anyhow::Context;
85
86 let story = raw_story_name.to_ascii_lowercase();
87
88 if story == "kitchen_sink" {
89 return Ok(Self::KitchenSink);
90 }
91
92 if let Some((_, story)) = story.split_once("components/") {
93 let component_story = ComponentStory::from_str(story)
94 .with_context(|| format!("story not found for component '{story}'"))?;
95
96 return Ok(Self::Component(component_story));
97 }
98
99 Err(anyhow!("story not found for '{raw_story_name}'"))
100 }
101}
102
103impl StorySelector {
104 pub fn story(&self, cx: &mut WindowContext) -> AnyView {
105 match self {
106 Self::Component(component_story) => component_story.story(cx),
107 Self::KitchenSink => KitchenSinkStory::view(cx).into(),
108 }
109 }
110}
111
112/// The list of all stories available in the storybook.
113static ALL_STORY_SELECTORS: OnceLock<Vec<StorySelector>> = OnceLock::new();
114
115impl ValueEnum for StorySelector {
116 fn value_variants<'a>() -> &'a [Self] {
117 let stories = ALL_STORY_SELECTORS.get_or_init(|| {
118 let component_stories = ComponentStory::iter().map(StorySelector::Component);
119
120 component_stories
121 .chain(std::iter::once(StorySelector::KitchenSink))
122 .collect::<Vec<_>>()
123 });
124
125 stories
126 }
127
128 fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
129 let value = match self {
130 Self::Component(story) => format!("components/{story}"),
131 Self::KitchenSink => "kitchen_sink".to_string(),
132 };
133
134 Some(PossibleValue::new(value))
135 }
136}