codeblog-app 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/package.json +8 -71
  2. package/drizzle/0000_init.sql +0 -34
  3. package/drizzle/meta/_journal.json +0 -13
  4. package/drizzle.config.ts +0 -10
  5. package/src/ai/__tests__/chat.test.ts +0 -110
  6. package/src/ai/__tests__/provider.test.ts +0 -184
  7. package/src/ai/__tests__/tools.test.ts +0 -90
  8. package/src/ai/chat.ts +0 -169
  9. package/src/ai/configure.ts +0 -89
  10. package/src/ai/provider.ts +0 -238
  11. package/src/ai/tools.ts +0 -321
  12. package/src/auth/index.ts +0 -47
  13. package/src/auth/oauth.ts +0 -93
  14. package/src/cli/__tests__/commands.test.ts +0 -225
  15. package/src/cli/__tests__/setup.test.ts +0 -57
  16. package/src/cli/cmd/agent.ts +0 -102
  17. package/src/cli/cmd/chat.ts +0 -190
  18. package/src/cli/cmd/comment.ts +0 -70
  19. package/src/cli/cmd/config.ts +0 -153
  20. package/src/cli/cmd/feed.ts +0 -57
  21. package/src/cli/cmd/forum.ts +0 -123
  22. package/src/cli/cmd/login.ts +0 -45
  23. package/src/cli/cmd/logout.ts +0 -12
  24. package/src/cli/cmd/me.ts +0 -202
  25. package/src/cli/cmd/post.ts +0 -29
  26. package/src/cli/cmd/publish.ts +0 -70
  27. package/src/cli/cmd/scan.ts +0 -80
  28. package/src/cli/cmd/search.ts +0 -40
  29. package/src/cli/cmd/setup.ts +0 -201
  30. package/src/cli/cmd/tui.ts +0 -20
  31. package/src/cli/cmd/update.ts +0 -78
  32. package/src/cli/cmd/vote.ts +0 -50
  33. package/src/cli/cmd/whoami.ts +0 -21
  34. package/src/cli/ui.ts +0 -124
  35. package/src/config/index.ts +0 -54
  36. package/src/flag/index.ts +0 -23
  37. package/src/global/index.ts +0 -38
  38. package/src/id/index.ts +0 -20
  39. package/src/index.ts +0 -186
  40. package/src/mcp/__tests__/client.test.ts +0 -149
  41. package/src/mcp/__tests__/e2e.ts +0 -327
  42. package/src/mcp/__tests__/integration.ts +0 -148
  43. package/src/mcp/client.ts +0 -148
  44. package/src/server/index.ts +0 -48
  45. package/src/storage/chat.ts +0 -92
  46. package/src/storage/db.ts +0 -85
  47. package/src/storage/schema.sql.ts +0 -39
  48. package/src/storage/schema.ts +0 -1
  49. package/src/tui/app.tsx +0 -163
  50. package/src/tui/commands.ts +0 -177
  51. package/src/tui/context/exit.tsx +0 -15
  52. package/src/tui/context/helper.tsx +0 -25
  53. package/src/tui/context/route.tsx +0 -24
  54. package/src/tui/context/theme.tsx +0 -470
  55. package/src/tui/routes/home.tsx +0 -490
  56. package/src/tui/routes/model.tsx +0 -209
  57. package/src/tui/routes/notifications.tsx +0 -85
  58. package/src/tui/routes/post.tsx +0 -108
  59. package/src/tui/routes/search.tsx +0 -104
  60. package/src/tui/routes/setup.tsx +0 -255
  61. package/src/tui/routes/trending.tsx +0 -107
  62. package/src/util/__tests__/context.test.ts +0 -31
  63. package/src/util/__tests__/lazy.test.ts +0 -37
  64. package/src/util/context.ts +0 -23
  65. package/src/util/error.ts +0 -46
  66. package/src/util/lazy.ts +0 -18
  67. package/src/util/log.ts +0 -142
  68. package/tsconfig.json +0 -11
package/src/tui/app.tsx DELETED
@@ -1,163 +0,0 @@
1
- import { render, useKeyboard, useRenderer, useTerminalDimensions } from "@opentui/solid"
2
- import { Switch, Match, onMount, createSignal, Show } from "solid-js"
3
- import { RouteProvider, useRoute } from "./context/route"
4
- import { ExitProvider, useExit } from "./context/exit"
5
- import { ThemeProvider, useTheme } from "./context/theme"
6
- import { Home } from "./routes/home"
7
- import { ThemePicker } from "./routes/setup"
8
- import { ModelPicker } from "./routes/model"
9
-
10
- import pkg from "../../package.json"
11
- const VERSION = pkg.version
12
-
13
- export function tui(input: { onExit?: () => Promise<void> }) {
14
- return new Promise<void>(async (resolve) => {
15
- render(
16
- () => (
17
- <ExitProvider onExit={async () => { await input.onExit?.(); resolve() }}>
18
- <ThemeProvider>
19
- <RouteProvider>
20
- <App />
21
- </RouteProvider>
22
- </ThemeProvider>
23
- </ExitProvider>
24
- ),
25
- {
26
- targetFps: 30,
27
- exitOnCtrlC: false,
28
- autoFocus: false,
29
- openConsoleOnError: false,
30
- },
31
- )
32
- })
33
- }
34
-
35
- function App() {
36
- const route = useRoute()
37
- const exit = useExit()
38
- const theme = useTheme()
39
- const dimensions = useTerminalDimensions()
40
- const renderer = useRenderer()
41
- const [loggedIn, setLoggedIn] = createSignal(false)
42
- const [username, setUsername] = createSignal("")
43
- const [hasAI, setHasAI] = createSignal(false)
44
- const [aiProvider, setAiProvider] = createSignal("")
45
- const [modelName, setModelName] = createSignal("")
46
-
47
- async function refreshAI() {
48
- try {
49
- const { AIProvider } = await import("../ai/provider")
50
- const has = await AIProvider.hasAnyKey()
51
- setHasAI(has)
52
- if (has) {
53
- const { Config } = await import("../config")
54
- const cfg = await Config.load()
55
- const model = cfg.model || AIProvider.DEFAULT_MODEL
56
- setModelName(model)
57
- const info = AIProvider.BUILTIN_MODELS[model]
58
- setAiProvider(info?.providerID || model.split("/")[0] || "ai")
59
- }
60
- } catch {}
61
- }
62
-
63
- onMount(async () => {
64
- renderer.setTerminalTitle("CodeBlog")
65
-
66
- // Check auth status
67
- try {
68
- const { Auth } = await import("../auth")
69
- const authenticated = await Auth.authenticated()
70
- setLoggedIn(authenticated)
71
- if (authenticated) {
72
- const token = await Auth.get()
73
- if (token?.username) setUsername(token.username)
74
- }
75
- } catch {}
76
-
77
- await refreshAI()
78
- })
79
-
80
- useKeyboard((evt) => {
81
- if (evt.ctrl && evt.name === "c") {
82
- exit()
83
- evt.preventDefault()
84
- return
85
- }
86
-
87
- // Back navigation from sub-pages
88
- if (evt.name === "escape" && route.data.type !== "home") {
89
- route.navigate({ type: "home" })
90
- evt.preventDefault()
91
- return
92
- }
93
- })
94
-
95
- return (
96
- <box flexDirection="column" width={dimensions().width} height={dimensions().height}>
97
- <Switch>
98
- <Match when={route.data.type === "home"}>
99
- <Home
100
- loggedIn={loggedIn()}
101
- username={username()}
102
- hasAI={hasAI()}
103
- aiProvider={aiProvider()}
104
- modelName={modelName()}
105
- onLogin={async () => {
106
- try {
107
- const { OAuth } = await import("../auth/oauth")
108
- await OAuth.login("github")
109
- const { Auth } = await import("../auth")
110
- setLoggedIn(true)
111
- const token = await Auth.get()
112
- if (token?.username) setUsername(token.username)
113
- } catch {}
114
- }}
115
- onLogout={() => { setLoggedIn(false); setUsername("") }}
116
- onAIConfigured={refreshAI}
117
- />
118
- </Match>
119
- <Match when={route.data.type === "theme"}>
120
- <ThemePicker onDone={() => route.navigate({ type: "home" })} />
121
- </Match>
122
- <Match when={route.data.type === "model"}>
123
- <ModelPicker onDone={async (model) => {
124
- if (model) setModelName(model)
125
- await refreshAI()
126
- route.navigate({ type: "home" })
127
- }} />
128
- </Match>
129
- </Switch>
130
-
131
- {/* Status bar — like OpenCode */}
132
- <box paddingLeft={2} paddingRight={2} flexShrink={0} flexDirection="row" gap={2}>
133
- <text fg={theme.colors.textMuted}>{process.cwd()}</text>
134
- <box flexGrow={1} />
135
- <Show when={hasAI()}>
136
- <text fg={theme.colors.text}>
137
- <span style={{ fg: theme.colors.success }}>● </span>
138
- {modelName()}
139
- </text>
140
- </Show>
141
- <Show when={!hasAI()}>
142
- <text fg={theme.colors.text}>
143
- <span style={{ fg: theme.colors.error }}>○ </span>
144
- no AI <span style={{ fg: theme.colors.textMuted }}>/ai</span>
145
- </text>
146
- </Show>
147
- <Show when={loggedIn()}>
148
- <text fg={theme.colors.text}>
149
- <span style={{ fg: theme.colors.success }}>● </span>
150
- {username() || "logged in"}
151
- </text>
152
- </Show>
153
- <Show when={!loggedIn()}>
154
- <text fg={theme.colors.text}>
155
- <span style={{ fg: theme.colors.error }}>○ </span>
156
- <span style={{ fg: theme.colors.textMuted }}>/login</span>
157
- </text>
158
- </Show>
159
- <text fg={theme.colors.textMuted}>v{VERSION}</text>
160
- </box>
161
- </box>
162
- )
163
- }
@@ -1,177 +0,0 @@
1
- // Slash command definitions for the TUI home screen
2
-
3
- export interface CmdDef {
4
- name: string
5
- description: string
6
- action: (parts: string[]) => void | Promise<void>
7
- }
8
-
9
- export interface CommandDeps {
10
- showMsg: (text: string, color?: string) => void
11
- navigate: (route: any) => void
12
- exit: () => void
13
- onLogin: () => Promise<void>
14
- onLogout: () => void
15
- clearChat: () => void
16
- startAIConfig: () => void
17
- setMode: (mode: "dark" | "light") => void
18
- send: (prompt: string) => void
19
- resume: (id?: string) => void
20
- listSessions: () => Array<{ id: string; title: string | null; time: number; count: number }>
21
- colors: {
22
- primary: string
23
- success: string
24
- warning: string
25
- error: string
26
- text: string
27
- }
28
- }
29
-
30
- export function createCommands(deps: CommandDeps): CmdDef[] {
31
- return [
32
- // UI-only commands (no AI needed)
33
- { name: "/ai", description: "Configure AI provider (paste URL + key)", action: () => deps.startAIConfig() },
34
- { name: "/model", description: "Choose AI model", action: () => deps.navigate({ type: "model" }) },
35
- { name: "/clear", description: "Clear conversation", action: () => deps.clearChat() },
36
- { name: "/new", description: "New conversation", action: () => deps.clearChat() },
37
- { name: "/login", description: "Sign in to CodeBlog", action: async () => {
38
- deps.showMsg("Opening browser for login...", deps.colors.primary)
39
- await deps.onLogin()
40
- deps.showMsg("Logged in!", deps.colors.success)
41
- }},
42
- { name: "/logout", description: "Sign out of CodeBlog", action: async () => {
43
- try {
44
- const { Auth } = await import("../auth")
45
- await Auth.remove()
46
- deps.showMsg("Logged out.", deps.colors.text)
47
- deps.onLogout()
48
- } catch (err) { deps.showMsg(`Logout failed: ${err instanceof Error ? err.message : String(err)}`, deps.colors.error) }
49
- }},
50
- { name: "/theme", description: "Change color theme", action: () => deps.navigate({ type: "theme" }) },
51
- { name: "/dark", description: "Switch to dark mode", action: () => { deps.setMode("dark"); deps.showMsg("Dark mode", deps.colors.text) } },
52
- { name: "/light", description: "Switch to light mode", action: () => { deps.setMode("light"); deps.showMsg("Light mode", deps.colors.text) } },
53
- { name: "/exit", description: "Exit CodeBlog", action: () => deps.exit() },
54
- { name: "/resume", description: "Resume last chat session", action: (parts) => deps.resume(parts[1]) },
55
- { name: "/history", description: "Show recent chat sessions", action: () => {
56
- try {
57
- const sessions = deps.listSessions()
58
- if (sessions.length === 0) { deps.showMsg("No chat history yet", deps.colors.warning); return }
59
- const lines = sessions.map((s, i) => `${i + 1}. ${s.title || "(untitled)"} (${s.count} msgs, ${new Date(s.time).toLocaleDateString()})`)
60
- deps.showMsg(lines.join(" | "), deps.colors.text)
61
- } catch { deps.showMsg("Failed to load history", deps.colors.error) }
62
- }},
63
-
64
- // === Session tools (scan_sessions, read_session, analyze_session) ===
65
- { name: "/scan", description: "Scan IDE coding sessions", action: () => deps.send("Scan my local IDE coding sessions and tell me what you found. Show sources, projects, and session counts.") },
66
- { name: "/read", description: "Read a session: /read <index>", action: (parts) => {
67
- const idx = parts[1]
68
- deps.send(idx ? `Read session #${idx} from my scan results and show me the conversation.` : "Scan my sessions and read the most recent one in full.")
69
- }},
70
- { name: "/analyze", description: "Analyze a session: /analyze <index>", action: (parts) => {
71
- const idx = parts[1]
72
- deps.send(idx ? `Analyze session #${idx} — extract topics, problems, solutions, code snippets, and insights.` : "Scan my sessions and analyze the most interesting one.")
73
- }},
74
-
75
- // === Posting tools (post_to_codeblog, auto_post, weekly_digest) ===
76
- { name: "/publish", description: "Auto-publish a coding session", action: () => deps.send("Scan my IDE sessions, pick the most interesting one with enough content, and auto-publish it as a blog post on CodeBlog.") },
77
- { name: "/write", description: "Write a custom post: /write <title>", action: (parts) => {
78
- const title = parts.slice(1).join(" ")
79
- deps.send(title ? `Write and publish a blog post titled "${title}" on CodeBlog.` : "Help me write a blog post for CodeBlog. Ask me what I want to write about.")
80
- }},
81
- { name: "/digest", description: "Weekly coding digest", action: () => deps.send("Generate a weekly coding digest from my recent sessions — aggregate projects, languages, problems, and insights. Preview it first.") },
82
-
83
- // === Forum browse & search (browse_posts, search_posts, read_post, browse_by_tag, trending_topics, explore_and_engage) ===
84
- { name: "/feed", description: "Browse recent posts", action: () => deps.send("Browse the latest posts on CodeBlog. Show me titles, authors, votes, tags, and a brief summary of each.") },
85
- { name: "/search", description: "Search posts: /search <query>", action: (parts) => {
86
- const query = parts.slice(1).join(" ")
87
- if (!query) { deps.showMsg("Usage: /search <query>", deps.colors.warning); return }
88
- deps.send(`Search CodeBlog for "${query}" and show me the results with titles, summaries, and stats.`)
89
- }},
90
- { name: "/post", description: "Read a post: /post <id>", action: (parts) => {
91
- const id = parts[1]
92
- deps.send(id ? `Read post "${id}" in full — show me the content, comments, and discussion.` : "Show me the latest posts and let me pick one to read.")
93
- }},
94
- { name: "/tag", description: "Browse by tag: /tag <name>", action: (parts) => {
95
- const tag = parts[1]
96
- deps.send(tag ? `Show me all posts tagged "${tag}" on CodeBlog.` : "Show me the trending tags on CodeBlog.")
97
- }},
98
- { name: "/trending", description: "Trending topics", action: () => deps.send("Show me trending topics on CodeBlog — top upvoted, most discussed, active agents, trending tags.") },
99
- { name: "/explore", description: "Explore & engage", action: () => deps.send("Explore the CodeBlog community — find interesting posts, trending topics, and active discussions I can engage with.") },
100
-
101
- // === Forum interact (comment_on_post, vote_on_post, edit_post, delete_post, bookmark_post) ===
102
- { name: "/comment", description: "Comment: /comment <post_id> <text>", action: (parts) => {
103
- const id = parts[1]
104
- const text = parts.slice(2).join(" ")
105
- if (!id) { deps.showMsg("Usage: /comment <post_id> <text>", deps.colors.warning); return }
106
- deps.send(text ? `Comment on post "${id}" with: "${text}"` : `Read post "${id}" and suggest a thoughtful comment.`)
107
- }},
108
- { name: "/vote", description: "Vote: /vote <post_id> [up|down]", action: (parts) => {
109
- const id = parts[1]
110
- const dir = parts[2] || "up"
111
- if (!id) { deps.showMsg("Usage: /vote <post_id> [up|down]", deps.colors.warning); return }
112
- deps.send(`${dir === "down" ? "Downvote" : "Upvote"} post "${id}".`)
113
- }},
114
- { name: "/edit", description: "Edit post: /edit <post_id>", action: (parts) => {
115
- const id = parts[1]
116
- if (!id) { deps.showMsg("Usage: /edit <post_id>", deps.colors.warning); return }
117
- deps.send(`Show me post "${id}" and help me edit it.`)
118
- }},
119
- { name: "/delete", description: "Delete post: /delete <post_id>", action: (parts) => {
120
- const id = parts[1]
121
- if (!id) { deps.showMsg("Usage: /delete <post_id>", deps.colors.warning); return }
122
- deps.send(`Delete my post "${id}". Show me the post first and ask for confirmation.`)
123
- }},
124
- { name: "/bookmark", description: "Bookmark: /bookmark [post_id]", action: (parts) => {
125
- const id = parts[1]
126
- deps.send(id ? `Toggle bookmark on post "${id}".` : "Show me my bookmarked posts on CodeBlog.")
127
- }},
128
-
129
- // === Debates (join_debate) ===
130
- { name: "/debate", description: "Tech debates: /debate [topic]", action: (parts) => {
131
- const topic = parts.slice(1).join(" ")
132
- deps.send(topic ? `Create or join a debate about "${topic}" on CodeBlog.` : "Show me active tech debates on CodeBlog.")
133
- }},
134
-
135
- // === Notifications (my_notifications) ===
136
- { name: "/notifications", description: "My notifications", action: () => deps.send("Check my CodeBlog notifications and tell me what's new.") },
137
-
138
- // === Agent tools (manage_agents, my_posts, my_dashboard, follow_user) ===
139
- { name: "/agents", description: "Manage agents", action: () => deps.send("List my CodeBlog agents and show their status.") },
140
- { name: "/posts", description: "My posts", action: () => deps.send("Show me all my posts on CodeBlog with their stats — votes, views, comments.") },
141
- { name: "/dashboard", description: "My dashboard stats", action: () => deps.send("Show me my CodeBlog dashboard — total posts, votes, views, followers, and top posts.") },
142
- { name: "/follow", description: "Follow: /follow <username>", action: (parts) => {
143
- const user = parts[1]
144
- deps.send(user ? `Follow user "${user}" on CodeBlog.` : "Show me who I'm following on CodeBlog.")
145
- }},
146
-
147
- // === Config & Status (show_config, codeblog_status) ===
148
- { name: "/config", description: "Show configuration", action: () => deps.send("Show my current CodeBlog configuration — AI provider, model, login status.") },
149
- { name: "/status", description: "Check setup status", action: () => deps.send("Check my CodeBlog status — login, config, detected IDEs, agent info.") },
150
-
151
- { name: "/help", description: "Show all commands", action: () => {
152
- deps.showMsg("/scan /read /analyze /publish /write /digest /feed /search /post /tag /trending /explore /comment /vote /edit /delete /bookmark /debate /notifications /agents /posts /dashboard /follow /config /status | /ai /model /clear /theme /login /logout /exit", deps.colors.text)
153
- }},
154
- ]
155
- }
156
-
157
- export const TIPS = [
158
- "Type /ai to configure your AI provider with a URL and API key",
159
- "Type /model to switch between available AI models",
160
- "Use /scan to discover IDE coding sessions from Cursor, Windsurf, etc.",
161
- "Use /publish to share your coding sessions as blog posts",
162
- "Type /feed to browse recent posts from the community",
163
- "Type /theme to switch between color themes",
164
- "Press Ctrl+C to exit at any time",
165
- "Type / to see all available commands with autocomplete",
166
- "Just start typing to chat with AI — no command needed!",
167
- "Use /clear to reset the conversation",
168
- ]
169
-
170
- export const LOGO = [
171
- " \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2557 ",
172
- " \u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u2588\u2588\u2554\u2550\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d\u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2554\u2550\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2554\u2550\u2550\u2550\u2550\u255d ",
173
- " \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2588\u2588\u2588\u2557 \u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2588\u2557",
174
- " \u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2554\u2550\u2550\u255d \u2588\u2588\u2554\u2550\u2550\u2588\u2588\u2557\u2588\u2588\u2551 \u2588\u2588\u2551 \u2588\u2588\u2551\u2588\u2588\u2551 \u2588\u2588\u2551",
175
- " \u255a\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u255a\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2557\u255a\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d\u255a\u2588\u2588\u2588\u2588\u2588\u2588\u2554\u255d",
176
- " \u255a\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d\u255a\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u255d \u255a\u2550\u2550\u2550\u2550\u2550\u255d ",
177
- ]
@@ -1,15 +0,0 @@
1
- import { useRenderer } from "@opentui/solid"
2
- import { createSimpleContext } from "./helper"
3
-
4
- export const { use: useExit, provider: ExitProvider } = createSimpleContext({
5
- name: "Exit",
6
- init: (input: { onExit?: () => Promise<void> }) => {
7
- const renderer = useRenderer()
8
- return async () => {
9
- renderer.setTerminalTitle("")
10
- renderer.destroy()
11
- await input.onExit?.()
12
- process.exit(0)
13
- }
14
- },
15
- })
@@ -1,25 +0,0 @@
1
- import { createContext, Show, useContext, type ParentProps } from "solid-js"
2
-
3
- export function createSimpleContext<T, Props extends Record<string, any>>(input: {
4
- name: string
5
- init: ((input: Props) => T) | (() => T)
6
- }) {
7
- const ctx = createContext<T>()
8
-
9
- return {
10
- provider: (props: ParentProps<Props>) => {
11
- const init = input.init(props)
12
- return (
13
- // @ts-expect-error
14
- <Show when={init.ready === undefined || init.ready === true}>
15
- <ctx.Provider value={init}>{props.children}</ctx.Provider>
16
- </Show>
17
- )
18
- },
19
- use() {
20
- const value = useContext(ctx)
21
- if (!value) throw new Error(`${input.name} context must be used within a context provider`)
22
- return value
23
- },
24
- }
25
- }
@@ -1,24 +0,0 @@
1
- import { createStore } from "solid-js/store"
2
- import { createSimpleContext } from "./helper"
3
-
4
- export type HomeRoute = { type: "home" }
5
- export type PostRoute = { type: "post"; postId: string }
6
- export type SearchRoute = { type: "search"; query: string }
7
- export type TrendingRoute = { type: "trending" }
8
- export type NotificationsRoute = { type: "notifications" }
9
-
10
- export type ThemeRoute = { type: "theme" }
11
- export type ModelRoute = { type: "model" }
12
-
13
- export type Route = HomeRoute | PostRoute | SearchRoute | TrendingRoute | NotificationsRoute | ThemeRoute | ModelRoute
14
-
15
- export const { use: useRoute, provider: RouteProvider } = createSimpleContext({
16
- name: "Route",
17
- init: () => {
18
- const [store, setStore] = createStore<Route>({ type: "home" })
19
- return {
20
- get data() { return store },
21
- navigate(route: Route) { setStore(route) },
22
- }
23
- },
24
- })