$request->user() ->stores() ->latest() ->get() ->map(fn (Store $store) => $this->present($store)), ]); } /** * Show the "create a store" form. */ public function create(): Response { return Inertia::render('stores/create', [ 'categories' => StoreCategory::options(), 'languages' => StoreLanguage::options(), ]); } /** * Save a new store for the current user. */ public function store(CreateStoreRequest $request): RedirectResponse { $store = $request->user()->stores()->create($request->validated()); Inertia::flash('toast', ['type' => 'success', 'message' => __('Store created.')]); return to_route('stores.show', $store->slug); } /** * Show one store. Looking it up through the user means other people's stores give a 404. */ public function show(Request $request, UsageLimits $usage, string $slug): Response { $store = $request->user()->stores()->where('slug', $slug)->firstOrFail(); StoreGeneration::failStale($store->id); $latest = $store->generations()->first(); return Inertia::render('stores/show', [ 'store' => $this->present($store), // The newest AI job. The page keeps refreshing while it is queued or running. 'generation' => $latest ? $this->presentGeneration($latest) : null, 'edits' => $store->generations() ->where('type', GenerationType::Edit) ->limit(5) ->get() ->map(fn (StoreGeneration $edit) => $this->presentGeneration($edit)), 'usage' => $usage->for($request->user()), ]); } /** * The store fields the frontend is allowed to see. * * @return array */ private function present(Store $store): array { return [ 'id' => $store->id, 'name' => $store->name, 'slug' => $store->slug, 'category' => $store->category->value, 'category_label' => $store->category->label(), 'description' => $store->description, 'language' => $store->language->value, 'language_label' => $store->language->label(), 'status' => $store->status->value, 'status_label' => $store->status->label(), 'has_design' => $store->plan !== null, // Changes whenever the design is saved, so the preview knows to reload. 'design_version' => $store->updated_at?->getTimestamp(), 'created_at' => $store->created_at?->toIso8601String(), ]; } /** * The AI job fields the frontend is allowed to see. * * @return array */ private function presentGeneration(StoreGeneration $generation): array { return [ 'id' => $generation->id, 'type' => $generation->type->value, 'status' => $generation->status->value, 'instruction' => $generation->instruction, 'reply' => $generation->reply, 'error' => $generation->error, 'created_at' => $generation->created_at?->toIso8601String(), ]; } }