Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Delusoire committed Aug 17, 2024
1 parent 39408d5 commit e6a226c
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 71 deletions.
78 changes: 78 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions modules/stdlib/src/registers/topbarLeftButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ declare global {
let topbarLeftButtonFactoryCtx: React.Context<TopbarLeftButtonFactory>;
globalThis.__renderTopbarLeftButtons = () =>
React.createElement(() => {
[, refresh] = React.useReducer(n => n + 1, 0);
[, refresh] = React.useReducer((n) => n + 1, 0);

const topbarLeftButtonFactory = isGlobalNavBarEnabled() ? _TopbarLeftButtonT : _TopbarLeftButton;
const topbarLeftButtonFactory = isGlobalNavBarEnabled() ? _TopbarLeftButtonB : _TopbarLeftButtonA;

if (!topbarLeftButtonFactoryCtx)
if (!topbarLeftButtonFactoryCtx) {
topbarLeftButtonFactoryCtx = React.createContext<TopbarLeftButtonFactory>(null!);
}

return (
<topbarLeftButtonFactoryCtx.Provider value={topbarLeftButtonFactory}>
Expand All @@ -47,7 +48,7 @@ globalThis.__renderTopbarLeftButtons = () =>
);
});
transformer(
emit => str => {
(emit) => (str) => {
str = str.replace(/("top-bar-forward-button"[^\]]*)/g, "$1,__renderTopbarLeftButtons()");
emit();
return str;
Expand All @@ -70,13 +71,12 @@ export const TopbarLeftButton = (props: TopbarLeftButtonProps) => {

type TopbarLeftButtonFactory = React.FC<TopbarLeftButtonProps>;

const _TopbarLeftButtonT: TopbarLeftButtonFactory = props => (
const _TopbarLeftButtonB: TopbarLeftButtonFactory = (props) => (
<Tooltip label={props.label}>
<UI.ButtonTertiary
size="medium"
iconOnly={() =>
props.icon && createIconComponent({ icon: props.icon, iconSize: 16, realIconSize: 24 })
}
props.icon && createIconComponent({ icon: props.icon, iconSize: 16, realIconSize: 24 })}
condensed
aria-label={props.label}
disabled={props.disabled}
Expand All @@ -86,7 +86,7 @@ const _TopbarLeftButtonT: TopbarLeftButtonFactory = props => (
</Tooltip>
);

const _TopbarLeftButton: TopbarLeftButtonFactory = props => (
const _TopbarLeftButtonA: TopbarLeftButtonFactory = (props) => (
<Tooltip label={props.label}>
<button
aria-label={props.label}
Expand Down
3 changes: 2 additions & 1 deletion modules/stdlib/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const isGlobalNavBarEnabled = () => {
if (!Platform) {
return undefined;
}
const { enableGlobalNavBar } = Platform.getLocalStorageAPI().getItem("remote-config-overrides");
const RemoteConfiguration = Platform.getRemoteConfiguration();
const enableGlobalNavBar = RemoteConfiguration.getValue("enableGlobalNavBar");
return (
enableGlobalNavBar === "home-next-to-navigation" || enableGlobalNavBar === "home-next-to-search"
);
Expand Down
108 changes: 55 additions & 53 deletions modules/stdlib/src/webpack/CODEGEN.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env -S deno run -A

/*
* Copyright (C) 2024 Delusoire
* SPDX-License-Identifier: GPL-3.0-or-later
Expand All @@ -8,32 +10,32 @@
import swc from "npm:@swc/core";

async function* getFileExports(path: string) {
const module = await swc.parseFile(path, {
syntax: "typescript",
tsx: false,
decorators: true,
comments: false,
script: false,
target: "esnext",
});
const module = await swc.parseFile(path, {
syntax: "typescript",
tsx: false,
decorators: true,
comments: false,
script: false,
target: "esnext",
});

for (const node of module.body) {
switch (node.type) {
case "ExportDeclaration": {
switch (node.declaration.type) {
case "VariableDeclaration": {
for (const decl of node.declaration.declarations) {
switch (decl.id.type) {
case "Identifier":
yield decl.id.value;
break;
}
}
}
}
}
}
}
for (const node of module.body) {
switch (node.type) {
case "ExportDeclaration": {
switch (node.declaration.type) {
case "VariableDeclaration": {
for (const decl of node.declaration.declarations) {
switch (decl.id.type) {
case "Identifier":
yield decl.id.value;
break;
}
}
}
}
}
}
}
}

const header = `/*
Expand All @@ -44,42 +46,42 @@ const header = `/*
`;

async function generateBarrelContent(filenames: string[]) {
const content = await Promise.all(filenames.map(async (file) => {
const toImport = `./${file}`;
const importAs = file.replaceAll(".", "_");
const exports = await Array.fromAsync(getFileExports(file));
const importType = `import type * as ${importAs} from "${toImport}";\n`;
const exportLets = exports.map((exp) => `export let ${exp}: typeof ${importAs}.${exp};\n`).join("");
const initializer = `import("${toImport}").then(m => {\n` +
exports.map((exp) => ` ${exp} = m.${exp};\n`).join("") +
`});\n`;
const content = await Promise.all(filenames.map(async (file) => {
const toImport = `./${file}`;
const importAs = file.replaceAll(".", "_");
const exports = await Array.fromAsync(getFileExports(file));
const importType = `import type * as ${importAs} from "${toImport}";\n`;
const exportLets = exports.map((exp) => `export let ${exp}: typeof ${importAs}.${exp};\n`).join("");
const initializer = `import("${toImport}").then(m => {\n` +
exports.map((exp) => ` ${exp} = m.${exp};\n`).join("") +
`});\n`;

return importType + exportLets + initializer;
}));
return header + content.join("\n");
return importType + exportLets + initializer;
}));
return header + content.join("\n");
}

async function main() {
const nameToFilenames: Record<string, string[]> = {};
const nameToFilenames: Record<string, string[]> = {};

for await (const sibling of Deno.readDir(".")) {
if (!sibling.isFile) {
continue;
}
for await (const sibling of Deno.readDir(".")) {
if (!sibling.isFile) {
continue;
}

const [name, type, ext] = sibling.name.split(".");
if (ext !== "ts" || type === "gen") {
continue;
}
const [name, type, ext] = sibling.name.split(".");
if (ext !== "ts" || type === "gen") {
continue;
}

nameToFilenames[name] ??= [];
nameToFilenames[name].push(sibling.name);
}
nameToFilenames[name] ??= [];
nameToFilenames[name].push(sibling.name);
}

for (const [name, filenames] of Object.entries(nameToFilenames)) {
const content = await generateBarrelContent(filenames);
await Deno.writeTextFile(`${name}.gen.ts`, content);
}
for (const [name, filenames] of Object.entries(nameToFilenames)) {
const content = await generateBarrelContent(filenames);
await Deno.writeTextFile(`${name}.gen.ts`, content);
}
}

await main();
2 changes: 0 additions & 2 deletions modules/stdlib/src/webpack/CustomHooks.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export let useContextMenuState: typeof CustomHooks_xpui_ts.useContextMenuState;
export let imageAnalysis: typeof CustomHooks_xpui_ts.imageAnalysis;
export let fallbackPreset: typeof CustomHooks_xpui_ts.fallbackPreset;
export let getPlayContext: typeof CustomHooks_xpui_ts.getPlayContext;
export let useLocation: typeof CustomHooks_xpui_ts.useLocation;
export let useTrackListColumns: typeof CustomHooks_xpui_ts.useTrackListColumns;
export let usePanelStateMachine: typeof CustomHooks_xpui_ts.usePanelStateMachine;
export let extractColorPreset: typeof CustomHooks_xpui_ts.extractColorPreset;
Expand All @@ -23,7 +22,6 @@ import("./CustomHooks.xpui.ts").then(m => {
imageAnalysis = m.imageAnalysis;
fallbackPreset = m.fallbackPreset;
getPlayContext = m.getPlayContext;
useLocation = m.useLocation;
useTrackListColumns = m.useTrackListColumns;
usePanelStateMachine = m.usePanelStateMachine;
extractColorPreset = m.extractColorPreset;
Expand Down
2 changes: 0 additions & 2 deletions modules/stdlib/src/webpack/CustomHooks.xpui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export const fallbackPreset: any = exports.find((m) => m.colorDark);

export const getPlayContext: Function = findBy("referrerIdentifier", "usePlayContextItem")(exportedFunctions);

export const useLocation: Function = findBy("location", "useContext")(exportedFunctions);

export const useTrackListColumns: Function = findBy("useTrackListColumns")(exportedFunctions);

export const usePanelStateMachine: () => [state: any, actor: any, machine: any] = findBy(
Expand Down
2 changes: 2 additions & 0 deletions modules/stdlib/src/webpack/ReactRouter.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import type * as ReactRouter_xpui_ts from "./ReactRouter.xpui.ts";
export let useMatch: typeof ReactRouter_xpui_ts.useMatch;
export let useLocation: typeof ReactRouter_xpui_ts.useLocation;
import("./ReactRouter.xpui.ts").then(m => {
useMatch = m.useMatch;
useLocation = m.useLocation;
});
11 changes: 6 additions & 5 deletions modules/stdlib/src/webpack/ReactRouter.xpui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/

import { chunks, require } from "./index.ts";
import { findBy } from "/hooks/util.ts";
import { chunks, exportedFunctions, require } from "./index.ts";

import type { useMatch as useMatchT } from "npm:react-router";
import type { useLocation as useLocationT, useMatch as useMatchT } from "npm:react-router";

await CHUNKS.xpui.promise;

const [ReactRouterModuleID] = chunks.find(([_, v]) =>
v.toString().includes("React Router")
)!;
const [ReactRouterModuleID] = chunks.find(([_, v]) => v.toString().includes("React Router"))!;
const ReactRouterModule = Object.values(require(ReactRouterModuleID));

// https://github.com/remix-run/react-router/blob/main/packages/react-router/lib/hooks.tsx#L131
export const useMatch: typeof useMatchT = ReactRouterModule.find((f) =>
f.toString().includes("let{pathname:") &&
!f.toString().includes(".createElement(")
);

export const useLocation: typeof useLocationT = findBy("location", "useContext")(exportedFunctions);

0 comments on commit e6a226c

Please sign in to comment.