Skip to content

Commit

Permalink
fix:coderabbitai
Browse files Browse the repository at this point in the history
  • Loading branch information
Innocent-Akim committed Nov 14, 2024
1 parent 3a2a258 commit 40cafab
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions apps/web/lib/components/custom-select/multi-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,45 @@ export function MultiSelect<T>({
removeItems,
localStorageKey = "select-items-selected",
}: MultiSelectProps<T>) {
const [selectedItems, setSelectedItems] = useState<T[]>(
JSON.parse(typeof window !== 'undefined'
&& window.localStorage.getItem(localStorageKey) as any) || []
);
const [selectedItems, setSelectedItems] = useState<T[]>(() => {
if (typeof window === 'undefined') return [];
try {
const saved = localStorage.getItem(localStorageKey);
return saved ? JSON.parse(saved) : [];
} catch {
return [];
}
});
const [isPopoverOpen, setPopoverOpen] = useState(false);
const [popoverWidth, setPopoverWidth] = useState<number | null>(null);
const triggerRef = useRef<HTMLButtonElement>(null);

// Load selected items from localStorage on component mount
useEffect(() => {
const savedItems = localStorage.getItem(localStorageKey);
if (savedItems) {
setSelectedItems(typeof window !== 'undefined' && JSON.parse(savedItems));
} else if (defaultValue) {
if (defaultValue) {
const initialItems = Array.isArray(defaultValue) ? defaultValue : [defaultValue];
setSelectedItems(initialItems);
if (onValueChange) onValueChange(multiSelect ? initialItems : initialItems[0]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [defaultValue, setSelectedItems]);

useEffect(() => {
if (onValueChange) {
onValueChange(multiSelect ? selectedItems : selectedItems[0] || null);
}
}, [selectedItems, multiSelect, onValueChange]);

// Save selected items to localStorage whenever they change
// Handle persistence
useEffect(() => {
if (typeof window !== 'undefined') {
localStorage.setItem(localStorageKey, JSON.stringify(selectedItems));
if (onValueChange) {
onValueChange(multiSelect ? selectedItems : selectedItems[0] || null);
try {
localStorage.setItem(localStorageKey, JSON.stringify(selectedItems));
} catch (error) {
console.error('Failed to save to localStorage:', error);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedItems]);
}, [selectedItems, localStorageKey]);

const onClick = (item: T) => {
let newSelectedItems: T[];
Expand Down

0 comments on commit 40cafab

Please sign in to comment.