Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { useMobileI18n } from "../../lib/i18n";
import type { EnvironmentThreadShell } from "@akeru/client-runtime/state/shell";
import { threadSearchMatchKey } from "@akeru/client-runtime/state/thread-search";
import { LegendList } from "@legendapp/list/react-native";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { LayoutChangeEvent } from "react-native";
import { Platform, Pressable, StyleSheet, TextInput, View } from "react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import type { SearchBarCommands } from "react-native-screens";
import { AppText as Text } from "../../components/AppText";
import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
import { NativeStackScreenOptions } from "../../native/StackHeader";
import { scopedProjectKey, scopedThreadKey } from "../../lib/scopedEntities";
import { useThemeColor } from "../../lib/useThemeColor";
import { useProjects, useThreadShells } from "../../state/entities";
import { usePendingNewTasks } from "../../state/use-pending-new-tasks";
import { useWorkspaceState } from "../../state/workspace";
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
import { useHomeListOptions } from "../home/home-list-options";
import { buildHomeListFilterMenu } from "../home/home-list-filter-menu";
import { SwipeableScrollGateProvider, useSwipeableScrollGate } from "../home/thread-swipe-actions";
import { usePendingTaskListActions } from "../home/usePendingTaskListActions";
import { useThreadListActions } from "../home/useThreadListActions";
import { getConnectionAwareBrandHeaderOptions } from "../home/WorkspaceConnectionTitle";
import { createSidebarHeaderItems } from "./sidebar-native-header-items";
import { SidebarNavigationShell } from "./sidebar-navigation-shell";
import {
SIDEBAR_STICKY_HEADER_HEIGHT,
ThreadNavigationSidebarHeader,
} from "./thread-navigation-sidebar-header";
import { useThreadListV2Projection } from "./use-thread-list-v2-projection";
import {
ThreadListV2PendingRow,
ThreadListV2Row,
threadListV2TimeLabel,
ThreadListV2SettledShelfHeader,
ThreadListV2SnoozedShelfHeader,
} from "./thread-list-v2-items";
import { THREAD_LIST_V2_SETTLED_PAGE_COUNT, type ThreadListV2ListItem } from "./threadListV2";
/** Flat Thread List v2 rows with queued tasks spliced in, plus the settled
"Show more" pager. */
type SidebarListItem =
| ThreadListV2ListItem
| { readonly type: "v2-show-more"; readonly key: string; readonly hiddenCount: number };
interface ThreadNavigationSidebarProps {
readonly width: number;
readonly visible: boolean;
readonly selectedThreadKey: string | null;
readonly onOpenSettings: () => void;
readonly onOpenEnvironmentSettings: () => void;
readonly onSearchQueryChange: (query: string) => void;
readonly onSelectThread: (thread: EnvironmentThreadShell) => void;
readonly onRequestVisibility: () => void;
readonly searchQuery: string;
}
/**
* iPad/large-width sidebar column.
*
* On iOS the pane is hosted inside its own navigation-inert single-screen
* native stack (SidebarNavigationShell) so the header is a real
* UINavigationBar: large title, native bar-button items, and a
* UISearchController search field — the same chrome a UISplitViewController
* column gets. Other platforms keep the custom header chrome.
*/
export function ThreadNavigationSidebar(props: ThreadNavigationSidebarProps) {
if (Platform.OS !== "ios") {
return <ThreadNavigationSidebarPane {...props} nativeChrome={false} />;
}
return <NativeSidebarContainer {...props} />;
}
function NativeSidebarContainer(props: ThreadNavigationSidebarProps) {
const backgroundColor = useThemeColor("--color-drawer");
const borderColor = useThemeColor("--color-border");
return (
<View
testID="thread-navigation-sidebar"
className="flex-1"
style={{
width: props.width,
backgroundColor,
borderRightColor: borderColor,
borderRightWidth: StyleSheet.hairlineWidth,
}}
>
<SidebarNavigationShell>
<ThreadNavigationSidebarPane {...props} nativeChrome />
</SidebarNavigationShell>
</View>
);
}
function ThreadNavigationSidebarPane(
props: ThreadNavigationSidebarProps & { readonly nativeChrome: boolean },
) {
const { t } = useMobileI18n();
const insets = useSafeAreaInsets();
const projects = useProjects();
const threads = useThreadShells();
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
const { savedConnectionsById } = useSavedRemoteConnections();
const searchInputRef = useRef<TextInput>(null);
const searchBarRef = useRef<SearchBarCommands>(null);
const openSwipeableRef = useRef<SwipeableMethods | null>(null);
const sidebarScrollGesture = useMemo(() => Gesture.Native(), []);
const {
archiveThread,
confirmDeleteThread,
settleThread,
snoozeThread,
unsnoozeThread,
unsettleThread,
pinThread,
unpinThread,
movePinnedThread,
regenerateThreadTitle,
} = useThreadListActions();
const pendingTasks = usePendingNewTasks();
const { openPendingTask, confirmDeletePendingTask } = usePendingTaskListActions();
const environments = useMemo(
() =>
Object.values(savedConnectionsById)
.map((connection) => ({
environmentId: connection.environmentId,
label: connection.environmentLabel,
}))
.sort((left, right) => left.label.localeCompare(right.label)),
[savedConnectionsById],
);
const availableEnvironmentIds = useMemo(
() => new Set(environments.map((environment) => environment.environmentId)),
[environments],
);
const { options, setSelectedEnvironmentId } = useHomeListOptions(availableEnvironmentIds);
const [selectedProjectKey, setSelectedProjectKey] = useState<string | null>(null);
const {
threadSearch,
threadSearchMatchByKey,
projectScopes,
selectedProjectScope,
projectByKey,
projectCwdByKey,
projectTitleByProjectKey,
serverConfigs,
capabilities,
arrangedPinnedKeys,
nowMinute,
shelves,
listItems: threadListItems,
hiddenSettledCount,
showMoreSettled,
} = useThreadListV2Projection({
threads,
projects,
pendingTasks,
searchEnvironments: workspaceEnvironments,
selectedEnvironmentId: options.selectedEnvironmentId,
selectedProjectKey,
projectGroupingMode: options.projectGroupingMode,
searchQuery: props.searchQuery,
selectedThreadKey: props.selectedThreadKey ?? null,
});
const {
settlement: settlementEnvironmentIds,
snooze: snoozeEnvironmentIds,
pinning: pinningEnvironmentIds,
pinReorder: pinReorderEnvironmentIds,
titleRegeneration: titleRegenerationEnvironmentIds,
} = capabilities;
const {
loaded: shelfPreferencesLoaded,
settledShelfExpanded,
toggleSettledShelf,
toggleSnoozedShelf,
} = shelves;
const projectFilterOptions = useMemo(
() =>
projectScopes.map((scope) => ({
key: scope.key,
label: scope.title,
})),
[projectScopes],
);
useEffect(() => {
if (
selectedProjectKey !== null &&
!projectFilterOptions.some((project) => project.key === selectedProjectKey)
) {
setSelectedProjectKey(null);
}
}, [projectFilterOptions, selectedProjectKey]);
const listItems = useMemo<readonly SidebarListItem[]>(
() =>
settledShelfExpanded && hiddenSettledCount > 0
? [
...threadListItems,
{ type: "v2-show-more", key: "v2-show-more", hiddenCount: hiddenSettledCount },
]
: threadListItems,
[hiddenSettledCount, settledShelfExpanded, threadListItems],
);
const backgroundColor = useThemeColor("--color-drawer");
const borderColor = useThemeColor("--color-border");
const [measuredHeaderHeight, setMeasuredHeaderHeight] = useState<number | null>(null);
// The sticky header (title row, search field, optional connection status)
// is measured so the list inset always matches its real height — no
// hardcoded per-variant constants.
const stickyHeaderHeight = measuredHeaderHeight ?? insets.top + SIDEBAR_STICKY_HEADER_HEIGHT;
const topListInset = stickyHeaderHeight + 6;
const handleStickyHeaderLayout = useCallback((event: LayoutChangeEvent) => {
const nextHeight = Math.round(event.nativeEvent.layout.height);
setMeasuredHeaderHeight((current) => (current === nextHeight ? current : nextHeight));
}, []);
const handleSwipeableWillOpen = useCallback((methods: SwipeableMethods) => {
if (openSwipeableRef.current !== methods) {
openSwipeableRef.current?.close();
openSwipeableRef.current = methods;
}
}, []);
const handleSwipeableClose = useCallback((methods: SwipeableMethods) => {
if (openSwipeableRef.current === methods) {
openSwipeableRef.current = null;
}
}, []);
const handleSelectThread = useCallback(
(thread: EnvironmentThreadShell) => {
props.onSelectThread(thread);
openSwipeableRef.current?.close();
},
[props.onSelectThread],
);
const handleScrollBeginDrag = useCallback(() => {
openSwipeableRef.current?.close();
}, []);
const { swipeEnabled, scrollGateHandlers } = useSwipeableScrollGate({
onScrollBeginDrag: handleScrollBeginDrag,
});
// Project shells load after the first rows draw, so the maps they feed have
// to bust the recycler's memoization — otherwise a row keeps the blank
// favicon and fallback title it was first rendered with.
const listExtraData = useMemo(
() => ({
selectedThreadKey: props.selectedThreadKey ?? "",
projectByKey,
projectCwdByKey,
projectTitleByProjectKey,
savedConnectionsById,
serverConfigs,
snoozePresetMinute: nowMinute,
threadSearchMatchByKey,
}),
[
props.selectedThreadKey,
projectByKey,
projectCwdByKey,
projectTitleByProjectKey,
savedConnectionsById,
serverConfigs,
nowMinute,
threadSearchMatchByKey,
],
);
const sidebarItemsAreEqual = useCallback(
(previous: SidebarListItem, item: SidebarListItem): boolean => {
if (previous.type === "v2-thread" && item.type === "v2-thread") {
return (
previous.key === item.key &&
previous.item.thread === item.item.thread &&
previous.item.variant === item.item.variant &&
previous.item.snoozed === item.item.snoozed &&
previous.item.pinned === item.item.pinned &&
previous.snoozeWakeLabelText === item.snoozeWakeLabelText
);
}
if (previous.type === "v2-show-more" && item.type === "v2-show-more") {
return previous.hiddenCount === item.hiddenCount;
}
if (previous.type === "v2-pending" && item.type === "v2-pending") {
return (
previous.pendingTask === item.pendingTask &&
previous.showPendingDivider === item.showPendingDivider
);
}
if (previous.type === "v2-snoozed-shelf" && item.type === "v2-snoozed-shelf") {
return previous.count === item.count && previous.expanded === item.expanded;
}
if (previous.type === "v2-settled-shelf" && item.type === "v2-settled-shelf") {
return previous.count === item.count && previous.expanded === item.expanded;
}
return false;
},
[],
);
const focusSearch = useCallback(() => {
const focus = () => {
if (props.nativeChrome) {
searchBarRef.current?.focus();
return;
}
searchInputRef.current?.focus();
};
if (!props.visible) {
props.onRequestVisibility();
setTimeout(focus, 240);
} else {
focus();
}
return true;
}, [props.nativeChrome, props.onRequestVisibility, props.visible]);
useHardwareKeyboardCommand("focusSearch", focusSearch);
const renderListItem = useCallback(
({ item }: { readonly item: SidebarListItem }) => {
switch (item.type) {
case "v2-pending": {
const pendingScopeKey = scopedProjectKey(
item.pendingTask.message.environmentId,
item.pendingTask.creation.projectId,
);
return (
<ThreadListV2PendingRow
pendingTask={item.pendingTask}
project={projectByKey.get(pendingScopeKey) ?? null}
projectTitle={projectTitleByProjectKey.get(pendingScopeKey)}
environmentLabel={
Object.keys(savedConnectionsById).length > 1
? (savedConnectionsById[item.pendingTask.message.environmentId]
?.environmentLabel ?? null)
: null
}
pane="sidebar"
showPendingDivider={item.showPendingDivider}
onSelectPendingTask={openPendingTask}
onDeletePendingTask={confirmDeletePendingTask}
/>
);
}
case "v2-thread": {
const thread = item.item.thread;
const scopeKey = scopedProjectKey(thread.environmentId, thread.projectId);
return (
<ThreadListV2Row
thread={thread}
variant={item.item.variant}
snoozed={item.item.snoozed}
pinned={item.item.pinned}
timeLabel={threadListV2TimeLabel(thread)}
snoozePresetMinute={
!item.item.snoozed && snoozeEnvironmentIds.has(thread.environmentId)
? nowMinute
: null
}
snoozeWakeLabelText={item.snoozeWakeLabelText}
project={projectByKey.get(scopeKey) ?? null}
projectTitle={projectTitleByProjectKey.get(scopeKey)}
providerDriver={
serverConfigs
.get(thread.environmentId)
?.providers.find(
(provider) =>
provider.instanceId ===
(thread.session?.providerInstanceId ?? thread.modelSelection.instanceId),
)?.driver ?? null
}
environmentLabel={
Object.keys(savedConnectionsById).length > 1
? (savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
: null
}
searchMatch={threadSearchMatchByKey.get(
threadSearchMatchKey({
environmentId: thread.environmentId,
threadId: thread.id,
}),
)}
searchQuery={props.searchQuery}
pane="sidebar"
selected={
scopedThreadKey(thread.environmentId, thread.id) === props.selectedThreadKey
}
fullSwipeWidth={props.width - 20}
onSelectThread={handleSelectThread}
onDeleteThread={confirmDeleteThread}
onArchiveThread={archiveThread}
onRegenerateThreadTitle={regenerateThreadTitle}
titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)}
settlementSupported={settlementEnvironmentIds.has(thread.environmentId)}
onSettleThread={settleThread}
snoozeSupported={snoozeEnvironmentIds.has(thread.environmentId)}
pinningSupported={pinningEnvironmentIds.has(thread.environmentId)}
pinReorderSupported={pinReorderEnvironmentIds.has(thread.environmentId)}
canMovePinnedUp={
arrangedPinnedKeys.indexOf(`${thread.environmentId}:${thread.id}`) > 0
}
canMovePinnedDown={(() => {
const index = arrangedPinnedKeys.indexOf(`${thread.environmentId}:${thread.id}`);
return index !== -1 && index < arrangedPinnedKeys.length - 1;
})()}
onSnoozeThread={snoozeThread}
onUnsnoozeThread={unsnoozeThread}
onUnsettleThread={unsettleThread}
onPinThread={pinThread}
onUnpinThread={unpinThread}
onMovePinnedThread={movePinnedThread}
projectCwd={projectCwdByKey.get(scopeKey) ?? null}
onSwipeableClose={handleSwipeableClose}
onSwipeableWillOpen={handleSwipeableWillOpen}
simultaneousSwipeGesture={sidebarScrollGesture}
/>
);
}
case "v2-snoozed-shelf":
return (
<ThreadListV2SnoozedShelfHeader
count={item.count}
disabled={!shelfPreferencesLoaded}
expanded={item.expanded}
onToggle={toggleSnoozedShelf}
pane="sidebar"
/>
);
case "v2-settled-shelf":
return (
<ThreadListV2SettledShelfHeader
count={item.count}
disabled={!shelfPreferencesLoaded}
expanded={item.expanded}
onToggle={toggleSettledShelf}
pane="sidebar"
/>
);
case "v2-show-more":
return (
<Pressable
accessibilityRole="button"
accessibilityLabel={`Show ${Math.min(item.hiddenCount, THREAD_LIST_V2_SETTLED_PAGE_COUNT)} more settled chats`}
onPress={showMoreSettled}
className="mx-4 mt-2 items-center rounded-lg border border-dashed border-border py-2.5"
style={({ pressed }) => ({ opacity: pressed ? 0.6 : 1 })}
>
<Text className="text-xs font-t3-medium text-foreground-muted">
Show more ({item.hiddenCount} settled hidden)
</Text>
</Pressable>
);
}
},
[
archiveThread,
arrangedPinnedKeys,
confirmDeletePendingTask,
confirmDeleteThread,
handleSelectThread,
handleSwipeableClose,
handleSwipeableWillOpen,
movePinnedThread,
openPendingTask,
pinReorderEnvironmentIds,
pinThread,
pinningEnvironmentIds,
projectByKey,
projectCwdByKey,
projectTitleByProjectKey,
regenerateThreadTitle,
props.searchQuery,
props.selectedThreadKey,
props.width,
savedConnectionsById,
serverConfigs,
shelfPreferencesLoaded,
threadSearchMatchByKey,
titleRegenerationEnvironmentIds,
settleThread,
settlementEnvironmentIds,
showMoreSettled,
sidebarScrollGesture,
snoozeEnvironmentIds,
snoozeThread,
nowMinute,
toggleSettledShelf,
toggleSnoozedShelf,
unpinThread,
unsettleThread,
unsnoozeThread,
],
);
const filterCustomized = options.selectedEnvironmentId !== null || selectedProjectKey !== null;
const filterIcon = filterCustomized
? "line.3.horizontal.decrease.circle.fill"
: "line.3.horizontal.decrease.circle";
const filterMenu = useMemo(
() =>
buildHomeListFilterMenu({
environments,
projects: projectFilterOptions,
selectedEnvironmentId: options.selectedEnvironmentId,
selectedProjectKey,
onEnvironmentChange: setSelectedEnvironmentId,
onProjectChange: setSelectedProjectKey,
}),
[
environments,
options.selectedEnvironmentId,
projectFilterOptions,
selectedProjectKey,
setSelectedEnvironmentId,
],
);
const nativeHeaderItems = useMemo(
() =>
createSidebarHeaderItems({
filterIcon,
filterMenu,
onOpenSettings: props.onOpenSettings,
}),
[filterIcon, filterMenu, props.onOpenSettings],
);
// Snoozed threads need no special case: the shelf header is a list row
// even while collapsed.
const listEmpty = (
<Text className="px-2 py-4 text-sm text-foreground-muted">
{catalogState.isLoadingConnections
? t("Loading chats…")
: props.searchQuery.trim().length > 0
? threadSearch.isPending
? t("Searching chats…")
: t("No matching chats")
: selectedProjectScope !== null
? `No chats in ${selectedProjectScope.title}`
: t("No chats yet")}
</Text>
);
if (props.nativeChrome) {
return (
<>
<NativeStackScreenOptions
optionsVersion={nativeHeaderItems}
options={{
// Re-applies the shell's static brand slot with the
// connection-status swap so reconnects surface in the header
// instead of shifting the list.
...getConnectionAwareBrandHeaderOptions({
onOpenEnvironments: props.onOpenEnvironmentSettings,
fallbackTitleStyle: { fontSize: 18, fontWeight: "800" },
}),
headerSearchBarOptions: {
ref: searchBarRef,
autoCapitalize: "none",
hideNavigationBar: false,
// Keep the search bar pinned under the title — UIKit's default
// hidesSearchBarWhenScrolling collapses it on scroll.
hideWhenScrolling: false,
obscureBackground: false,
placeholder: "Search",
placement: "stacked",
onCancelButtonPress: () => {
props.onSearchQueryChange("");
},
onChangeText: (event) => {
props.onSearchQueryChange(event.nativeEvent.text);
},
},
unstable_headerRightItems: () => nativeHeaderItems,
}}
/>
<View className="flex-1">
<SwipeableScrollGateProvider enabled={swipeEnabled}>
<GestureDetector gesture={sidebarScrollGesture}>
<LegendList
data={listItems}
drawDistance={500}
estimatedItemSize={64}
extraData={listExtraData}
getItemType={(item) => item.type}
itemsAreEqual={sidebarItemsAreEqual}
keyExtractor={(item) => item.key}
renderItem={renderListItem}
automaticallyAdjustsScrollIndicatorInsets={NATIVE_LIQUID_GLASS_SUPPORTED}
contentInsetAdjustmentBehavior={
NATIVE_LIQUID_GLASS_SUPPORTED ? "automatic" : "never"
}
contentContainerStyle={[
styles.threadListContent,
{
paddingBottom: Math.max(insets.bottom, 16) + 16,
paddingTop: 6,
},
]}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
{...scrollGateHandlers}
recycleItems
scrollEventThrottle={16}
showsVerticalScrollIndicator={false}
style={styles.threadList}
ListEmptyComponent={listEmpty}
/>
</GestureDetector>
</SwipeableScrollGateProvider>
</View>
</>
);
}
return (
<View
testID="thread-navigation-sidebar"
className="flex-1"
style={{
width: props.width,
backgroundColor,
borderRightColor: borderColor,
borderRightWidth: StyleSheet.hairlineWidth,
}}
>
<View className="flex-1" style={{ paddingBottom: insets.bottom }}>
<SwipeableScrollGateProvider enabled={swipeEnabled}>
<GestureDetector gesture={sidebarScrollGesture}>
<LegendList
data={listItems}
drawDistance={500}
estimatedItemSize={64}
extraData={listExtraData}
getItemType={(item) => item.type}
itemsAreEqual={sidebarItemsAreEqual}
keyExtractor={(item) => item.key}
renderItem={renderListItem}
contentContainerStyle={[
styles.threadListContent,
{
paddingBottom:
Platform.OS === "android"
? Math.max(insets.bottom, 16) + 88 - insets.bottom
: 16 + insets.bottom,
paddingTop: topListInset,
},
]}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
{...scrollGateHandlers}
recycleItems
scrollEventThrottle={16}
showsVerticalScrollIndicator={false}
style={styles.threadList}
ListEmptyComponent={listEmpty}
/>
</GestureDetector>
</SwipeableScrollGateProvider>
</View>
<ThreadNavigationSidebarHeader
topInset={insets.top}
onLayout={handleStickyHeaderLayout}
filterIcon={filterIcon}
environments={environments}
projectFilterOptions={projectFilterOptions}
selectedEnvironmentId={options.selectedEnvironmentId}
selectedProjectKey={selectedProjectKey}
onEnvironmentChange={setSelectedEnvironmentId}
onProjectChange={setSelectedProjectKey}
onOpenSettings={props.onOpenSettings}
onOpenEnvironmentSettings={props.onOpenEnvironmentSettings}
searchInputRef={searchInputRef}
searchQuery={props.searchQuery}
onSearchQueryChange={props.onSearchQueryChange}
/>
</View>
);
}
const styles = StyleSheet.create({
threadList: {
flex: 1,
},
threadListContent: {
paddingHorizontal: 8,
},
});