@@ -9,9 +9,55 @@ import { useDenFlow } from "../_providers/den-flow-provider";
99
1010type SettingsTab = "profile" | "organizations" ;
1111
12+ const PENDING_ORG_DRAFT_STORAGE_KEY = "openwork-den-pending-org-draft" ;
13+
14+ function readPendingOrgDraft ( userEmail : string | null | undefined ) {
15+ if ( typeof window === "undefined" || ! userEmail ) {
16+ return null ;
17+ }
18+
19+ const raw = window . localStorage . getItem ( PENDING_ORG_DRAFT_STORAGE_KEY ) ;
20+ if ( ! raw ) {
21+ return null ;
22+ }
23+
24+ try {
25+ const parsed = JSON . parse ( raw ) as { name ?: unknown ; email ?: unknown } ;
26+ if ( typeof parsed . name !== "string" || typeof parsed . email !== "string" ) {
27+ return null ;
28+ }
29+
30+ return parsed . email === userEmail ? parsed . name . trim ( ) : null ;
31+ } catch {
32+ return null ;
33+ }
34+ }
35+
36+ function writePendingOrgDraft ( name : string , userEmail : string | null | undefined ) {
37+ if ( typeof window === "undefined" || ! userEmail ) {
38+ return ;
39+ }
40+
41+ window . localStorage . setItem (
42+ PENDING_ORG_DRAFT_STORAGE_KEY ,
43+ JSON . stringify ( {
44+ name,
45+ email : userEmail ,
46+ } ) ,
47+ ) ;
48+ }
49+
50+ function clearPendingOrgDraft ( ) {
51+ if ( typeof window === "undefined" ) {
52+ return ;
53+ }
54+
55+ window . localStorage . removeItem ( PENDING_ORG_DRAFT_STORAGE_KEY ) ;
56+ }
57+
1258export function OrganizationScreen ( ) {
1359 const router = useRouter ( ) ;
14- const { user, sessionHydrated, signOut } = useDenFlow ( ) ;
60+ const { user, sessionHydrated, signOut, billingSummary } = useDenFlow ( ) ;
1561 const [ orgs , setOrgs ] = useState < DenOrgSummary [ ] > ( [ ] ) ;
1662 const [ busy , setBusy ] = useState ( true ) ;
1763 const [ error , setError ] = useState < string | null > ( null ) ;
@@ -20,6 +66,7 @@ export function OrganizationScreen() {
2066 const [ createName , setCreateName ] = useState ( "" ) ;
2167 const [ createBusy , setCreateBusy ] = useState ( false ) ;
2268 const [ createError , setCreateError ] = useState < string | null > ( null ) ;
69+ const [ hasPendingOrgDraft , setHasPendingOrgDraft ] = useState ( false ) ;
2370
2471 const userDisplayName = useMemo ( ( ) => {
2572 const trimmedName = user ?. name ?. trim ( ) ;
@@ -35,6 +82,8 @@ export function OrganizationScreen() {
3582
3683 const activeOrg = useMemo ( ( ) => orgs . find ( ( org ) => org . isActive ) ?? null , [ orgs ] ) ;
3784 const showDirectCreateFlow = orgs . length === 0 ;
85+ const returningToSavedDraft = showDirectCreateFlow && hasPendingOrgDraft ;
86+ const draftReadyAfterCheckout = returningToSavedDraft && Boolean ( billingSummary ?. hasActivePlan ) ;
3887
3988 useEffect ( ( ) => {
4089 if ( ! sessionHydrated ) return ;
@@ -74,6 +123,28 @@ export function OrganizationScreen() {
74123 } ;
75124 } , [ sessionHydrated , user , router ] ) ;
76125
126+ useEffect ( ( ) => {
127+ if ( ! user ?. email ) {
128+ setHasPendingOrgDraft ( false ) ;
129+ return ;
130+ }
131+
132+ if ( ! showDirectCreateFlow ) {
133+ clearPendingOrgDraft ( ) ;
134+ setHasPendingOrgDraft ( false ) ;
135+ return ;
136+ }
137+
138+ const savedName = readPendingOrgDraft ( user . email ) ;
139+ if ( ! savedName ) {
140+ setHasPendingOrgDraft ( false ) ;
141+ return ;
142+ }
143+
144+ setCreateName ( ( current ) => current || savedName ) ;
145+ setHasPendingOrgDraft ( true ) ;
146+ } , [ showDirectCreateFlow , user ?. email ] ) ;
147+
77148 async function handleCreate ( e : React . FormEvent ) {
78149 e . preventDefault ( ) ;
79150 const trimmed = createName . trim ( ) ;
@@ -89,6 +160,8 @@ export function OrganizationScreen() {
89160
90161 if ( ! response . ok ) {
91162 if ( response . status === 402 ) {
163+ writePendingOrgDraft ( trimmed , user ?. email ) ;
164+ setHasPendingOrgDraft ( true ) ;
92165 setCreateBusy ( false ) ;
93166 router . push ( "/checkout" ) ;
94167 return ;
@@ -106,6 +179,8 @@ export function OrganizationScreen() {
106179 throw new Error ( "Organization was created, but no slug was returned." ) ;
107180 }
108181
182+ clearPendingOrgDraft ( ) ;
183+ setHasPendingOrgDraft ( false ) ;
109184 router . push ( getOrgDashboardRoute ( nextSlug ) ) ;
110185 } catch ( err ) {
111186 setCreateError ( err instanceof Error ? err . message : "Failed to create organization." ) ;
@@ -153,9 +228,15 @@ export function OrganizationScreen() {
153228 </ div >
154229 < div className = "min-w-0" >
155230 < p className = "text-sm font-medium uppercase tracking-[0.18em] text-gray-400" > OpenWork Cloud</ p >
156- < h1 className = "mt-2 text-3xl font-semibold tracking-[-0.03em] text-gray-950" > Create your first organization.</ h1 >
231+ < h1 className = "mt-2 text-3xl font-semibold tracking-[-0.03em] text-gray-950" >
232+ { returningToSavedDraft ? "Finish creating your organization." : "Create your first organization." }
233+ </ h1 >
157234 < p className = "mt-3 max-w-xl text-sm leading-6 text-gray-500" >
158- Name the workspace you want to set up for your team. If billing is enabled, the plan step comes right after this.
235+ { draftReadyAfterCheckout
236+ ? "Your plan is ready. Confirm the workspace name below and create the workspace to finish setup."
237+ : returningToSavedDraft
238+ ? "We saved your workspace name so you can pick up right where you left off."
239+ : "Name the workspace you want to set up for your team. If billing is enabled, the plan step comes right after this." }
159240 </ p >
160241 </ div >
161242 </ div >
@@ -188,7 +269,7 @@ export function OrganizationScreen() {
188269 disabled = { createBusy || ! createName . trim ( ) }
189270 className = "rounded-2xl bg-gray-900 px-5 py-3 text-sm font-medium text-white transition-colors hover:bg-gray-800 disabled:opacity-50"
190271 >
191- { createBusy ? "Creating..." : "Continue" }
272+ { createBusy ? "Creating..." : returningToSavedDraft ? "Create organization" : "Continue" }
192273 </ button >
193274 < p className = "text-sm text-gray-500" > Signed in as { user ?. email ?? "your account" } </ p >
194275 </ div >
0 commit comments