-
Notifications
You must be signed in to change notification settings - Fork 5
Permalinks Feature - Abel #1281
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
Open
abelpz
wants to merge
19
commits into
unfoldingWord:develop
Choose a base branch
from
idiomaspuentes:feature-abel-permalinks
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
81c7e7d
Merge pull request #1 from unfoldingWord/develop
abelpz 29abe50
Added permalinks hook
abelpz 23bd0c2
first stage of permalinks feature
abelpz eb083b0
Clean up
abelpz cda58f3
Merge pull request #2 from unfoldingWord/develop
abelpz e680e09
Merge branch 'develop' of https://github.com/idiomaspuentes/tc-create…
abelpz dbaf33c
Review fixes
abelpz 173e1c5
permalinks optimization
abelpz 449f46f
Merge branch 'develop' of https://github.com/unfoldingWord/tc-create-…
abelpz 16cfce6
add history state handling
abelpz 8589dea
bump permalinks-hooks
abelpz e4a9afb
remove unnecesary helpers
abelpz 91db065
rm file
abelpz c99a619
clean up
abelpz 0c2529b
Merge branch 'develop' of https://github.com/unfoldingWord/tc-create-…
abelpz 2026410
Merge branch 'feature-abel-permalinks' into develop
abelpz b771798
Merge pull request #3 from idiomaspuentes/develop
abelpz 035f023
replace dcs-js with grt
abelpz 86550a8
remove unnecessaryinitialState prop
abelpz 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 |
---|---|---|
@@ -1 +1 @@ | ||
147-d089b2c | ||
149-e680e09 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { useContext } from 'react' | ||
import { useDeepCompareEffect } from 'use-deep-compare'; | ||
import { useNavigation,PermalinksConfig } from '@gwdevs/permalinks-hooks'; | ||
import { AppContext } from '../../App.context'; | ||
import useFormattedLink from './useFormattedLink'; | ||
import routes from './routes.json'; | ||
|
||
|
||
export default function PermalinksHandler({ children }) { | ||
const { state } = useContext(AppContext); | ||
|
||
const formattedLink = useFormattedLink(state); | ||
const { push } = useNavigation(); | ||
|
||
useDeepCompareEffect(() => { | ||
push(formattedLink, state); | ||
}, [formattedLink, push, state]); | ||
|
||
return ( | ||
<PermalinksConfig routes={routes}>{children}</PermalinksConfig> | ||
) | ||
} |
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,9 @@ | ||
export function getStateKeys(state = {}) { | ||
const { filepath, organization, sourceRepository, language } = state; | ||
return { | ||
organization: organization?.username, | ||
language: language?.languageId, | ||
resource: sourceRepository?.full_name.split('/')[1].split('_')[1], | ||
filepath: filepath, | ||
} | ||
} |
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,6 @@ | ||
[ | ||
{ | ||
"entry": "project", | ||
"path": ["organization","language","resource","...filepath"] | ||
} | ||
] |
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,25 @@ | ||
import { useState } from 'react'; | ||
import { useDeepCompareEffect } from 'use-deep-compare'; | ||
import useStateKeys from './useStateKeys'; | ||
import { useLocation } from '@gwdevs/permalinks-hooks'; | ||
|
||
export default function useFormattedLink({ filepath, organization, sourceRepository, language }) { | ||
const { keys } = useStateKeys({ filepath, organization, sourceRepository, language }); | ||
const [formattedLink, setFormattedLink] = useState(); | ||
const { search } = useLocation(); | ||
|
||
useDeepCompareEffect(() => { | ||
const entry = 'project'; | ||
const org = keys?.organization; | ||
const lang = keys?.language; | ||
const repo = keys?.resource; | ||
const filepath = keys?.filepath; | ||
|
||
const path = [org, lang, repo, filepath].filter(Boolean).join('/'); | ||
if (!!path) { | ||
setFormattedLink(entry + '/' + path + search); | ||
} | ||
}, [keys,search]); | ||
|
||
return formattedLink; | ||
} |
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,78 @@ | ||
import { useState } from 'react'; | ||
import { useLanguages } from 'uw-languages-rcl'; | ||
import { usePermalinks } from '@gwdevs/permalinks-hooks'; | ||
import useOrgApi from '../../hooks/api/useOrgApi'; | ||
import useRepoApi from '../../hooks/api/useRepoApi'; | ||
|
||
import { getLanguage } from '../../components/languages/helpers'; | ||
import { useDeepCompareCallback, useDeepCompareEffect } from 'use-deep-compare'; | ||
import routes from './routes.json'; | ||
import useStateKeys from './useStateKeys'; | ||
|
||
export default function usePermalinksState(loadedState) { | ||
const { permalink, isLoading: isLoadingPermalink } = usePermalinks({ routes }); | ||
const [permalinkState, setPermalinkState] = useState(); | ||
|
||
const { state: languages } = useLanguages(); | ||
const orgClient = useOrgApi(); | ||
const repoClient = useRepoApi(); | ||
|
||
const { keys: loadedKeys } = useStateKeys(loadedState); | ||
|
||
const getPermalinkState = useDeepCompareCallback(async () => { | ||
|
||
if (!languages?.length || !orgClient || !repoClient || !loadedState) return; | ||
|
||
console.log('Loading app from permalink...'); | ||
|
||
const authentication = loadedState.authentication; | ||
|
||
const organization = loadedKeys.organization === permalink.organization | ||
? loadedState.organization | ||
: permalink.organization && await orgClient.orgGet(permalink.organization).then(({ data }) => data).catch(err => console.warn(err)) | ||
|
||
const language = loadedKeys.language === permalink.language | ||
? loadedState.language | ||
: getLanguage({ languageId: permalink.language, languagesJSON: languages }); | ||
|
||
if (!language) { | ||
alert(`Language "${permalink.language}" was not found.`); | ||
} | ||
|
||
const sourceRepoName = 'en_' + permalink.resource; | ||
const sourceRepository = loadedKeys.resource === permalink.resource | ||
? loadedState.sourceRepository | ||
: permalink.resource && await repoClient.repoGet('unfoldingWord', sourceRepoName).then(({ data }) => { | ||
data.tree_url = `api/v1/repos/unfoldingWord/${sourceRepoName}/git/trees/master`; | ||
return data; | ||
}).catch(err => console.warn(err)); | ||
|
||
const filepath = permalink.filepath; | ||
|
||
const _permalinkState = { | ||
authentication, | ||
language, | ||
sourceRepository, | ||
filepath, | ||
organization, | ||
resourceLinks: null, | ||
}; | ||
setPermalinkState(_permalinkState); | ||
window.history.replaceState(_permalinkState,''); | ||
}, [languages, orgClient, repoClient, permalink, loadedKeys, loadedState]); | ||
|
||
useDeepCompareEffect(() => { | ||
|
||
if (isLoadingPermalink && !loadedState) | ||
return; | ||
|
||
if (permalink && !permalinkState) | ||
getPermalinkState(); | ||
|
||
if (!permalink && !permalinkState) | ||
setPermalinkState(loadedState); | ||
|
||
}, [isLoadingPermalink, permalink, loadedState, permalinkState, getPermalinkState]); | ||
|
||
return { permalinkState, isLoading: isLoadingPermalink || (!!permalink && !permalinkState)} | ||
} |
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,39 @@ | ||
import { useState } from 'react'; | ||
import { useDeepCompareEffect } from 'use-deep-compare'; | ||
import { usePermalinks } from '@gwdevs/permalinks-hooks'; | ||
|
||
export default function useQueryOptions({defaultOptions,columnNames}) { | ||
const { query } = usePermalinks({}); | ||
const [extraColumns, setExtraColumns] = useState([]); | ||
const [options, setOptions] = useState(defaultOptions); | ||
|
||
useDeepCompareEffect(() => { | ||
const exludedFromSearch = ['columns', 'check']; | ||
|
||
if (!query) return; | ||
|
||
Object.keys(query).forEach((key) => { | ||
if (!exludedFromSearch.includes(key)) { | ||
setOptions((options) => ({ ...options, searchText: query[key] })); | ||
} | ||
}); | ||
|
||
},[query]); | ||
|
||
useDeepCompareEffect(() => { | ||
if (query && columnNames) { | ||
const queryColumns = query.columns?.split(','); | ||
const validColumns = columnNames.map(column => queryColumns && queryColumns.includes(column) && column) || []; | ||
|
||
const columnsShowDefault = columnNames.map(column => | ||
query[column] && column | ||
); | ||
setExtraColumns([ | ||
...columnsShowDefault, | ||
...validColumns, | ||
]); | ||
} | ||
}, [query, columnNames]); | ||
|
||
return {options,extraColumns} | ||
} |
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,17 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
export default function useStateKeys(state = {}) { | ||
const [keys, setKeys] = useState({}); | ||
const { filepath, organization, sourceRepository, language } = state; | ||
|
||
useEffect(() => { | ||
setKeys({ | ||
organization: organization?.username, | ||
language: language?.languageId, | ||
resource: sourceRepository?.full_name.split('/')[1].split('_')[1], | ||
filepath: filepath, | ||
}) | ||
}, [filepath, organization?.username, sourceRepository?.full_name, language?.languageId]) | ||
|
||
return {keys, setKeys} | ||
} |
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,9 @@ | ||
import { SERVER_URL } from '../../core/state.defaults'; | ||
|
||
const useApiConfig = ({ token, ...config }) => ({ | ||
apiKey: token ? (key) => key === "Authorization" && `token ${token}` : undefined, | ||
basePath: SERVER_URL + "/api/v1", | ||
...config | ||
}) | ||
|
||
export default useApiConfig |
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,15 @@ | ||
import { OrganizationApi } from "dcs-js"; | ||
|
||
import useApiConfig from "./useApiConfig"; | ||
|
||
/** | ||
* Uses DCS organization API. | ||
* @param {string} token - Token needed to make secure requests. | ||
*/ | ||
const useOrgApi = (config = {}) => { | ||
const orgConfig = useApiConfig(config); | ||
const orgClient = new OrganizationApi(orgConfig); | ||
return orgClient; | ||
}; | ||
|
||
export default useOrgApi; |
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,15 @@ | ||
import { RepositoryApi } from "dcs-js"; | ||
|
||
import useApiConfig from "./useApiConfig"; | ||
|
||
/** | ||
* Uses DCS Repository API. | ||
* @param {string} token - Token needed to make secure requests. | ||
*/ | ||
const useRepoApi = (config = {}) => { | ||
const repoConfig = useApiConfig(config); | ||
const repoClient = new RepositoryApi(repoConfig); | ||
return repoClient; | ||
}; | ||
|
||
export default useRepoApi; |
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
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.