-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pathname): implement "source" option and "Generate" button (#65)
* feat(pathname): implement "source" option and "Generate" button * refactor(pathname): wrap i18nOptions in useMemo
- Loading branch information
1 parent
7dd9046
commit a09d24f
Showing
7 changed files
with
324 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tinloof/sanity-studio": minor | ||
--- | ||
|
||
Add support for the `source` field, which when set will render a button to generate the pathname similar to Sanity's `Slug` type. Thanks @marcusforsberg! |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copied from https://github.com/sanity-io/sanity/blob/next/packages/sanity/src/core/form/inputs/Slug/utils/useAsync.tsx | ||
|
||
import { type DependencyList, useCallback, useRef, useState } from "react"; | ||
|
||
export type AsyncCompleteState<T> = { | ||
status: "complete"; | ||
result: T; | ||
}; | ||
export type AsyncPendingState = { | ||
status: "pending"; | ||
}; | ||
export type AsyncErrorState = { | ||
status: "error"; | ||
error: Error; | ||
}; | ||
|
||
export type AsyncState<T> = | ||
| AsyncPendingState | ||
| AsyncCompleteState<T> | ||
| AsyncErrorState; | ||
|
||
/** | ||
* Takes an async function and returns a [AsyncState<value>, callback] pair. | ||
* Whenever the callback is invoked, a new AsyncState is returned. | ||
* If the returned callback is called again before the previous callback has settled, the resolution of the previous one will be ignored, thus preventing race conditions. | ||
* @param fn - an async function that returns a value | ||
* @param dependencies - list of dependencies that will return a new [value, callback] pair | ||
*/ | ||
export function useAsync<T, U>( | ||
fn: (arg: U) => Promise<T>, | ||
dependencies: DependencyList | ||
): [null | AsyncState<T>, (arg: U) => void] { | ||
const [state, setState] = useState<AsyncState<T> | null>(null); | ||
|
||
const lastId = useRef(0); | ||
|
||
const wrappedCallback = useCallback( | ||
(arg: U) => { | ||
const asyncId = ++lastId.current; | ||
setState({ status: "pending" }); | ||
|
||
Promise.resolve() | ||
.then(() => fn(arg)) | ||
.then( | ||
(res) => { | ||
if (asyncId === lastId.current) { | ||
setState({ status: "complete", result: res }); | ||
} | ||
}, | ||
(err) => { | ||
if (asyncId === lastId.current) { | ||
setState({ status: "error", error: err }); | ||
} | ||
} | ||
); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps -- this is under control, and enforced by our linter setup | ||
[fn, ...dependencies] | ||
); | ||
|
||
return [state, wrappedCallback]; | ||
} |
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
Oops, something went wrong.