2026-05-01 23:31:01 +02:00
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
2026-05-02 18:33:41 +02:00
|
|
|
import AddEntryForm from '@/components/Transactions/AddEntryForm';
|
2025-08-01 00:56:26 +02:00
|
|
|
import AddMilestoneForm from '@/components/Milestones/AddMilestoneForm';
|
2026-05-02 18:17:42 +02:00
|
|
|
import CreateTrackerStep from '@/components/Onboarding/CreateTrackerStep';
|
2026-05-01 23:31:01 +02:00
|
|
|
|
2025-08-01 00:56:26 +02:00
|
|
|
interface OnboardingStep {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
completed: boolean;
|
|
|
|
|
required: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 10:51:40 +02:00
|
|
|
const STEPS: OnboardingStep[] = [
|
2026-05-02 18:17:42 +02:00
|
|
|
{ id: 'entries', title: 'STARTING AMOUNT', description: 'Enter your starting amount', completed: false, required: true },
|
2026-05-01 23:31:01 +02:00
|
|
|
{ id: 'milestones', title: 'SET MILESTONES', description: 'Define your goals', completed: false, required: true },
|
|
|
|
|
];
|
|
|
|
|
|
2025-08-01 00:56:26 +02:00
|
|
|
interface OnboardingFlowProps {
|
|
|
|
|
onComplete?: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function OnboardingFlow({ onComplete }: OnboardingFlowProps) {
|
2026-05-02 18:17:42 +02:00
|
|
|
const [trackerCreated, setTrackerCreated] = useState(false);
|
2025-08-01 00:56:26 +02:00
|
|
|
const [currentStep, setCurrentStep] = useState(0);
|
2026-05-01 23:31:01 +02:00
|
|
|
const [steps, setSteps] = useState<OnboardingStep[]>([]);
|
2025-08-01 00:56:26 +02:00
|
|
|
|
2026-05-03 02:30:55 +02:00
|
|
|
// On mount: check if a tracker already exists and skip step 1 if so
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetch('/tracker')
|
|
|
|
|
.then(r => r.ok ? r.json() : null)
|
2026-05-03 02:38:38 +02:00
|
|
|
.then(data => {
|
|
|
|
|
if (data?.tracker) {
|
2026-05-03 02:30:55 +02:00
|
|
|
setTrackerCreated(true);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {});
|
|
|
|
|
}, []);
|
|
|
|
|
|
2026-05-01 23:31:01 +02:00
|
|
|
const checkOnboardingStatus = useCallback(async (currentSteps: OnboardingStep[]) => {
|
2025-08-01 00:56:26 +02:00
|
|
|
try {
|
2026-05-09 10:51:40 +02:00
|
|
|
const [entriesData, milestonesData] = await Promise.all([
|
2026-05-02 18:17:42 +02:00
|
|
|
fetch('/entries/summary').then(r => r.json()),
|
2026-05-01 23:31:01 +02:00
|
|
|
fetch('/milestones').then(r => r.json()),
|
|
|
|
|
]);
|
2025-08-01 00:56:26 +02:00
|
|
|
|
2026-05-02 18:17:42 +02:00
|
|
|
const hasEntries = entriesData.total_quantity > 0;
|
2025-08-01 00:56:26 +02:00
|
|
|
const hasMilestones = milestonesData.length > 0;
|
|
|
|
|
|
2026-05-01 23:31:01 +02:00
|
|
|
const freshSteps = currentSteps.map(step => ({
|
2025-08-01 00:56:26 +02:00
|
|
|
...step,
|
2026-05-01 22:02:13 +02:00
|
|
|
completed:
|
2026-05-02 18:17:42 +02:00
|
|
|
(step.id === 'entries' && hasEntries) ||
|
2026-05-09 10:51:40 +02:00
|
|
|
(step.id === 'milestones' && hasMilestones),
|
2026-05-01 22:02:13 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
setSteps(freshSteps);
|
|
|
|
|
|
|
|
|
|
const firstIncompleteRequired = freshSteps.findIndex(s => s.required && !s.completed);
|
|
|
|
|
|
|
|
|
|
if (firstIncompleteRequired !== -1) {
|
|
|
|
|
setCurrentStep(firstIncompleteRequired);
|
2026-05-01 23:31:01 +02:00
|
|
|
} else if (onComplete) {
|
|
|
|
|
onComplete();
|
2025-08-01 00:56:26 +02:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to check onboarding status:', error);
|
|
|
|
|
}
|
2026-05-01 23:31:01 +02:00
|
|
|
}, [onComplete]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-05-02 18:17:42 +02:00
|
|
|
if (!trackerCreated) return;
|
2026-05-01 23:31:01 +02:00
|
|
|
|
2026-05-09 10:51:40 +02:00
|
|
|
setSteps(STEPS);
|
2026-05-01 23:31:01 +02:00
|
|
|
setCurrentStep(0);
|
2026-05-09 10:51:40 +02:00
|
|
|
checkOnboardingStatus(STEPS);
|
|
|
|
|
}, [trackerCreated, checkOnboardingStatus]);
|
2026-05-02 18:17:42 +02:00
|
|
|
|
2026-05-09 10:51:40 +02:00
|
|
|
const handleTrackerCreated = () => {
|
2026-05-02 18:17:42 +02:00
|
|
|
setTrackerCreated(true);
|
|
|
|
|
};
|
2025-08-01 00:56:26 +02:00
|
|
|
|
|
|
|
|
const handleStepComplete = async () => {
|
2026-05-01 23:31:01 +02:00
|
|
|
const updatedSteps = steps.map((step, index) =>
|
2025-08-01 00:56:26 +02:00
|
|
|
index === currentStep ? { ...step, completed: true } : step
|
2026-05-01 23:31:01 +02:00
|
|
|
);
|
|
|
|
|
setSteps(updatedSteps);
|
|
|
|
|
await checkOnboardingStatus(updatedSteps);
|
2025-08-01 00:56:26 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleStepSelect = (stepIndex: number) => {
|
|
|
|
|
setCurrentStep(stepIndex);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const renderStepContent = () => {
|
|
|
|
|
const step = steps[currentStep];
|
2026-05-02 18:17:42 +02:00
|
|
|
if (!step) return null;
|
2026-05-01 23:31:01 +02:00
|
|
|
|
2025-08-01 00:56:26 +02:00
|
|
|
switch (step.id) {
|
2026-05-02 18:17:42 +02:00
|
|
|
case 'entries':
|
2026-05-09 10:51:40 +02:00
|
|
|
return <AddEntryForm onSuccess={handleStepComplete} />;
|
2025-08-01 00:56:26 +02:00
|
|
|
case 'milestones':
|
2026-05-01 23:31:01 +02:00
|
|
|
return <AddMilestoneForm onSuccess={handleStepComplete} />;
|
2025-08-01 00:56:26 +02:00
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="min-h-screen bg-black flex items-center justify-center p-4">
|
|
|
|
|
<div className="w-full max-w-4xl">
|
|
|
|
|
<div className="border-2 border-red-500 bg-black shadow-[0_0_20px_rgba(239,68,68,0.3)] p-8">
|
|
|
|
|
<div className="mb-8">
|
|
|
|
|
<h1 className="text-red-400 font-mono text-2xl font-bold uppercase tracking-wider mb-2">
|
|
|
|
|
[SYSTEM] ONBOARDING SEQUENCE
|
|
|
|
|
</h1>
|
|
|
|
|
<p className="text-red-400/60 font-mono text-sm">
|
2026-05-02 18:17:42 +02:00
|
|
|
{!trackerCreated ? 'Set up your tracker' : 'Configure your tracker'}
|
2025-08-01 00:56:26 +02:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-05-02 18:17:42 +02:00
|
|
|
{!trackerCreated ? (
|
2026-05-01 23:31:01 +02:00
|
|
|
<div className="border border-red-500/30 bg-black/50 p-6">
|
2026-05-02 18:17:42 +02:00
|
|
|
<CreateTrackerStep onSuccess={handleTrackerCreated} />
|
2025-08-01 00:56:26 +02:00
|
|
|
</div>
|
2026-05-01 23:31:01 +02:00
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<div className="mb-8">
|
|
|
|
|
<div className="flex items-center justify-between mb-4">
|
|
|
|
|
{steps.map((step, index) => (
|
|
|
|
|
<button
|
|
|
|
|
key={step.id}
|
|
|
|
|
onClick={() => handleStepSelect(index)}
|
|
|
|
|
className={`flex-1 px-4 py-2 font-mono text-xs uppercase tracking-wider border border-red-500/50 transition-all ${
|
|
|
|
|
index === currentStep
|
|
|
|
|
? 'bg-red-500 text-black border-red-500'
|
|
|
|
|
: step.completed
|
|
|
|
|
? 'bg-red-950/50 text-red-300 border-red-400'
|
|
|
|
|
: 'bg-black text-red-400/60 hover:text-red-400 hover:border-red-400'
|
|
|
|
|
} ${index > 0 ? 'ml-2' : ''}`}
|
|
|
|
|
>
|
2026-05-09 10:51:40 +02:00
|
|
|
{step.completed ? '[✓]' : '[REQ]'} {step.title}
|
2026-05-01 23:31:01 +02:00
|
|
|
</button>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2025-08-01 00:56:26 +02:00
|
|
|
|
2026-05-01 23:31:01 +02:00
|
|
|
<div className="text-center">
|
|
|
|
|
<p className="text-red-400 font-mono text-sm">
|
|
|
|
|
{steps[currentStep]?.description}
|
|
|
|
|
</p>
|
|
|
|
|
<p className="text-red-400/60 font-mono text-xs mt-1">
|
|
|
|
|
STEP {currentStep + 1}/{steps.length}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2025-08-01 00:56:26 +02:00
|
|
|
|
2026-05-01 23:31:01 +02:00
|
|
|
<div className="border border-red-500/30 bg-black/50 p-6">
|
|
|
|
|
{renderStepContent()}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="mt-6 pt-4 border-t border-red-500/30">
|
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
|
<p className="text-red-400/60 font-mono text-xs">
|
|
|
|
|
[STATUS] {steps.filter(s => s.completed).length}/{steps.length} STEPS COMPLETE
|
|
|
|
|
</p>
|
|
|
|
|
<p className="text-red-400/60 font-mono text-xs">
|
2026-05-09 10:51:40 +02:00
|
|
|
{steps.filter(s => !s.completed).length} REQUIRED REMAINING
|
2026-05-01 23:31:01 +02:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2025-08-01 00:56:26 +02:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-05-01 23:31:01 +02:00
|
|
|
}
|