Skip to content

Commit 759ea59

Browse files
committed
add configurable branch and file path
1 parent 4312cf7 commit 759ea59

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

src/Tool.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { PARAM_KEY, TOOL_ID } from "./constants";
55
import CodeflowLogo from "./components/CodeflowLogo";
66

77
export const Tool = function MyAddonSelector() {
8-
const githubUrl = useParameter(PARAM_KEY);
8+
const repositoryUrl = useParameter<string>(PARAM_KEY.REPO, null);
9+
const branch = useParameter<string>(PARAM_KEY.BRANCH, 'main');
10+
const filePath = useParameter<string>(PARAM_KEY.FILE_PATH, null);
11+
912
const api = useStorybookApi();
1013
const [disabled, setDisabled] = useState(false)
1114

@@ -15,14 +18,22 @@ export const Tool = function MyAddonSelector() {
1518
return null;
1619
}
1720

18-
if (!githubUrl && !disabled) {
21+
if (!repositoryUrl && !disabled) {
1922
console.warn(`"${PARAM_KEY}" parameter not defined. Make sure to configure it in your story.`);
2023
setDisabled(true);
21-
} else if (githubUrl && disabled) {
24+
} else if (repositoryUrl && disabled) {
2225
setDisabled(false);
2326
}
2427

25-
const stackblitzUrl = `https://pr.new/${githubUrl}`;
28+
let stackblitzUrl = `https://pr.new/${repositoryUrl}`;
29+
if (filePath) {
30+
stackblitzUrl = `${stackblitzUrl}/blob/${branch}/${filePath}`;
31+
/*
32+
* We've just addded `/` between all segments not caring if user already appended or prepanded them,
33+
* so let's remove any possible double `//` (not preceded by `:` so we don't mess up the `https://`)
34+
*/
35+
stackblitzUrl = stackblitzUrl.replaceAll(/(?<!:)\/\//g, '/')
36+
}
2637

2738
return (
2839
<IconButton

src/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export const ADDON_ID = "storybook/stackblitz";
22
export const TOOL_ID = `${ADDON_ID}/tool`;
3-
export const PARAM_KEY = `repositoryUrl`;
3+
export const PARAM_KEY = {
4+
FILE_PATH: `filePath`,
5+
REPO: `repositoryUrl`,
6+
BRANCH: `branch`,
7+
};

src/preview.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { PARAM_KEY } from "./constants";
1818

1919
const preview: ProjectAnnotations<Renderer> = {
2020
parameters: {
21-
[PARAM_KEY]: `https://github.com/stackblitz/docs`,
21+
[PARAM_KEY.REPO]: `https://github.com/stackblitz/docs`,
2222
}
2323
};
2424

src/stories/Button.stories.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const Primary: Story = {
2828
},
2929
parameters: {
3030
// repositoryUrl: `https://github.com/stackblitz/docs`
31+
filePath: 'docs/links.ts'
3132
}
3233
};
3334

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"incremental": false,
88
"isolatedModules": true,
99
"jsx": "react",
10-
"lib": ["es2020", "dom"],
10+
"lib": ["es2021", "dom"],
1111
"module": "commonjs",
1212
"noImplicitAny": true,
1313
"rootDir": "./src",

0 commit comments

Comments
 (0)