Repository navigation
refactor(chat): one navigation decision for every link surface CU-17tkuw61kfy #2883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a3519f1
feat(chat): CU-17tkuw61kfy chat videos open from their card instead o…
michaelassraf b6cc660
feat(chat): CU-17tkuw61kfy card footers and source strips keep their …
michaelassraf facc207
fix(chat): CU-17tkuw61kfy the sources strip's skeleton is one row, on…
michaelassraf c56da62
feat(chat): CU-17tkuw61kfy a link to a screen of the app is an inline…
michaelassraf eb0d984
refactor(chat): CU-17tkuw61kfy one inline box component, InlineChip
michaelassraf 556d341
refactor(chat): CU-17tkuw61kfy one card disclosure, an explicit app-s…
michaelassraf 120f283
refactor(chat): CU-17tkuw61kfy in-app links by the runtime's rule, di…
michaelassraf ee333ac
Merge remote-tracking branch 'origin/main' into feat/mcp-in-app-links
michaelassraf bb8745a
fix(chat): CU-17tkuw61kfy the in-app link box is drawn inside the rou…
michaelassraf 98be922
refactor(chat): CU-17tkuw61kfy markdown links read the navigation SSOT
michaelassraf 81cf49d
refactor(chat): CU-17tkuw61kfy one navigation decision for every link…
michaelassraf 8c78f9b
Merge branch 'main' into feat/mcp-in-app-links
michaelassraf 5356117
refactor(chat): CU-17tkuw61kfy navigation goes through the host, with…
michaelassraf f636338
Merge remote-tracking branch 'origin/feat/mcp-in-app-links' into feat…
michaelassraf 28f0873
feat(lint): CU-17tkuw61kfy flamingo/no-navigation-fallback blocks a s…
michaelassraf d85c25b
feat(chat): CU-17tkuw61kfy the navigation hooks a host's own links use
michaelassraf b17ce36
fix(chat): CU-17tkuw61kfy a link keeps the hub's old behavior exactly
michaelassraf eeea97f
Merge remote-tracking branch 'origin/main' into feat/mcp-in-app-links
michaelassraf 5f148ed
fix(chat): CU-17tkuw61kfy one navigation path across the lib, from re…
michaelassraf 5ffc671
fix(chat): CU-17tkuw61kfy review findings on the one navigation path
michaelassraf 3b525ca
feat(chat): CU-17tkuw61kfy the host sets how many source skeletons an…
michaelassraf 558e959
Merge branch 'main' into feat/mcp-in-app-links
michaelassraf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /** | ||
| * A link is followed in ONE way: the lib's navigation executor (`executeNavigation`, `executeNavigationImperative`, | ||
| * `executeNewTabNavigation`, the hooks built on it), which hands a same-tab link to the host's `runtime.navigation.navigate` and lets the browser | ||
| * open what the host does not take. Nothing beside it navigates: no fallback option handed to it, and no router push or | ||
| * `location` write in the same function as a call to it. A second path is how a host's routing gets skipped quietly | ||
| * (CU-17tkuw61kfy: every caller passed `fallbackNavigate: router.push`). If a host does not navigate, wire its | ||
| * `navigate`; never route around it. | ||
| */ | ||
|
|
||
| /** The executor and the hooks that call it. */ | ||
| const EXECUTORS = new Set([ | ||
| 'executeNavigation', | ||
| 'executeNavigationImperative', | ||
| 'executeNewTabNavigation', | ||
| 'useNavLinkViaRuntime', | ||
| 'useNavigateViaRuntime', | ||
| ]); | ||
|
|
||
| /** `router.push(...)`, `router.replace(...)`: a router call on anything named like a router. */ | ||
| const ROUTER_METHOD = new Set(['push', 'replace']); | ||
| const LOCATION_METHOD = new Set(['assign', 'replace']); | ||
|
|
||
| function calleeName(callee) { | ||
| if (callee.type === 'Identifier') return callee.name; | ||
| if (callee.type === 'MemberExpression' && !callee.computed && callee.property.type === 'Identifier') { | ||
| return callee.property.name; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function objectName(node) { | ||
| if (node.type === 'Identifier') return node.name; | ||
| if (node.type === 'MemberExpression' && !node.computed && node.property.type === 'Identifier') { | ||
| return node.property.name; | ||
| } | ||
| if (node.type === 'CallExpression') return calleeName(node.callee); | ||
| return null; | ||
| } | ||
|
|
||
| /** A direct navigation: `router.push(x)`, `location.assign(x)`, `window.open(x)`, or a `location.href =` write. */ | ||
| function directNavigation(node) { | ||
| if (node.type === 'CallExpression' && node.callee.type === 'MemberExpression' && !node.callee.computed) { | ||
| const method = node.callee.property.name; | ||
| const owner = objectName(node.callee.object); | ||
| if (!owner) return null; | ||
| if (ROUTER_METHOD.has(method) && /router$/i.test(owner)) return `${owner}.${method}()`; | ||
| if (LOCATION_METHOD.has(method) && owner === 'location') return `location.${method}()`; | ||
| if (method === 'open' && owner === 'window') return 'window.open()'; | ||
| } | ||
| if ( | ||
| node.type === 'AssignmentExpression' && | ||
| node.left.type === 'MemberExpression' && | ||
| !node.left.computed && | ||
| node.left.property.name === 'href' && | ||
| objectName(node.left.object) === 'location' | ||
| ) { | ||
| return 'location.href ='; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| const FUNCTION_TYPES = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression']); | ||
|
|
||
| /** @type {import('eslint').Rule.RuleModule} */ | ||
| const noNavigationFallback = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { description: 'Disallow a second way to navigate beside the lib navigation executor' }, | ||
| schema: [], | ||
| messages: { | ||
| option: | ||
| "`{{key}}` hands {{executor}} a second way to navigate. Links go through the host's " + | ||
| '`runtime.navigation.navigate`; a host that does not navigate wires `navigate`, never a fallback.', | ||
| beside: | ||
| '{{call}} navigates beside {{executor}} in the same function. Follow a link through the executor only; a ' + | ||
| 'host that does not navigate wires `runtime.navigation.navigate`.', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| // The module that DEFINES the executor is where navigating is implemented; the rule guards its callers. | ||
| const definesExecutor = context.sourceCode.ast.body.some(statement => { | ||
| const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement; | ||
| return declaration?.type === 'FunctionDeclaration' && EXECUTORS.has(declaration.id?.name); | ||
| }); | ||
| if (definesExecutor) return {}; | ||
| /** Per enclosing function: the executor calls and the direct navigations anywhere inside it (nested handlers | ||
| * included). The file itself is a frame too, but only for its top-level code: two components in one file are | ||
| * two functions. */ | ||
| const frames = []; | ||
| const reported = new Set(); | ||
| const enter = () => frames.push({ executors: [], direct: [] }); | ||
| const exit = () => { | ||
| const frame = frames.pop(); | ||
| if (!frame || frame.executors.length === 0) return; | ||
| for (const { node, call } of frame.direct) { | ||
| if (reported.has(node)) continue; | ||
| reported.add(node); | ||
| context.report({ node, messageId: 'beside', data: { call, executor: frame.executors[0] } }); | ||
| } | ||
| }; | ||
| /** The frames a fact belongs to: every enclosing function, or the file when at the top level. */ | ||
| const owners = () => (frames.length > 1 ? frames.slice(1) : frames); | ||
| const noteDirect = node => { | ||
| const call = directNavigation(node); | ||
| if (call) for (const frame of owners()) frame.direct.push({ node, call }); | ||
| }; | ||
|
|
||
| return { | ||
| Program: enter, | ||
| 'Program:exit': exit, | ||
| ...Object.fromEntries( | ||
| [...FUNCTION_TYPES].flatMap(t => [ | ||
| [t, enter], | ||
| [`${t}:exit`, exit], | ||
| ]), | ||
| ), | ||
| CallExpression(node) { | ||
| const name = calleeName(node.callee); | ||
| if (name && EXECUTORS.has(name)) { | ||
| for (const frame of owners()) frame.executors.push(name); | ||
| for (const arg of node.arguments) { | ||
| if (arg.type !== 'ObjectExpression') continue; | ||
| for (const prop of arg.properties) { | ||
| const key = prop.type === 'Property' && !prop.computed ? (prop.key.name ?? prop.key.value) : null; | ||
| if (typeof key === 'string' && /^fallback/i.test(key)) { | ||
| context.report({ node: prop, messageId: 'option', data: { key, executor: name } }); | ||
| } | ||
| } | ||
| } | ||
| return; | ||
| } | ||
| noteDirect(node); | ||
| }, | ||
| AssignmentExpression: noteDirect, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export const navigationPlugin = { | ||
| meta: { name: 'flamingo-navigation' }, | ||
| rules: { 'no-navigation-fallback': noNavigationFallback }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
openframe-frontend-core/src/__tests__/eslint-navigation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { RuleTester } from 'eslint'; | ||
| import { navigationPlugin } from '../../eslint-config/navigation.js'; | ||
|
|
||
| new RuleTester().run('no-navigation-fallback', navigationPlugin.rules['no-navigation-fallback'], { | ||
| valid: [ | ||
| // The executor alone. | ||
| 'function onClick(e) { executeNavigation({ event: e, runtime, href, path, targetPlatform, hostRoute }); }', | ||
| 'function open() { executeNewTabNavigation({ runtime, href, targetPlatform: null }); }', | ||
| // A router call with no executor in the function: a query-string write, not a link. | ||
| 'function setPage(p) { router.replace(`${pathname}?page=${p}`); }', | ||
| // Two components in one file: one follows links, the other writes the URL. | ||
| 'function Row() { const go = e => executeNavigation({ event: e, runtime, href }); }\n' + | ||
| 'function List() { router.replace(`${pathname}?q=1`); }', | ||
| // The module that defines the executor implements navigating. | ||
| 'export function executeNavigation(args) { window.location.assign(args.href); }', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| // CU-17tkuw61kfy: every caller handed the executor `router.push`. | ||
| code: 'function onClick(e) { executeNavigation({ event: e, runtime, href, fallbackNavigate: router.push }); }', | ||
| errors: [{ messageId: 'option' }], | ||
| }, | ||
| { | ||
| code: 'function go() { executeNavigationImperative({ runtime, href, fallback: href => location.assign(href) }); }', | ||
| errors: [{ messageId: 'option' }, { messageId: 'beside' }], | ||
| }, | ||
| { | ||
| // A second path after the executor. | ||
| code: 'function onClick(e) { if (!executeNavigation({ event: e, runtime, href })) router.push(href); }', | ||
| errors: [{ messageId: 'beside' }], | ||
| }, | ||
| { | ||
| // A new tab opened by hand beside the executor. | ||
| code: | ||
| 'function onSelect(row, wantsNewTab) {\n' + | ||
| " if (wantsNewTab) { window.open(row.href, '_blank'); return; }\n" + | ||
| ' executeNavigationImperative({ runtime, href: row.href });\n' + | ||
| '}', | ||
| errors: [{ messageId: 'beside' }], | ||
| }, | ||
| { | ||
| code: 'function onClick(e) { executeNavigation({ event: e, runtime, href }) || (window.location.href = href); }', | ||
| errors: [{ messageId: 'beside' }], | ||
| }, | ||
| { | ||
| // A handler nested in a component: the component follows links, so it navigates only through the executor. | ||
| code: | ||
| 'function Card() {\n' + | ||
| ' const onClick = e => executeNavigation({ event: e, runtime, href });\n' + | ||
| ' const onKey = () => router.push(href);\n' + | ||
| '}', | ||
| errors: [{ messageId: 'beside' }], | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.