Skip to content

Commit 4ad9759

Browse files
elemdosclaude
andcommitted
fix: improve project handling and routing UX
- Guard against undefined project_id in data module generation - Reset auth throttle on token expiry for immediate re-auth - Add null-check in getProject - Redirect first-time users to new-kit page - Update routes to use /tinykit instead of /tinykit/dashboard - Remove unnecessary auto-build on kit creation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b3ac520 commit 4ad9759

File tree

8 files changed

+34
-23
lines changed

8 files changed

+34
-23
lines changed

src/lib/compiler/iframe.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,22 @@ export const generate_design_css = (design) => {
2121
* @returns {string}
2222
*/
2323
const generate_data_module = (project_id, collections) => {
24+
// Guard against undefined/null project_id being stringified as 'undefined'/'null'
25+
const safe_project_id = project_id || ''
26+
2427
const collection_entries = collections
2528
.map(name => ` ${name}: create_collection('${name}')`)
2629
.join(',\n')
2730

2831
return `
29-
const PROJECT_ID = '${project_id}'
32+
const PROJECT_ID = '${safe_project_id}'
3033
const API_BASE = '/_tk/data'
3134
35+
// Validate PROJECT_ID is set
36+
if (!PROJECT_ID) {
37+
console.warn('[db] No project ID configured - data operations will fail')
38+
}
39+
3240
// Global registry for realtime updates
3341
const _collections = {}
3442

src/lib/components/Preview.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,20 @@
139139
},
140140
);
141141
142-
// Watch for content/design/data_files changes only (not agent_chat or record data)
142+
// Watch for content/design/data_files changes AND project_id changes
143143
// Using JSON.stringify ensures we only react to actual value changes, not reference changes
144+
// Including project_id ensures srcdoc is recreated when project changes
144145
watch(
145146
() => store?.project
146147
? JSON.stringify({
148+
project_id, // Include project_id to trigger recreation when it changes
147149
content: store.content,
148150
design: store.design,
149151
data_files: store.data_files
150152
})
151153
: null,
152154
() => {
153-
if (store?.project) {
155+
if (store?.project && project_id) {
154156
apply_config_update(
155157
store.content || [],
156158
store.design || [],

src/lib/server/pb.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export async function ensureAuth(): Promise<boolean> {
8888
if (isAuthenticated && !pb.authStore.isValid) {
8989
console.log('[PB] Auth token expired, re-authenticating...')
9090
isAuthenticated = false
91+
lastCredentialsCheck = 0 // Reset throttle so we can re-auth immediately
9192
}
9293

9394
// Check for new config every 5 seconds if not authenticated
@@ -228,6 +229,8 @@ export async function listProjects(): Promise<Project[]> {
228229
* Get a project by ID
229230
*/
230231
export async function getProject(id: string): Promise<Project | null> {
232+
if (!id) return null
233+
231234
const authed = await ensureAuth()
232235
if (!authed) {
233236
console.error(`[PB] Cannot get project ${id}: Server auth not available`)

src/routes/setup/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
144144
// Setup complete, auto-login with the same credentials
145145
await auth.login(email, password);
146-
goto("/tinykit/dashboard");
146+
goto("/tinykit");
147147
} catch (err: any) {
148148
error = err.message || "Setup failed. Please try again.";
149149
} finally {

src/routes/tinykit/+page.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
8787
// Load data
8888
await Promise.all([load_projects(), load_kits()]);
89+
90+
// First-time user with no projects? Guide them to create one
91+
if (projects.length === 0 && kits.length === 0) {
92+
goto("/tinykit/new-kit");
93+
}
8994
});
9095
9196
async function load_projects() {

src/routes/tinykit/lib/api.svelte.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,15 +583,23 @@ function generate_production_html({ body, head = '', hydration_js, config }: Pro
583583
* Includes realtime subscription support via PocketBase
584584
*/
585585
function generate_data_module(project_id: string, collections: string[]): string {
586+
// Guard against undefined/null project_id being stringified
587+
const safe_project_id = project_id || ''
588+
586589
// Generate collection entries
587590
const collection_entries = collections
588591
.map(name => ` ${name}: create_collection('${name}')`)
589592
.join(',\n')
590593

591594
return `
592-
const PROJECT_ID = '${project_id}'
595+
const PROJECT_ID = '${safe_project_id}'
593596
const API_BASE = '/_tk/data'
594597
598+
// Validate PROJECT_ID is set
599+
if (!PROJECT_ID) {
600+
console.warn('[db] No project ID configured - data operations will fail')
601+
}
602+
595603
// Global registry for realtime updates
596604
const _collections = {}
597605

src/routes/tinykit/new-kit/+page.svelte

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import { auth, pb } from "$lib/pocketbase.svelte";
1010
import { get_featured_kits_with_templates, type KitWithTemplates, type Template } from "$lib/templates";
1111
import { processCode, dynamic_iframe_srcdoc, generate_design_css } from "$lib/compiler/init";
12-
import { build_app } from "../lib/api.svelte";
1312
1413
const kits = get_featured_kits_with_templates();
1514
let is_creating = $state(false);
@@ -176,22 +175,8 @@
176175
// Batch create selected templates
177176
const created_projects = await project_service.batch_create_kit(kit.id, templates_to_create);
178177
179-
// Build all projects in parallel for preview thumbnails
180-
console.log(`Building ${created_projects.length} projects...`);
181-
const build_results = await Promise.all(
182-
created_projects.map(p =>
183-
build_app(p.id)
184-
.then(res => {
185-
console.log(`Built ${p.name}:`, res);
186-
return { success: true, project: p.name };
187-
})
188-
.catch(err => {
189-
console.error(`Failed to build ${p.name}:`, err);
190-
return { success: false, project: p.name, error: err };
191-
})
192-
)
193-
);
194-
console.log('Build results:', build_results);
178+
// Note: We don't auto-build here - thumbnails only show for deployed apps
179+
// Users can deploy individual apps when ready
195180
196181
// Redirect to dashboard with this kit selected
197182
goto(`/tinykit?kit=${kit.id}`);

src/routes/tinykit/settings/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
<header class="border-b border-[var(--builder-border)] px-6 py-4">
251251
<div class="max-w-2xl mx-auto flex items-center gap-4">
252252
<a
253-
href="/tinykit/dashboard"
253+
href="/tinykit"
254254
class="p-2 -ml-2 text-[var(--builder-text-secondary)] hover:text-[var(--builder-text-primary)] transition-colors"
255255
>
256256
<ArrowLeft class="w-5 h-5" />

0 commit comments

Comments
 (0)