mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-04 00:28:33 +00:00
103 lines
2.7 KiB
TypeScript
103 lines
2.7 KiB
TypeScript
import { atom } from 'jotai';
|
|
import { atomFamily, atomWithStorage } from 'jotai/utils';
|
|
|
|
import type { CreateWorkspaceMode } from '../components/affine/create-workspace-modal';
|
|
import type { SettingProps } from '../components/affine/setting-modal';
|
|
|
|
// modal atoms
|
|
export const openWorkspacesModalAtom = atom(false);
|
|
export const openCreateWorkspaceModalAtom = atom<CreateWorkspaceMode>(false);
|
|
export const openQuickSearchModalAtom = atom(false);
|
|
export const openOnboardingModalAtom = atom(false);
|
|
|
|
export type SettingAtom = Pick<SettingProps, 'activeTab' | 'workspaceId'> & {
|
|
open: boolean;
|
|
};
|
|
|
|
export const openSettingModalAtom = atom<SettingAtom>({
|
|
activeTab: 'appearance',
|
|
workspaceId: null,
|
|
open: false,
|
|
});
|
|
|
|
export const openDisableCloudAlertModalAtom = atom(false);
|
|
|
|
type PageMode = 'page' | 'edgeless';
|
|
type PageLocalSetting = {
|
|
mode: PageMode;
|
|
};
|
|
|
|
type PartialPageLocalSettingWithPageId = Partial<PageLocalSetting> & {
|
|
id: string;
|
|
};
|
|
|
|
const pageSettingsBaseAtom = atomWithStorage(
|
|
'pageSettings',
|
|
{} as Record<string, PageLocalSetting>
|
|
);
|
|
|
|
// readonly atom by design
|
|
export const pageSettingsAtom = atom(get => get(pageSettingsBaseAtom));
|
|
|
|
const recentPageSettingsBaseAtom = atomWithStorage<string[]>(
|
|
'recentPageSettings',
|
|
[]
|
|
);
|
|
|
|
export const recentPageSettingsAtom = atom<PartialPageLocalSettingWithPageId[]>(
|
|
get => {
|
|
const recentPageIDs = get(recentPageSettingsBaseAtom);
|
|
const pageSettings = get(pageSettingsAtom);
|
|
return recentPageIDs.map(id => ({
|
|
...pageSettings[id],
|
|
id,
|
|
}));
|
|
}
|
|
);
|
|
|
|
const defaultPageSetting = {
|
|
mode: 'page',
|
|
} satisfies PageLocalSetting;
|
|
|
|
export const pageSettingFamily = atomFamily((pageId: string) =>
|
|
atom(
|
|
get =>
|
|
get(pageSettingsBaseAtom)[pageId] ?? {
|
|
...defaultPageSetting,
|
|
},
|
|
(
|
|
get,
|
|
set,
|
|
patch:
|
|
| Partial<PageLocalSetting>
|
|
| ((prevSetting: PageLocalSetting | undefined) => void)
|
|
) => {
|
|
set(recentPageSettingsBaseAtom, ids => {
|
|
// pick 3 recent page ids
|
|
return [...new Set([pageId, ...ids]).values()].slice(0, 3);
|
|
});
|
|
const prevSetting = {
|
|
...defaultPageSetting,
|
|
...get(pageSettingsBaseAtom)[pageId],
|
|
};
|
|
set(pageSettingsBaseAtom, settings => ({
|
|
...settings,
|
|
[pageId]: {
|
|
...prevSetting,
|
|
...(typeof patch === 'function' ? patch(prevSetting) : patch),
|
|
},
|
|
}));
|
|
}
|
|
)
|
|
);
|
|
|
|
export const setPageModeAtom = atom(
|
|
void 0,
|
|
(get, set, pageId: string, mode: PageMode) => {
|
|
set(pageSettingFamily(pageId), { mode });
|
|
}
|
|
);
|
|
|
|
export type PageModeOption = 'all' | 'page' | 'edgeless';
|
|
export const allPageModeSelectAtom = atom<PageModeOption>('all');
|