Skip to content

Commit

Permalink
Add Remix/ESBuild support by checking the _debugOwner._debugSource (#73)
Browse files Browse the repository at this point in the history
* fix: add support to specify project's root

* fix: add path modifier

* fix: remove bad code

* fix: add changeset

* fix: remix example

* fix: use pathModifier in ContextMenu

---------

Co-authored-by: Eric Clemmons <eric@smarterspam.com>
  • Loading branch information
HosseinAgha and ericclemmons committed Nov 25, 2023
1 parent b56ca31 commit 9246a89
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 50 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-bobcats-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'click-to-react-component': minor
---

Add remix/esbuild support by checking the \_debugOwner.\_debugSource
44 changes: 19 additions & 25 deletions apps/remix/app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type {
LinksFunction,
LoaderFunction,
MetaFunction,
} from "@remix-run/node";
import { cssBundleHref } from "@remix-run/css-bundle";
import type { LinksFunction, LoaderArgs } from "@remix-run/node";
import { ClickToComponent } from "click-to-react-component";
import { json } from "@remix-run/node";
import {
Links,
Expand All @@ -11,36 +9,32 @@ import {
Outlet,
Scripts,
ScrollRestoration,
useLoaderData,
} from "@remix-run/react";
import { ClickToComponent } from "click-to-react-component";

import tailwindStylesheetUrl from "./styles/tailwind.css";
import { getUser } from "./session.server";

export const links: LinksFunction = () => {
return [{ rel: "stylesheet", href: tailwindStylesheetUrl }];
};
import { getUser } from "~/session.server";
import stylesheet from "~/tailwind.css";

export const meta: MetaFunction = () => ({
charset: "utf-8",
title: "Remix Notes",
viewport: "width=device-width,initial-scale=1",
});
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: stylesheet },
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
];

type LoaderData = {
user: Awaited<ReturnType<typeof getUser>>;
};

export const loader: LoaderFunction = async ({ request }) => {
return json<LoaderData>({
user: await getUser(request),
export const loader = async ({ request }: LoaderArgs) => {
return json({
user: await getUser(request),
projectDir: process.env.NODE_ENV === 'production' ? undefined : process.cwd(),
});
};

export default function App() {
const { projectDir } = useLoaderData();

return (
<html lang="en" className="h-full">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<Meta />
<Links />
</head>
Expand All @@ -49,7 +43,7 @@ export default function App() {
<ScrollRestoration />
<Scripts />
<LiveReload />
<ClickToComponent editor="vscode-insiders" />
<ClickToComponent pathModifier={(path: string) => `${projectDir}/${path}`} />
</body>
</html>
);
Expand Down
9 changes: 5 additions & 4 deletions apps/remix/app/session.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const sessionStorage = createCookieSessionStorage({
cookie: {
name: "__session",
httpOnly: true,
maxAge: 0,
path: "/",
sameSite: "lax",
secrets: [process.env.SESSION_SECRET],
Expand All @@ -25,13 +24,15 @@ export async function getSession(request: Request) {
return sessionStorage.getSession(cookie);
}

export async function getUserId(request: Request): Promise<string | undefined> {
export async function getUserId(
request: Request
): Promise<User["id"] | undefined> {
const session = await getSession(request);
const userId = session.get(USER_SESSION_KEY);
return userId;
}

export async function getUser(request: Request): Promise<null | User> {
export async function getUser(request: Request) {
const userId = await getUserId(request);
if (userId === undefined) return null;

Expand All @@ -44,7 +45,7 @@ export async function getUser(request: Request): Promise<null | User> {
export async function requireUserId(
request: Request,
redirectTo: string = new URL(request.url).pathname
): Promise<string> {
) {
const userId = await getUserId(request);
if (!userId) {
const searchParams = new URLSearchParams([["redirectTo", redirectTo]]);
Expand Down
3 changes: 3 additions & 0 deletions apps/remix/app/tailwind.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
6 changes: 6 additions & 0 deletions apps/remix/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
7 changes: 0 additions & 7 deletions apps/remix/tailwind.config.js

This file was deleted.

9 changes: 9 additions & 0 deletions apps/remix/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Config } from "tailwindcss";

export default {
content: ["./app/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
} satisfies Config;
7 changes: 3 additions & 4 deletions packages/click-to-react-component/src/ClickToComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const State = /** @type {const} */ ({
/**
* @param {Props} props
*/
export function ClickToComponent({ editor = 'vscode' }) {
export function ClickToComponent({ editor = 'vscode', pathModifier }) {
const [state, setState] = React.useState(
/** @type {State[keyof State]} */
(State.IDLE)
Expand All @@ -41,7 +41,7 @@ export function ClickToComponent({ editor = 'vscode' }) {
) {
if (state === State.HOVER && target instanceof HTMLElement) {
const source = getSourceForElement(target)
const path = getPathToSource(source)
const path = getPathToSource(source, pathModifier)
const url = getUrl({
editor,
pathToSource: path,
Expand Down Expand Up @@ -174,11 +174,9 @@ export function ClickToComponent({ editor = 'vscode' }) {

if (state === State.IDLE) {
delete window.document.body.dataset.clickToComponent

if (target) {
delete target.dataset.clickToComponentTarget
}

return
}

Expand Down Expand Up @@ -233,6 +231,7 @@ export function ClickToComponent({ editor = 'vscode' }) {
${html`<${ContextMenu}
key="click-to-component-contextmenu"
onClose=${onClose}
pathModifier=${pathModifier}
/>`}
</${FloatingPortal}
`
Expand Down
4 changes: 2 additions & 2 deletions packages/click-to-react-component/src/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const ContextMenu = React.forwardRef(
props,
ref
) => {
const { onClose } = props
const { onClose, pathModifier } = props

const [target, setTarget] = React.useState(
/** @type {HTMLElement | null} */
Expand Down Expand Up @@ -327,7 +327,7 @@ export const ContextMenu = React.forwardRef(
${instances.map((instance, i) => {
const name = getDisplayNameForInstance(instance)
const source = getSourceForInstance(instance)
const path = getPathToSource(source)
const path = getPathToSource(source, pathModifier)
const props = getPropsForInstance(instance)
return html`
Expand Down
11 changes: 9 additions & 2 deletions packages/click-to-react-component/src/getPathToSource.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* @typedef {import('react-reconciler').Source} Source
* @typedef {import('./types').PathModifier} PathModifier
*/

/**
* @param {Source} source
* @param {PathModifier} pathModifier
*/
export function getPathToSource(source) {
export function getPathToSource(source, pathModifier) {
const {
// It _does_ exist!
// @ts-ignore Property 'columnNumber' does not exist on type 'Source'.ts(2339)
Expand All @@ -14,5 +16,10 @@ export function getPathToSource(source) {
lineNumber = 1,
} = source

return `${fileName}:${lineNumber}:${columnNumber}`
let path = `${fileName}:${lineNumber}:${columnNumber}`
if (pathModifier) {
path = pathModifier(path)
}

return path
}
9 changes: 6 additions & 3 deletions packages/click-to-react-component/src/getSourceForInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
/**
* @param {Fiber} instance
*/
export function getSourceForInstance({ _debugSource }) {
if (!_debugSource) return
export function getSourceForInstance({ _debugSource, _debugOwner }) {
// source is sometimes stored on _debugOwner
const source = _debugSource || (_debugOwner && _debugOwner._debugSource)

if (!source) return

const {
// It _does_ exist!
// @ts-ignore Property 'columnNumber' does not exist on type 'Source'.ts(2339)
columnNumber = 1,
fileName,
lineNumber = 1,
} = _debugSource
} = source

return { columnNumber, fileName, lineNumber }
}
10 changes: 7 additions & 3 deletions packages/click-to-react-component/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ export { ClickToComponent } from './ClickToComponent'

export type Editor = 'vscode' | 'vscode-insiders'

export type PathModifier = (path: string) => string;

export type ClickToComponentProps = {
editor?: Editor
editor?: Editor;
pathModifier?: PathModifier;
}

export type Coords = [MouseEvent['pageX'], MouseEvent['pageY']]

export type Target = HTMLElement

export type ContextMenuProps = {
onClose?: () => void
}
onClose?: () => void;
pathModifier?: PathModifier;
}

0 comments on commit 9246a89

Please sign in to comment.