Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

9.6 KB
Raw Blame
import {
  type ChannelPreviewSelection,
  channelIdForPreviewNavigation,
  getChannelEntityTarget,
} from '@app/features/next-soup/utils';
import { useFeatureFlag } from '@app/lib/analytics/posthog';
import { makePersistedState } from '@app/lib/persistence';
import {
  createSearchParams,
  useNavigate,
  useRouteParams,
} from '@app/lib/split-router';
import { channelDetailRoute, channelsSplitRoute } from '@app/routes/routes';
import { createPreviewSelectionGuard } from '@components/app/createPreviewSelectionGuard';
import { useSplitPanelOrThrow } from '@components/app/split-layout/layoutUtils';
import {
  enableChannelThreadsPreview,
  enableChatSidebarSections,
} from '@core/constant/featureFlags';
import { createAssertedContextProvider } from '@core/context/createContext';
import { useUserId } from '@core/context/user';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { fireAndForget } from '@core/util/fireAndForget';
import type { ContextProviderProps } from '@solid-primitives/context';
import { type Accessor, createEffect, createMemo, on } from 'solid-js';
import { createStore, type Store } from 'solid-js/store';
import { channelsSearch, channelsSearchCodec } from './channels-route';
import { CHANNELS_DEFAULT_SORT_BY } from './constants';
import { createChannelsViewPersistence } from './persistence';
import type {
  ChannelListSort,
  ChannelSectionKey,
  ChannelSectionPreferences,
  ChannelsRailSection,
  ChannelsSortGroup,
  ChannelsTab,
  ChannelsViewState,
  ChannelsViewStateOptions,
} from './types';

type ChannelsViewProviderProps = ContextProviderProps & {
  initialState?: ChannelsViewStateOptions;
};

export type ChannelsViewContext = {
  state: Store<ChannelsViewState>;
  /** The mobile list opens channels in the split; only desktop renders detail. */
  mobileLayout: () => boolean;
  /** Whether the redesigned desktop sections are enabled. */
  sectionsEnabled: Accessor<boolean>;
  sectionsLoading: Accessor<boolean>;
  selectedChannel: Accessor<ChannelPreviewSelection | undefined>;
  /** Whether the Threads tab is available to this user. */
  threadsEnabled: Accessor<boolean>;
  /**
   * The tab to render. A stored or linked Threads tab shows All while the flag
   * is off or still loading, and returns once it resolves on; `state.tab`
   * keeps the choice so persistence and URLs are not rewritten.
   */
  tab: Accessor<ChannelsTab>;
  /** Threads tab filter: the conversation whose threads are shown, or all. */
  threadsChannelId: Accessor<string | undefined>;
  setThreadsChannelId: (channelId: string | undefined) => void;
  setTab: (tab: ChannelsTab) => void;
  setSelectedChannel: (channel: ChannelPreviewSelection | undefined) => boolean;
  setGroupOpen: (group: ChannelsRailSection, open: boolean) => void;
  /** Per-user collapse state of a team channel label. */
  setLabelOpen: (labelId: string, open: boolean) => void;
  setSortBy: (group: ChannelsSortGroup, sort: ChannelListSort) => void;
  setSectionPreferences: (
    section: ChannelSectionKey,
    preferences: ChannelSectionPreferences
  ) => void;
  setSectionOrder: (sections: ChannelSectionKey[]) => void;
};

function createInitialState(
  initial: ChannelsViewStateOptions
): ChannelsViewState {
  return {
    tab: initial.tab ?? 'browse',
    expandedGroups: {
      favorites: initial.expandedGroups?.favorites ?? true,
      team_channels: initial.expandedGroups?.team_channels ?? true,
      channels: initial.expandedGroups?.channels ?? true,
      direct_messages: initial.expandedGroups?.direct_messages ?? true,
    },
    collapsedLabels: initial.collapsedLabels ?? [],
    threadsChannelId: initial.threadsChannelId,
    sortBy: {
      team_channels:
        initial.sortBy?.team_channels ?? CHANNELS_DEFAULT_SORT_BY.team_channels,
      channels: initial.sortBy?.channels ?? CHANNELS_DEFAULT_SORT_BY.channels,
      direct_messages:
        initial.sortBy?.direct_messages ??
        CHANNELS_DEFAULT_SORT_BY.direct_messages,
      threads: initial.sortBy?.threads ?? CHANNELS_DEFAULT_SORT_BY.threads,
    },
    sectionPreferences: { ...initial.sectionPreferences },
    sectionOrder: initial.sectionOrder ?? [],
  };
}

function shouldRestorePreferences(initial: ChannelsViewStateOptions): boolean {
  return initial.sortBy === undefined;
}

export const [ChannelsViewProvider, useChannelsView] =
  createAssertedContextProvider<ChannelsViewContext, ChannelsViewProviderProps>(
    'ChannelsView',
    (props) => {
      const panel = useSplitPanelOrThrow();
      const userId = useUserId();
      const navigate = useNavigate();
      const params = useRouteParams(channelDetailRoute);
      const [search, setSearch] = createSearchParams(channelsSearch);
      const sectionsFlag = useFeatureFlag(enableChatSidebarSections);
      const sectionsEnabled = () => sectionsFlag().enabled;
      const threadsFlag = useFeatureFlag(enableChannelThreadsPreview);
      const threadsEnabled = () => threadsFlag().enabled;
      const selectPreview = createPreviewSelectionGuard();
      const initial = props.initialState ?? {};
      const [state, setState] = makePersistedState(
        createStore(createInitialState(initial)),
        createChannelsViewPersistence({
          handle: panel.handle,
          userId,
          restoreEntryState: props.initialState === undefined,
          restoreLocalState: props.initialState === undefined,
          restorePreferences: shouldRestorePreferences(initial),
          restoreSectionOrder: initial.sectionOrder === undefined,
        })
      );

      createEffect(
        on(
          () => search.tab,
          (tab) => {
            if (state.tab !== tab) setState('tab', tab);
          }
        )
      );

      const mobileLayout = () => isTouchDevice();
      const selectedChannel = createMemo<ChannelPreviewSelection | undefined>(
        () => {
          const channelId = params.channelId;
          if (typeof channelId !== 'string') return undefined;
          const target = search.messageId
            ? {
                messageId: search.messageId,
                ...(search.threadId ? { threadId: search.threadId } : {}),
              }
            : undefined;
          return {
            type: 'channel',
            id: channelId,
            ...(target ? { target } : {}),
          };
        }
      );
      const routeSearch = (channel?: ChannelPreviewSelection) => {
        const target = channel && getChannelEntityTarget(channel);
        return channelsSearchCodec.serialize({
          ...channelsSearch.defaults,
          tab: state.tab,
          messageId: target?.kind === 'message' ? target.messageId : '',
          threadId: target?.kind === 'message' ? (target.threadId ?? '') : '',
        });
      };
      const navigateToChannel = (
        channel: ChannelPreviewSelection,
        replace = false
      ) => {
        fireAndForget(
          navigate(
            {
              route: channelDetailRoute,
              params: { channelId: channelIdForPreviewNavigation(channel) },
            },
            {
              replace,
              search: { [channelsSearch.namespace]: routeSearch(channel) },
            }
          ),
          'channels-view'
        );
      };
      const setSelectedChannel = (
        channel: ChannelPreviewSelection | undefined
      ) => {
        if (!channel) {
          fireAndForget(
            navigate(
              { route: channelsSplitRoute, params: {} },
              { search: { [channelsSearch.namespace]: routeSearch() } }
            ),
            'channels-view'
          );
          return true;
        }
        if (mobileLayout() || !selectPreview.canSelect(channel)) return false;
        navigateToChannel(channel);
        return true;
      };

      createEffect(
        on(selectedChannel, (channel, previous) => {
          if (!selectPreview(channel)) {
            if (previous) navigateToChannel(previous, true);
            else
              fireAndForget(
                navigate(
                  { route: channelsSplitRoute, params: {} },
                  {
                    replace: true,
                    search: { [channelsSearch.namespace]: routeSearch() },
                  }
                ),
                'channels-view'
              );
          }
        })
      );

      return {
        state,
        mobileLayout,
        selectedChannel,
        sectionsEnabled,
        sectionsLoading: () => sectionsFlag().loading,
        threadsEnabled,
        tab: () => {
          if (sectionsEnabled()) return 'browse';
          return state.tab === 'threads' && !threadsEnabled()
            ? 'browse'
            : state.tab;
        },
        threadsChannelId: () => state.threadsChannelId,
        setThreadsChannelId: (channelId) => {
          if (state.threadsChannelId === channelId) return;
          setState('threadsChannelId', channelId);
        },
        setTab: (tab) => {
          if (state.tab === tab) return;
          setState('tab', tab);
          setSearch({ tab });
        },
        setSelectedChannel,
        setGroupOpen: (group, open) => setState('expandedGroups', group, open),
        setLabelOpen: (labelId, open) =>
          setState('collapsedLabels', (collapsed) =>
            open
              ? collapsed.filter((id) => id !== labelId)
              : collapsed.includes(labelId)
                ? collapsed
                : [...collapsed, labelId]
          ),
        setSortBy: (group, sort) => setState('sortBy', group, sort),
        setSectionPreferences: (section, preferences) =>
          setState('sectionPreferences', (current) => ({
            ...current,
            [section]: { ...current[section], ...preferences },
          })),
        setSectionOrder: (sections) => setState('sectionOrder', sections),
      };
    }
  );