fix(webui): restore session drag and review findings

This commit is contained in:
chengyongru
2026-08-12 17:26:13 +08:00
committed by chengyongru
parent 4b5319b760
commit 686dd0603e
24 changed files with 494 additions and 169 deletions
+41 -14
View File
@@ -9,17 +9,20 @@ type ChannelMessagesModule = {
default?: ChannelMessages;
};
type ChannelMessagesLoader = () => Promise<ChannelMessagesModule>;
const modules = import.meta.glob<ChannelMessagesModule>(
"../../../nanobot/channels/*/webui/locales/*.json",
{ eager: true },
);
const loadersByChannel = new Map<
string,
Map<SupportedLocale, ChannelMessagesLoader>
>();
const translationsByChannel = new Map<string, Map<SupportedLocale, ChannelMessages>>();
const supportedLocaleCodes = new Set<string>(supportedLocales.map(({ code }) => code));
for (const [modulePath, module] of Object.entries(modules)) {
const messages = module.default;
if (!messages) continue;
for (const [modulePath, loader] of Object.entries(modules)) {
const match = modulePath.match(/nanobot\/channels\/([^/]+)\/webui\/locales\/([^/]+)\.json$/);
if (!match) {
throw new Error(`Cannot derive channel locale identity from '${modulePath}'`);
@@ -28,25 +31,27 @@ for (const [modulePath, module] of Object.entries(modules)) {
if (!supportedLocaleCodes.has(locale)) {
throw new Error(`Channel '${channel}' has unsupported locale '${locale}'`);
}
const translations = translationsByChannel.get(channel) ?? new Map();
if (translations.has(locale as SupportedLocale)) {
const loaders = loadersByChannel.get(channel) ?? new Map();
if (loaders.has(locale as SupportedLocale)) {
throw new Error(`Channel '${channel}' registers locale '${locale}' more than once`);
}
translations.set(locale as SupportedLocale, messages);
translationsByChannel.set(channel, translations);
loaders.set(locale as SupportedLocale, loader);
loadersByChannel.set(channel, loaders);
}
export function channelLocaleNamespaces(): string[] {
return [...translationsByChannel.keys()].map(channelNamespace);
return [...loadersByChannel.keys()].map(channelNamespace);
}
export function channelLocaleResources(locale: SupportedLocale): Record<string, unknown> {
return Object.fromEntries(
[...translationsByChannel.keys()].map((channel) => [
export async function channelLocaleResources(
locale: SupportedLocale,
): Promise<Record<string, ChannelMessages>> {
return Object.fromEntries(await Promise.all(
[...loadersByChannel.keys()].map(async (channel) => [
channelNamespace(channel),
channelLocaleMessages(channel, locale) ?? {},
await loadChannelLocale(channel, locale),
]),
);
));
}
export function channelLocaleMessages(
@@ -63,3 +68,25 @@ export function registeredChannelLocales(): ReadonlyMap<
> {
return translationsByChannel;
}
async function loadChannelLocale(
channel: string,
locale: SupportedLocale,
): Promise<ChannelMessages> {
const translations = translationsByChannel.get(channel) ?? new Map();
const loaded = translations.get(locale);
if (loaded) return loaded;
const loaders = loadersByChannel.get(channel);
const loader = loaders?.get(locale) ?? loaders?.get("en");
if (!loader) {
throw new Error(`Channel '${channel}' has no locale loader for '${locale}' or 'en'`);
}
const messages = (await loader()).default;
if (!messages) {
throw new Error(`Channel '${channel}' locale '${locale}' has no default export`);
}
translations.set(locale, messages);
translationsByChannel.set(channel, translations);
return messages;
}
+1 -1
View File
@@ -8,7 +8,7 @@ type ChannelUiContributionModule = {
};
const modules = import.meta.glob<ChannelUiContributionModule>(
"../../../nanobot/channels/*/webui/**/*.{ts,tsx}",
"../../../nanobot/channels/*/webui/index.{ts,tsx}",
{
eager: true,
},