Skip to content

Commit

Permalink
Order children (#380)
Browse files Browse the repository at this point in the history
* Setup children sorting

* Order children entries in the dashboard sidebar
  • Loading branch information
benmerckx authored Mar 20, 2024
1 parent 023cf8e commit acbe253
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion apps/dev/src/schema/Examples.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Config} from 'alinea'
import {Config, Query} from 'alinea'
import * as examples from './example'

export const Examples = Config.document('Examples', {
contains: Object.values(examples),
orderChildrenBy: Query.title.asc(),
fields: {}
})
7 changes: 4 additions & 3 deletions apps/web/src/page/playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ function PreviewField({field}: PreviewFieldProps) {

function editorConfig(monaco: Monaco) {
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
jsx: 'preserve'
jsx: 'preserve',
typeRoots: ['node_modules/@types']
})
monaco.languages.typescript.typescriptDefaults.addExtraLib(
`declare var alinea: typeof import('alinea').alinea;` + declarations,
'@types/alinea/index.d.ts'
`declare var alinea: typeof import('alinea').alinea;\n` + declarations,
'file:///node_modules/@types/alinea/index.d.ts'
)
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/Type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {Expr, createExprData} from './pages/Expr.js'
import {
BinaryOp,
ExprData,
OrderBy,
Selection,
toSelection
} from './pages/ResolveData.js'
Expand All @@ -38,6 +39,8 @@ export interface EntryUrlMeta {
export interface TypeMeta {
/** Accepts entries of these types as children */
contains?: Array<string | Type>
/** Order children entries in the sidebar content tree */
orderChildrenBy?: OrderBy
/** @deprecated Use contains instead */
isContainer?: true
/** @deprecated Use hidden instead */
Expand Down
37 changes: 32 additions & 5 deletions src/dashboard/atoms/EntryAtoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {useMemo} from 'react'
import {useDashboard} from '../hook/UseDashboard.js'
import {configAtom} from './DashboardAtoms.js'
import {graphAtom, useMutate} from './DbAtoms.js'
import {rootAtom, workspaceAtom} from './NavigationAtoms.js'
import {localeAtom, rootAtom, workspaceAtom} from './NavigationAtoms.js'

export function rootId(rootName: string) {
return `@alinea/root-${rootName}`
Expand Down Expand Up @@ -52,6 +52,7 @@ async function entryTreeRoot(
return {
id: rootId(root),
index: '',
type: '',
isFolder: true,
entries: [],
children
Expand All @@ -60,6 +61,7 @@ async function entryTreeRoot(

const entryTreeItemLoaderAtom = atom(async get => {
const graph = await get(graphAtom)
const locale = get(localeAtom)
const visibleTypes = get(visibleTypesAtom)
const {schema} = get(configAtom)
const root = get(rootAtom)
Expand All @@ -79,19 +81,21 @@ const entryTreeItemLoaderAtom = atom(async get => {
path: Entry.path,
parentPaths({parents}) {
return parents(Entry).select(Entry.path)
},
children({children}) {
}
/*children({children}) {
return children(Entry)
.where(Entry.type.isIn(visibleTypes))
.select(Entry.i18nId)
.groupBy(Entry.i18nId)
.orderBy(Entry.index.asc())
}
}*/
} satisfies Projection
const entries = Entry()
.select({
id: Entry.i18nId,
entryId: Entry.entryId,
index: Entry.index,
type: Entry.type,
data,
translations({translations}) {
return translations().select(data)
Expand All @@ -101,12 +105,34 @@ const entryTreeItemLoaderAtom = atom(async get => {
.where(Entry.i18nId.isIn(search))
const rows = await graph.preferDraft.find(entries)
for (const row of rows) {
const type = schema[row.type]
const orderBy = Type.meta(type).orderChildrenBy ?? Entry.index.asc()
const ids = row.translations.map(row => row.entryId).concat(row.entryId)
const children = await graph.preferDraft.find(
Entry()
.where(Entry.parent.isIn(ids), Entry.type.isIn(visibleTypes))
.select({locale: Entry.locale, i18nId: Entry.i18nId})
.orderBy(orderBy)
)
const entries = [row.data].concat(row.translations)
const translatedChildren = new Set(
children
.filter(child => child.locale === locale)
.map(child => child.i18nId)
)
const untranslated = new Set()
const orderedChildren = children.filter(child => {
if (translatedChildren.has(child.i18nId)) return child.locale === locale
if (untranslated.has(child.i18nId)) return false
untranslated.add(child.i18nId)
return true
})
indexed.set(row.id, {
id: row.id,
type: row.type,
index: row.index,
entries,
children: [...new Set(entries.flatMap(entry => entry.children))]
children: [...new Set(orderedChildren.map(child => child.i18nId))]
})
}
const res: Array<EntryTreeItem | undefined> = []
Expand Down Expand Up @@ -144,6 +170,7 @@ const loaderAtom = atom(get => {
export interface EntryTreeItem {
id: string
index: string
type: string
entries: Array<{
id: string
entryId: string
Expand Down
7 changes: 7 additions & 0 deletions src/dashboard/view/EntryTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface EntryTreeProps {

export function EntryTree({i18nId: entryId, selected = []}: EntryTreeProps) {
const root = useRoot()
const {schema} = useConfig()
const treeProvider = useEntryTreeProvider()
const navigate = useNavigate()
const nav = useNav()
Expand Down Expand Up @@ -170,6 +171,12 @@ export function EntryTree({i18nId: entryId, selected = []}: EntryTreeProps) {
}, [selected.join()])
useEffect(() => {
tree.invalidateChildrenIds(rootId(root.name))
for (const item of tree.getItems()) {
const typeName: string = item.getItemData()?.type
const type = schema[typeName]
const {orderChildrenBy} = Type.meta(type)
if (orderChildrenBy) tree.invalidateChildrenIds(item.getId())
}
}, [treeProvider])
useEffect(() => {
for (const i18nId of changed) {
Expand Down

0 comments on commit acbe253

Please sign in to comment.