-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add fork dashboard function * add test * fix --------- Co-authored-by: guyu <[email protected]>
- Loading branch information
Showing
8 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
40 changes: 40 additions & 0 deletions
40
client/app/pages/dashboards/hooks/useDuplicateDashboard.js
This file contains 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,40 @@ | ||
import { noop, extend, pick } from "lodash"; | ||
import { useCallback, useState } from "react"; | ||
import url from "url"; | ||
import qs from "query-string"; | ||
import { Dashboard } from "@/services/dashboard"; | ||
|
||
function keepCurrentUrlParams(targetUrl) { | ||
const currentUrlParams = qs.parse(window.location.search); | ||
targetUrl = url.parse(targetUrl); | ||
const targetUrlParams = qs.parse(targetUrl.search); | ||
return url.format( | ||
extend(pick(targetUrl, ["protocol", "auth", "host", "pathname"]), { | ||
search: qs.stringify(extend(currentUrlParams, targetUrlParams)), | ||
}) | ||
); | ||
} | ||
|
||
export default function useDuplicateDashboard(dashboard) { | ||
const [isDuplicating, setIsDuplicating] = useState(false); | ||
|
||
const duplicateDashboard = useCallback(() => { | ||
// To prevent opening the same tab, name must be unique for each browser | ||
const tabName = `duplicatedDashboardTab/${Math.random().toString()}`; | ||
|
||
// We should open tab here because this moment is a part of user interaction; | ||
// later browser will block such attempts | ||
const tab = window.open("", tabName); | ||
|
||
setIsDuplicating(true); | ||
Dashboard.fork({ id: dashboard.id }) | ||
.then(newDashboard => { | ||
tab.location = keepCurrentUrlParams(newDashboard.getUrl()); | ||
}) | ||
.finally(() => { | ||
setIsDuplicating(false); | ||
}); | ||
}, [dashboard.id]); | ||
|
||
return [isDuplicating, isDuplicating ? noop : duplicateDashboard]; | ||
} |
This file contains 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 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 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 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 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