import { ReactNode } from 'react'; import { atom, selector } from 'recoil'; import { Channel, GuildMetadata, Member, Message } from '../data-types'; import CombinedGuild from '../guild-combined'; export interface GuildWithValue { guild: CombinedGuild; value: T; } export interface GuildWithErrorableValue { guild: CombinedGuild; value: T | null; valueError: unknown | null; } export interface ChannelWithErrorableValue { channel: Channel; value: T | null; valueError: unknown | null; } export const overlayState = atom({ key: 'overlayState', default: null }); export const guildsState = atom[] | null>({ key: 'guildsState', default: null }); export const selectedGuildIdState = atom({ key: 'selectedGuildIdState', default: null }); export const selectedGuildWithMetaState = selector | null>({ key: 'selectedGuildWithMetaState', get: ({ get }) => { const guildsWithMeta = get(guildsState); if (guildsWithMeta === null) return null; const guildId = get(selectedGuildIdState); if (guildId === null) return null; return guildsWithMeta.find(guildWithMeta => guildWithMeta.guild.id === guildId) ?? null; } }); export const selectedGuildState = selector({ key: 'selectedGuildState', get: ({ get }) => { const guildWithMeta = get(selectedGuildWithMetaState); if (guildWithMeta === null) return null; return guildWithMeta.guild; } }); export const selectedGuildMembersState = atom | null>({ key: 'selectedGuildMembersState', default: null }); export const selectedGuildWithChannelsState = atom | null>({ key: 'selectedGuildChannelsState', default: null }); export const selectedGuildWithActiveChannelIdState = atom | null>({ key: 'selectedGuildWithActiveChannelIdState', default: null }); export const selectedGuildWithActiveChannelMessagesState = atom | null> | null>({ key: 'selectedGuildWithActiveChannelMessagesState', default: null }); export const selectedGuildWithActiveChannelState = selector | null>({ key: 'selectedGuildActiveChannelState', get: ({ get }) => { const guildWithChannelMessages = get(selectedGuildWithActiveChannelMessagesState); if (guildWithChannelMessages === null || guildWithChannelMessages.value === null) return null; return { guild: guildWithChannelMessages.guild, value: guildWithChannelMessages.value.channel }; } });