Spaces:
Build error
Build error
File size: 2,260 Bytes
9b63060 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import { useState, useCallback } from 'react';
export const useBrowserState = () => {
const [historyStack, setHistoryStack] = useState<string[]>([]);
const [currentIndex, setCurrentIndex] = useState(-1);
const [bookmarks, setBookmarks] = useState<string[]>([]);
const [summary, setSummary] = useState('');
const [isSummaryVisible, setIsSummaryVisible] = useState(false);
const loadUrl = useCallback(async (url: string, pushToHistory = true) => {
try {
console.log('Loading URL:', url);
if (pushToHistory) {
setHistoryStack(prev => {
const newStack = prev.slice(0, currentIndex + 1);
newStack.push(url);
return newStack;
});
setCurrentIndex(prev => prev + 1);
}
return url;
} catch (error) {
console.error('Error loading URL:', error);
throw error;
}
}, [currentIndex]);
const goBack = useCallback(() => {
if (currentIndex > 0) {
const newIndex = currentIndex - 1;
setCurrentIndex(newIndex);
return historyStack[newIndex];
}
return null;
}, [currentIndex, historyStack]);
const goForward = useCallback(() => {
if (currentIndex < historyStack.length - 1) {
const newIndex = currentIndex + 1;
setCurrentIndex(newIndex);
return historyStack[newIndex];
}
return null;
}, [currentIndex, historyStack]);
const addBookmark = useCallback(() => {
if (currentIndex >= 0) {
const currentUrl = historyStack[currentIndex];
if (currentUrl && !bookmarks.includes(currentUrl)) {
setBookmarks(prev => [...prev, currentUrl]);
console.log('Bookmark added:', currentUrl);
}
}
}, [currentIndex, historyStack, bookmarks]);
const setSummaryAndShow = useCallback((summaryText: string) => {
setSummary(summaryText);
setIsSummaryVisible(true);
}, []);
const hideSummary = useCallback(() => {
setIsSummaryVisible(false);
}, []);
return {
historyStack,
currentIndex,
bookmarks,
summary,
isSummaryVisible,
canGoBack: currentIndex > 0,
canGoForward: currentIndex < historyStack.length - 1,
loadUrl,
goBack,
goForward,
addBookmark,
setSummaryAndShow,
hideSummary
};
};
|