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