Skip to content

Commit

Permalink
feat: dont look
Browse files Browse the repository at this point in the history
  • Loading branch information
itschip committed Dec 19, 2024
1 parent b85cee7 commit 8a9fa74
Show file tree
Hide file tree
Showing 36 changed files with 3,568 additions and 1,077 deletions.
24 changes: 24 additions & 0 deletions apps/new-phone-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
50 changes: 50 additions & 0 deletions apps/new-phone-react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default tseslint.config({
languageOptions: {
// other options...
parserOptions: {
project: ['./tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: import.meta.dirname,
},
},
})
```

- Replace `tseslint.configs.recommended` to `tseslint.configs.recommendedTypeChecked` or `tseslint.configs.strictTypeChecked`
- Optionally add `...tseslint.configs.stylisticTypeChecked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and update the config:

```js
// eslint.config.js
import react from 'eslint-plugin-react'

export default tseslint.config({
// Set the react version
settings: { react: { version: '18.3' } },
plugins: {
// Add the react plugin
react,
},
rules: {
// other rules...
// Enable its recommended rules
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
},
})
```
28 changes: 28 additions & 0 deletions apps/new-phone-react/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'

export default tseslint.config(
{ ignores: ['dist'] },
{
extends: [js.configs.recommended, ...tseslint.configs.recommended],
files: ['**/*.{ts,tsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
},
plugins: {
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...reactHooks.configs.recommended.rules,
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
)
13 changes: 13 additions & 0 deletions apps/new-phone-react/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions apps/new-phone-react/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@npwd/nui",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --mode dev",
"dev:game": "vite build --watch --mode game",
"build": "vite build --mode game",
"build:watch": "vite build --watch --mode game",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"@npwd/keyos": "workspace:*",
"@tanstack/react-query": "^5.62.8",
"@tanstack/react-query-devtools": "^5.62.8",
"jotai": "^2.10.4",
"jotai-devtools": "^0.10.1",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-router": "^7.0.2"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
"@types/react": "^19.0.2",
"@types/react-dom": "^19.0.2",
"@vitejs/plugin-react": "^4.3.4",
"autoprefixer": "^10.4.14",
"eslint": "^9.17.0",
"eslint-plugin-react-hooks": "^5.0.0",
"eslint-plugin-react-refresh": "^0.4.16",
"globals": "^15.13.0",
"postcss": "^8.4.21",
"tailwindcss": "^3.4.0",
"typescript": "~5.6.2",
"typescript-eslint": "^8.18.1",
"vite": "^6.0.3",
"vite-tsconfig-paths": "^5.1.4"
}
}
6 changes: 6 additions & 0 deletions apps/new-phone-react/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
1 change: 1 addition & 0 deletions apps/new-phone-react/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
12 changes: 12 additions & 0 deletions apps/new-phone-react/src/Phone.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RouterProvider } from 'react-router';
import { createRouter } from '@navigation/navigator';
import { useInitApps } from '@system/hooks/useApps';

function Phone() {
const apps = useInitApps();
const router = createRouter(apps);

return <RouterProvider router={router} />
}

export default Phone;
9 changes: 9 additions & 0 deletions apps/new-phone-react/src/apps/Launcher.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AppGrid } from "@native/launcher/components/AppGrid"

export const Launcher = () => {
return (
<div className="h-full w-full bg-gray-500 rounded-xl">
<AppGrid />
</div>
)
}
8 changes: 8 additions & 0 deletions apps/new-phone-react/src/apps/dialer/app.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AppRoute } from '@navigation/navigator';
import { Dialer } from './screens/Dialer';

export default {
name: 'Phone',
path: '/phone',
Component: Dialer,
} satisfies AppRoute;
10 changes: 10 additions & 0 deletions apps/new-phone-react/src/apps/dialer/screens/Dialer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from "react";

export const Dialer: React.FC = () => {
return (
<div>
<h1>Dialer</h1>
<h1>Coming soon...</h1>
</div>
)
}
8 changes: 8 additions & 0 deletions apps/new-phone-react/src/apps/messages/app.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AppRoute } from '@navigation/navigator';
import { Messages } from './screens/Messages';

export default {
name: 'Messages',
path: '/messages',
Component: Messages,
} satisfies AppRoute;
7 changes: 7 additions & 0 deletions apps/new-phone-react/src/apps/messages/screens/Messages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const Messages = () => {
return (
<div>
<h1>Messages</h1>
</div>
)
}
26 changes: 26 additions & 0 deletions apps/new-phone-react/src/http/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export async function fetchApi<T>(url: string, options: RequestInit): Promise<T> {
const res = await fetch(url, options);

if (!res.ok) {
throw new Error(res.statusText);
}

return res.json() as Promise<T>;
}

export async function fxFetch<T = unknown, D = unknown>(eventName: string, data?: D): Promise<T> {
const options = {
method: 'post',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify(data),
};

// @ts-expect-error global nui property
const resourceName = (window as any).GetParentResourceName
? (window as any).GetParentResourceName()
: 'npwd';

return await fetchApi<T>(`https://${resourceName}/${eventName}`, options);
}
3 changes: 3 additions & 0 deletions apps/new-phone-react/src/http/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { QueryClient } from "@tanstack/react-query";

export const queryClient = new QueryClient()
3 changes: 3 additions & 0 deletions apps/new-phone-react/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
22 changes: 22 additions & 0 deletions apps/new-phone-react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import Phone from './Phone.tsx';
import { DevTools } from 'jotai-devtools';
import 'jotai-devtools/styles.css';
import { Provider } from 'jotai';
import { QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { queryClient } from '@http/query.ts';

createRoot(document.getElementById('root')!).render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<Provider>
<DevTools />
<Phone />
</Provider>
<ReactQueryDevtools />
</QueryClientProvider>
</StrictMode>,
);
11 changes: 11 additions & 0 deletions apps/new-phone-react/src/native/components/PhoneFrame.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Outlet } from 'react-router';

export const PhoneFrame = () => {
return (
<div className="bg-red-200 min-h-screen min-w-screen">
<div className="bg-white fixed bottom-5 right-5 w-[450px] h-[900px] rounded-xl shadow-lg">
<Outlet />
</div>
</div>
);
};
20 changes: 20 additions & 0 deletions apps/new-phone-react/src/native/launcher/components/AppGrid.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useApps } from '@system/hooks/useApps';
import { Link } from 'react-router';

export const AppGrid = () => {
const apps = useApps();

return (
<div className="grid grid-cols-4 gap-6 p-6">
{apps.map((app) => (
<Link
to={app.path}
key={app.path}
className="p-6 bg-gray-300 rounded-xl flex items-center justify-center"
>
<h2>{app.name[0]}</h2>
</Link>
))}
</div>
);
};
16 changes: 16 additions & 0 deletions apps/new-phone-react/src/navigation/navigator.state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { atom, useAtomValue, useSetAtom } from "jotai";
import { AppRoute } from "./navigator";

const navigatorAtom = atom<AppRoute[]>([]);

export const rwNavigatorAtom = atom((get) => get(navigatorAtom), (get, set, update: AppRoute) => {
const current = get(navigatorAtom);
if (current.find((route) => route.path === update.path)) {
return;
}

set(navigatorAtom, [...current, update]);
});

export const useSetNavigator = () => useSetAtom(rwNavigatorAtom);
export const useNavigators = () => useAtomValue(rwNavigatorAtom);
27 changes: 27 additions & 0 deletions apps/new-phone-react/src/navigation/navigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Launcher } from "@apps/Launcher";
import { PhoneFrame } from "@native/components/PhoneFrame";
import { createHashRouter } from "react-router";

export interface AppRoute {
name: string;
path: string;
Component: React.ComponentType;
}

export const createRouter = (navigators: AppRoute[]) => {
return createHashRouter([{
path: "/",
Component: PhoneFrame,
children: [
{
index: true,
Component: Launcher,
},
...navigators.map(({ path, Component }) => ({
path,
Component,
})),
]
}])
}

23 changes: 23 additions & 0 deletions apps/new-phone-react/src/system/hooks/useApps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useNavigators, useSetNavigator } from "@navigation/navigator.state";

export const useInitApps = () => {
const setNavigator = useSetNavigator();

const modules = import.meta.glob('/src/apps/*/app.routes.ts', { eager: true });
const apps = Object.values(modules).map((mod) => mod.default).filter((app) => app);

for (const app of apps) {
if (!app) {
continue;
}

setNavigator(app);
}

return apps;
};

export const useApps = () => {
const navigator = useNavigators();
return navigator;
}
Empty file.
Empty file.
Loading

0 comments on commit 8a9fa74

Please sign in to comment.