Skip to content

Commit

Permalink
Improve routing, and move the action pwa action receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
enricoros committed Nov 22, 2023
1 parent 6ed8529 commit 16916db
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 16 deletions.
13 changes: 7 additions & 6 deletions pages/launch.tsx → pages/link/share_target.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { useRouter } from 'next/router';
import { Alert, Box, Button, Typography } from '@mui/joy';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';

import { setComposerStartupText } from '../src/apps/chat/components/composer/store-composer';
import { setComposerStartupText } from '../../src/apps/chat/components/composer/store-composer';

import { AppLayout } from '~/common/layout/AppLayout';
import { LogoProgress } from '~/common/components/LogoProgress';
import { asValidURL } from '~/common/util/urlUtils';
import { navigateToIndex } from '~/common/app.routes';


/**
Expand All @@ -28,13 +29,13 @@ function AppShareTarget() {
const [isDownloading, setIsDownloading] = React.useState(false);

// external state
const { query, push: routerPush, replace: routerReplace } = useRouter();
const { query } = useRouter();


const queueComposerTextAndLaunchApp = React.useCallback((text: string) => {
setComposerStartupText(text);
void routerReplace('/');
}, [routerReplace]);
void navigateToIndex(true);
}, []);


// Detect the share Intent from the query
Expand Down Expand Up @@ -110,7 +111,7 @@ function AppShareTarget() {
</Alert>
<Button
variant='solid' color='danger'
onClick={() => routerPush('/')}
onClick={() => navigateToIndex()}
endDecorator={<ArrowBackIcon />}
sx={{ mt: 2 }}
>
Expand All @@ -130,7 +131,7 @@ function AppShareTarget() {

/**
* This page will be invoked on mobile when sharing Text/URLs/Files from other APPs
* Example URL: https://get.big-agi.com/launch?title=This+Title&text=https%3A%2F%2Fexample.com%2Fapp%2Fpath
* Example URL: https://localhost:3000/link/share_target?title=This+Title&text=https%3A%2F%2Fexample.com%2Fapp%2Fpath
*/
export default function LaunchPage() {
return (
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
],
"share_target": {
"action": "/launch",
"action": "/link/share_target",
"method": "GET",
"enctype": "application/x-www-form-urlencoded",
"params": {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chat/components/composer/Composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export function Composer(props: {
const remainingTokens = tokenLimit - directTokens - historyTokens - responseTokens;


// Effect: load initial text if queued up (e.g. by /launch)
// Effect: load initial text if queued up (e.g. by /link/share_targe)
React.useEffect(() => {
if (startupText) {
setStartupText(null);
Expand Down
4 changes: 2 additions & 2 deletions src/apps/link/AppChatLinkDrawerItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useChatLinkItems } from '~/modules/trade/store-module-trade';
import { Brand } from '~/common/app.config';
import { Link } from '~/common/components/Link';
import { closeLayoutDrawer } from '~/common/layout/store-applayout';
import { getChatLinkRelativePath, getHomeLink } from '~/common/app.routes';
import { getChatLinkRelativePath, ROUTE_INDEX } from '~/common/app.routes';


/**
Expand All @@ -28,7 +28,7 @@ export function AppChatLinkDrawerItems() {

<MenuItem
onClick={closeLayoutDrawer}
component={Link} href={getHomeLink()} noLinkStyle
component={Link} href={ROUTE_INDEX} noLinkStyle
>
<ListItemDecorator><ArrowBackIcon /></ListItemDecorator>
{Brand.Title.Base}
Expand Down
4 changes: 2 additions & 2 deletions src/apps/news/AppNews.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import ExpandMoreIcon from '@mui/icons-material/ExpandMore';

import { Brand } from '~/common/app.config';
import { Link } from '~/common/components/Link';
import { ROUTE_INDEX } from '~/common/app.routes';
import { capitalizeFirstLetter } from '~/common/util/textUtils';

import { newsCallout, NewsItems } from './news.data';
import { ROUTE_APP_CHAT } from '~/common/app.routes';


export function AppNews() {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function AppNews() {
<Box>
<Button
variant='solid' color='neutral' size='lg'
component={Link} href={ROUTE_APP_CHAT} noLinkStyle
component={Link} href={ROUTE_INDEX} noLinkStyle
endDecorator='✨'
sx={{ minWidth: 200 }}
>
Expand Down
15 changes: 11 additions & 4 deletions src/common/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@

import Router from 'next/router';

export const ROUTE_APP_CHAT = '/';
const APP_LINK_CHAT = '/link/chat/:linkId';
export const ROUTE_INDEX = '/';
export const ROUTE_APP_CHAT = '/chat';
export const ROUTE_APP_LINK_CHAT = '/link/chat/:linkId';
export const ROUTE_APP_NEWS = '/news';

export const getHomeLink = () => ROUTE_APP_CHAT;
export const getIndexLink = () => ROUTE_INDEX;

export const getChatLinkRelativePath = (chatLinkId: string) => APP_LINK_CHAT.replace(':linkId', chatLinkId);
export const getChatLinkRelativePath = (chatLinkId: string) => ROUTE_APP_LINK_CHAT.replace(':linkId', chatLinkId);

const navigateFn = (path: string) => (replace?: boolean): Promise<boolean> =>
Router[replace ? 'replace' : 'push'](path);

export const navigateToIndex = navigateFn(ROUTE_INDEX);
export const navigateToChat = async () => await Router.push(ROUTE_APP_CHAT);
export const navigateToNews = navigateFn(ROUTE_APP_NEWS);

export const navigateBack = Router.back;

Expand Down

0 comments on commit 16916db

Please sign in to comment.