import { Accessor, Component, createEffect, createMemo, createSignal, For, JSX, Show, untrack } from "solid-js"; import { decode, Mutation } from "~/utilities"; import { Column, GridApi as GridCompApi, Grid as GridComp } from "~/components/grid"; import { createDataSet, DataSetNode, DataSetRowNode } from "~/features/dataset"; import { SelectionItem } from "../selectable"; import { useI18n } from "../i18n"; import { debounce } from "@solid-primitives/scheduled"; import css from "./grid.module.css" import { Textarea } from "~/components/textarea"; export type Entry = { key: string } & { [lang: string]: string }; export interface GridApi { readonly mutations: Accessor; readonly selection: Accessor[]>; remove(indices: number[]): void; addKey(key: string): void; addLocale(locale: string): void; selectAll(): void; clearSelection(): void; }; const groupBy = (rows: DataSetRowNode[]) => { type R = DataSetRowNode & { _key: string }; const group = (nodes: R[]): DataSetNode[] => Object .entries(Object.groupBy(nodes, r => r._key.split('.').at(0)!) as Record) .map(([key, nodes]) => nodes.at(0)?._key === key ? nodes[0] : ({ kind: 'group', key, groupedBy: 'key', nodes: group(nodes.map(n => ({ ...n, _key: n._key.slice(key.length + 1) }))) }) ); return group(rows.filter(r => r.value.key).map(r => ({ ...r, _key: r.value.key }))) as any; } export function Grid(props: { class?: string, rows: Entry[], locales: string[], api?: (api: GridApi) => any, children?: (key: string) => JSX.Element }) { const { t } = useI18n(); const [addedLocales, setAddedLocales] = createSignal([]); const rows = createDataSet(() => props.rows, { group: { by: 'key', with: groupBy } }); const locales = createMemo(() => [...props.locales, ...addedLocales()]); const columns = createMemo[]>(() => [ { id: 'key', label: t('feature.file.grid.key'), renderer: ({ value }) => props.children?.(value) ?? value.split('.').at(-1), }, ...locales().toSorted().map>(lang => ({ id: lang, label: lang, renderer: ({ row, column, value, mutate }) => { const lang = String(column); const { key } = rows.value[row]!; return