import { AccountCommunityType } from "@openchat/types" import { useAccountCommunities }…
- User
import { AccountCommunityType } from "@openchat/types" import { useAccountCommunities } from "@openchat/ui" import React, { useEffect, useState } from "react" import { NestableDraggableFlatList, NestableScrollContainer, RenderItemParams, } from "react-native-draggable-flatlist" import { AppController } from "../../../../app/app.globals" import { DashboardTabsCommunityTabButton } from "../dashboard-tabs-community-tab-button/dashboard-tabs-community-tab-button.component" import { useDashboardSettings } from "../../../../hook/use-dashboard-settings.hook" import { ArrayUtils } from "@openchat/core"
// @reviewed export function DashboardTabsCommunityButtons() { // --------------------------------------------------------------------------- // variables // ---------------------------------------------------------------------------
const { setSelectedTab } = useDashboardSettings() const allAccountCommunities = useAccountCommunities() const [orderedAccountCommunities, setOrderedAccountCommunities] = useState( () => ArrayUtils.sortAsc(allAccountCommunities, "dashboardTabOrder"), ) const [draggableIndex, setDraggableIndex] = useState<string | null>(null)
// --------------------------------------------------------------------------- // effects // ---------------------------------------------------------------------------
useEffect(() => { setOrderedAccountCommunities( ArrayUtils.sortAsc(allAccountCommunities, "dashboardTabOrder"), ) }, [allAccountCommunities])
// --------------------------------------------------------------------------- return ( <NestableScrollContainer nestedScrollEnabled showsVerticalScrollIndicator={false} style={{ flexShrink: 1, flexGrow: 0, width: "100%", paddingVertical: orderedAccountCommunities.length > 0 ? 8 : 0, }} > <NestableDraggableFlatList data={orderedAccountCommunities} keyExtractor={(accountCommunity) => accountCommunity.id} renderItem={({ item: accountCommunity, drag, }: RenderItemParams) => ( <DashboardTabsCommunityTabButton accountCommunity={accountCommunity} drag={drag} communityDraggableIndex={draggableIndex} onCommunityIconClick={(communityId) => { setSelectedTab({ type: "community", communityId: communityId, roomId: null, }) }} /> )} onDragBegin={(index) => { setDraggableIndex(orderedAccountCommunities[index].id) }} onDragEnd={async (result) => { const draggedAccountCommunities: AccountCommunityType[] = result.data setDraggableIndex(null) setOrderedAccountCommunities(draggedAccountCommunities) await AppController.accountCommunity.reorderInDashboardTabs({ accountCommunityIds: draggedAccountCommunities.map( (accountCommunity) => accountCommunity.id, ), }) }} /> ) } auto scrolling to new accountCommunity active item
- Assistant
To achieve auto-scrolling to the newly active accountCommunity item in your list, you would need to adjust your implementation to programmatically scroll the list to the item of interest when it becomes active. This requires a reference to the flat list and a method to calculate the scroll position based on the active item's index or other identifiable property.
Below is an adjusted version of your component with added functionality to auto-scroll to the newly active community tab button. Please note, that for simplicity, the exact calculation for the scrollToPosition (e.g., taking into account item sizes, margins, etc.) might need adjustments to fit your exact UI layout and item heights:
import React, { useEffect, useState, useRef } from 'react'; import { NestableDraggableFlatList, NestableScrollContainer, RenderItemParams, } from 'react-native-draggable-flatlist'; import { DashboardTabsCommunityTabButton } from '../dashboard-tabs-community-tab-button/dashboard-tabs-community-tab-button.component'; import { useDashboardSettings } from '../../../../hook/use-dashboard-settings.hook'; import { ArrayUtils } from '@openchat/core'; import { useAccountCommunities } from '@openchat/ui'; import { AppController } from '../../../../app/app.globals'; export function DashboardTabsCommunityButtons() { const { setSelectedTab, selectedTab } = useDashboardSettings(); const allAccountCommunities = useAccountCommunities(); const [orderedAccountCommunities, setOrderedAccountCommunities] = useState(() => ArrayUtils.sortAsc(allAccountCommunities, 'dashboardTabOrder') ); const [draggableIndex, setDraggableIndex] = useState<string | null>(null); const flatListRef = useRef(null); useEffect(() => { setOrderedAccountCommunities(ArrayUtils.sortAsc(allAccountCommunities, 'dashboardTabOrder')); }, [allAccountCommunities]); // Scroll to active community tab button useEffect(() => { if (selectedTab?.type === 'community') { const index = orderedAccountCommunities.findIndex(accountCommunity => accountCommunity.id === selectedTab.communityId); if (index !== -1 && flatListRef.current) { flatListRef.current.scrollToIndex({ animated: true, index }); } } }, [selectedTab, orderedAccountCommunities]); return ( <NestableScrollContainer nestedScrollEnabled showsVerticalScrollIndicator={false} style={{ flexShrink: 1, flexGrow: 0, width: '100%', paddingVertical: orderedAccountCommunities.length > 0 ? 8 : 0, }} > <NestableDraggableFlatList ref={flatListRef} data={orderedAccountCommunities} keyExtractor={(accountCommunity) => accountCommunity.id} renderItem={({ item: accountCommunity, drag }: RenderItemParams<any>) => ( <DashboardTabsCommunityTabButton accountCommunity={accountCommunity} drag={drag} communityDraggableIndex={draggableIndex} onCommunityIconClick={(communityId) => { setSelectedTab({ type: 'community', communityId: communityId, roomId: null, }); }} /> )} onDragBegin={(index) => { setDraggableIndex(orderedAccountCommunities[index].id); }} onDragEnd={async (result) => { const draggedAccountCommunities = result.data; setDraggableIndex(null); setOrderedAccountCommunities(draggedAccountCommunities); await AppController.accountCommunity.reorderInDashboardTabs({ accountCommunityIds: draggedAccountCommunities.map(accountCommunity => accountCommunity.id), }); }} /> </NestableScrollContainer> ); }I've added a second
useEffecthook that listens for changes on theselectedTabstate. When theselectedTabchanges to a type of 'community', it locates the index of the active community in the ordered list and scrolls to that item using the.scrollToIndex()method provided by the flat list'sref.You'll need to ensure that your
useDashboardSettingshook provides access to the currentlyselectedTab, and adjust the item size/margin calculations based on your UI for the smoothest user experience.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.