@@ -12,7 +12,9 @@ import {
1212import { Separator } from '@components/ui/separator' ;
1313import type { SupportedLocale } from '@lib/config/language-config' ;
1414import { getLanguageInfo } from '@lib/config/language-config' ;
15+ import { cleanupUnusedImages } from '@lib/services/content-image-upload-service' ;
1516import { useAboutEditorStore } from '@lib/stores/about-editor-store' ;
17+ import { createClient } from '@lib/supabase/client' ;
1618import {
1719 AboutTranslationData ,
1820 ComponentInstance ,
@@ -27,7 +29,7 @@ import {
2729} from '@lib/utils/performance' ;
2830import { GripVertical , Plus , Redo2 , Trash2 , Undo2 } from 'lucide-react' ;
2931
30- import React , { useCallback , useEffect , useMemo } from 'react' ;
32+ import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
3133
3234import { useTranslations } from 'next-intl' ;
3335
@@ -58,6 +60,9 @@ export function AboutEditor({
5860} : AboutEditorProps ) {
5961 const t = useTranslations ( 'pages.admin.content.editor' ) ;
6062
63+ // User ID state for image cleanup
64+ const [ userId , setUserId ] = useState < string | null > ( null ) ;
65+
6166 // Context menu state
6267 const [ contextMenu , setContextMenu ] = React . useState < {
6368 x : number ;
@@ -131,6 +136,21 @@ export function AboutEditor({
131136 return null ;
132137 } , [ pageContent , contextMenu ?. componentId ] ) ;
133138
139+ // Fetch current user ID on mount
140+ useEffect ( ( ) => {
141+ const fetchUserId = async ( ) => {
142+ const supabase = createClient ( ) ;
143+ const {
144+ data : { user } ,
145+ } = await supabase . auth . getUser ( ) ;
146+ if ( user ) {
147+ setUserId ( user . id ) ;
148+ }
149+ } ;
150+
151+ fetchUserId ( ) ;
152+ } , [ ] ) ;
153+
134154 // Initial load and locale change handling
135155 useEffect ( ( ) => {
136156 if ( currentTranslation . sections ) {
@@ -150,9 +170,19 @@ export function AboutEditor({
150170
151171 // Debounced auto-save function
152172 const debouncedSave = useDebouncedCallback (
153- useCallback ( ( ) => {
173+ useCallback ( async ( ) => {
154174 if ( ! pageContent ) return ;
155175
176+ // Clean up unused images before saving (only if userId is available)
177+ if ( userId && pageContent . sections ) {
178+ try {
179+ await cleanupUnusedImages ( pageContent . sections , userId ) ;
180+ } catch ( error ) {
181+ console . error ( 'Failed to cleanup unused images:' , error ) ;
182+ // Continue with save even if cleanup fails
183+ }
184+ }
185+
156186 const updatedTranslation : AboutTranslationData = {
157187 sections : pageContent . sections ,
158188 metadata : {
@@ -167,9 +197,15 @@ export function AboutEditor({
167197 } ;
168198
169199 onTranslationsChange ( newTranslations ) ;
170- } , [ pageContent , translations , currentLocale , onTranslationsChange ] ) ,
200+ } , [
201+ pageContent ,
202+ translations ,
203+ currentLocale ,
204+ onTranslationsChange ,
205+ userId ,
206+ ] ) ,
171207 100 , // 100ms delay for near real-time sync
172- [ pageContent , translations , currentLocale ]
208+ [ pageContent , translations , currentLocale , userId ]
173209 ) ;
174210
175211 // Auto-save when pageContent changes
0 commit comments