Skip to content

Commit

Permalink
Update some parts of templates
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed May 3, 2024
1 parent 406edc6 commit db42943
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 113 deletions.
35 changes: 17 additions & 18 deletions packages/create/templates/app-basic/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {type PropsWithChildren} from 'react';

import {HTML} from '@quilted/quilt/html';
import {Routing, useRoutes} from '@quilted/quilt/navigate';
import {Localization, useLocaleFromEnvironment} from '@quilted/quilt/localize';

Expand All @@ -15,25 +14,21 @@ import {
type AppContext as AppContextType,
} from './shared/context.ts';

export interface AppProps extends AppContextType {}
export interface AppProps {
context?: AppContextType;
}

// The root component for your application. You will typically render any
// app-wide context in this component.
export function App(props: AppProps) {
const locale = useLocaleFromEnvironment() ?? 'en';

export function App({context}: AppProps) {
return (
<Localization locale={locale}>
<Routing>
<AppContext {...props}>
<Headers />
<Head />
<Frame>
<Routes />
</Frame>
</AppContext>
</Routing>
</Localization>
<AppContext context={context}>
<Headers />
<Head />
<Frame>
<Routes />
</Frame>
</AppContext>
);
}

Expand All @@ -48,10 +43,14 @@ function Routes() {
}

// This component renders any app-wide context.
function AppContext({children, ...context}: PropsWithChildren<AppProps>) {
function AppContext({children, context}: PropsWithChildren<AppProps>) {
const locale = useLocaleFromEnvironment() ?? 'en';

return (
<AppContextReact.Provider value={context}>
{children}
<Localization locale={locale}>
<Routing>{children}</Routing>
</Localization>
</AppContextReact.Provider>
);
}
9 changes: 8 additions & 1 deletion packages/create/templates/app-basic/browser.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import '@quilted/quilt/globals';
import {hydrateRoot} from 'react-dom/client';
import {Browser, BrowserContext} from '@quilted/quilt/browser';

import {App} from './App.tsx';

const element = document.querySelector('#app')!;
const browser = new Browser();

hydrateRoot(element, <App />);
hydrateRoot(
element,
<BrowserContext browser={browser}>
<App />
</BrowserContext>,
);
62 changes: 23 additions & 39 deletions packages/create/templates/app-graphql/App.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {useMemo, type PropsWithChildren} from 'react';
import {type PropsWithChildren} from 'react';

import {HTML} from '@quilted/quilt/html';
import {Routing, useRoutes} from '@quilted/quilt/navigate';
import {Localization, useLocaleFromEnvironment} from '@quilted/quilt/localize';
import {GraphQLContext, type GraphQLRun} from '@quilted/quilt/graphql';
import {GraphQLContext} from '@quilted/quilt/graphql';

import {ReactQueryContext} from '@quilted/react-query';
import {QueryClient} from '@tanstack/react-query';

import {Head} from './foundation/html.ts';
import {Headers} from './foundation/http.ts';
Expand All @@ -19,29 +17,21 @@ import {
type AppContext as AppContextType,
} from './shared/context.ts';

export interface AppProps extends AppContextType {
fetchGraphQL: GraphQLRun;
export interface AppProps {
context: AppContextType;
}

// The root component for your application. You will typically render any
// app-wide context in this component.
export function App(props: AppProps) {
const locale = useLocaleFromEnvironment() ?? 'en';

export function App({context}: AppProps) {
return (
<HTML>
<Localization locale={locale}>
<Routing>
<AppContext {...props}>
<Headers />
<Head />
<Frame>
<Routes />
</Frame>
</AppContext>
</Routing>
</Localization>
</HTML>
<AppContext context={context}>
<Headers />
<Head />
<Frame>
<Routes />
</Frame>
</AppContext>
);
}

Expand All @@ -56,24 +46,18 @@ function Routes() {
}

// This component renders any app-wide context.
function AppContext({
children,
fetchGraphQL,
...context
}: PropsWithChildren<AppProps>) {
const {queryClient} = useMemo(() => {
return {
queryClient: new QueryClient(),
};
}, []);
function AppContext({children, context}: PropsWithChildren<AppProps>) {
const locale = useLocaleFromEnvironment() ?? 'en';

return (
<GraphQLContext fetch={fetchGraphQL}>
<ReactQueryContext client={queryClient}>
<AppContextReact.Provider value={context}>
{children}
</AppContextReact.Provider>
</ReactQueryContext>
</GraphQLContext>
<AppContextReact.Provider value={context}>
<GraphQLContext fetch={context.fetchGraphQL}>
<ReactQueryContext client={context.queryClient}>
<Localization locale={locale}>
<Routing>{children}</Routing>
</Localization>
</ReactQueryContext>
</GraphQLContext>
</AppContextReact.Provider>
);
}
10 changes: 9 additions & 1 deletion packages/create/templates/app-graphql/browser.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import '@quilted/quilt/globals';

import {hydrateRoot} from 'react-dom/client';
import {QueryClient} from '@tanstack/react-query';
import {createGraphQLFetch} from '@quilted/quilt/graphql';
import {Browser, BrowserContext} from '@quilted/quilt/browser';

import {App} from './App.tsx';

const element = document.querySelector('#app')!;
const browser = new Browser();

const queryClient = new QueryClient();
const fetchGraphQL = createGraphQLFetch({url: '/api/graphql'});

hydrateRoot(
element,
<App fetchGraphQL={createGraphQLFetch({url: '/api/graphql'})} />,
<BrowserContext browser={browser}>
<App context={{fetchGraphQL, queryClient}} />
</BrowserContext>,
);
10 changes: 8 additions & 2 deletions packages/create/templates/app-graphql/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ router.post('/api/graphql', async (request) => {

// For all GET requests, render our React application.
router.get(async (request) => {
const [{App}, {performGraphQLOperation}, {renderToResponse}] =
const [{App}, {performGraphQLOperation}, {renderToResponse}, {QueryClient}] =
await Promise.all([
import('./App.tsx'),
import('./server/graphql.ts'),
import('@quilted/quilt/server'),
import('@tanstack/react-query'),
]);

const response = await renderToResponse(
<App fetchGraphQL={performGraphQLOperation} />,
<App
context={{
fetchGraphQL: performGraphQLOperation,
queryClient: new QueryClient(),
}}
/>,
{
request,
assets,
Expand Down
2 changes: 1 addition & 1 deletion packages/create/templates/app-graphql/server/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Query = createQueryResolver({

const schema = createGraphQLSchema(schemaSource, {Query, Person});

export const performGraphQLOperation: GraphQLRun =
export const performGraphQLOperation: GraphQLRun<{}> =
async function performGraphQLOperation(operation, options) {
const result = await graphql({
schema,
Expand Down
11 changes: 11 additions & 0 deletions packages/create/templates/app-graphql/shared/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {GraphQLFetch} from '@quilted/quilt/graphql';
import type {QueryClient} from '@tanstack/react-query';

declare module '~/shared/context.ts' {
interface AppContext {
queryClient: QueryClient;
fetchGraphQL: GraphQLFetch;
}
}

export {};
73 changes: 29 additions & 44 deletions packages/create/templates/app-trpc/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {useMemo, type PropsWithChildren} from 'react';
import {type PropsWithChildren} from 'react';

import {HTML} from '@quilted/quilt/html';
import {Routing, useRoutes, useInitialUrl} from '@quilted/quilt/navigate';
import {Routing, useRoutes} from '@quilted/quilt/navigate';
import {Localization, useLocaleFromEnvironment} from '@quilted/quilt/localize';

import {httpBatchLink} from '@trpc/client';
import {QueryClient} from '@tanstack/react-query';
import {ReactQueryContext} from '@quilted/react-query';

import {Head} from './foundation/html.ts';
Expand All @@ -20,27 +17,21 @@ import {
type AppContext as AppContextType,
} from './shared/context.ts';

export interface AppProps extends AppContextType {}
export interface AppProps {
context: AppContextType;
}

// The root component for your application. You will typically render any
// app-wide context in this component.
export function App(props: AppProps) {
const locale = useLocaleFromEnvironment() ?? 'en';

export function App({context}: AppProps) {
return (
<HTML>
<Localization locale={locale}>
<Routing>
<AppContext {...props}>
<Headers />
<Head />
<Frame>
<Routes />
</Frame>
</AppContext>
</Routing>
</Localization>
</HTML>
<AppContext context={context}>
<Headers />
<Head />
<Frame>
<Routes />
</Frame>
</AppContext>
);
}

Expand All @@ -55,29 +46,23 @@ function Routes() {
}

// This component renders any app-wide context.
function AppContext({children, ...context}: PropsWithChildren<AppProps>) {
const initialUrl = useInitialUrl();

const {queryClient, trpcClient} = useMemo(() => {
return {
queryClient: new QueryClient(),
trpcClient: trpc.createClient({
links: [
// We need to use an absolute URL so that queries will
// work during server-side rendering
httpBatchLink({url: new URL('/api', initialUrl).href}),
],
}),
};
}, [initialUrl]);
function AppContext({children, context}: PropsWithChildren<AppProps>) {
const locale = useLocaleFromEnvironment() ?? 'en';

return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<ReactQueryContext client={queryClient}>
<AppContextReact.Provider value={context}>
{children}
</AppContextReact.Provider>
</ReactQueryContext>
</trpc.Provider>
<AppContextReact.Provider value={context}>
<Localization locale={locale}>
<Routing>
<trpc.Provider
client={context.trpc}
queryClient={context.queryClient}
>
<ReactQueryContext client={context.queryClient}>
{children}
</ReactQueryContext>
</trpc.Provider>
</Routing>
</Localization>
</AppContextReact.Provider>
);
}
11 changes: 10 additions & 1 deletion packages/create/templates/app-trpc/browser.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import '@quilted/quilt/globals';
import {hydrateRoot} from 'react-dom/client';
import {httpBatchLink} from '@trpc/client';
import {QueryClient} from '@tanstack/react-query';
import {Browser, BrowserContext} from '@quilted/quilt/browser';

import {trpc} from '~/shared/trpc.ts';

import {App} from './App.tsx';

const element = document.querySelector('#app')!;
const browser = new Browser();

const queryClient = new QueryClient();
const trpcClient = trpc.createClient({
links: [httpBatchLink({url: new URL('/api', window.location.href).href})],
});

hydrateRoot(element, <App trpc={trpcClient} />);
hydrateRoot(
element,
<BrowserContext browser={browser}>
<App context={{trpc: trpcClient, queryClient}} />
</BrowserContext>,
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Title, Viewport, Favicon, SearchRobots} from '@quilted/quilt/html';
import {Title, Viewport, Favicon, SearchRobots} from '@quilted/quilt/browser';

// This component sets details of the HTML page. If you need to customize
// any of these details based on conditions like the active route, or some
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ContentSecurityPolicy,
PermissionsPolicy,
StrictTransportSecurity,
} from '@quilted/quilt/http';
useBro,
} from '@quilted/quilt/server';

// This component sets details on the HTTP response for all HTML server-rendering
// requests. If you need to customize any of these details based on conditions like
Expand Down
10 changes: 8 additions & 2 deletions packages/create/templates/app-trpc/server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ router.any(

// For all GET requests, render our React application.
router.get(async (request) => {
const [{App}, {renderToResponse}] = await Promise.all([
const [{App}, {renderToResponse}, {QueryClient}] = await Promise.all([
import('./App.tsx'),
import('@quilted/quilt/server'),
import('@tanstack/react-query'),
]);

const response = await renderToResponse(
<App trpc={createDirectClient(appRouter)} />,
<App
context={{
trpc: createDirectClient(appRouter),
queryClient: new QueryClient(),
}}
/>,
{
request,
assets,
Expand Down
Loading

0 comments on commit db42943

Please sign in to comment.