-
Notifications
You must be signed in to change notification settings - Fork 179
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
TypeScript: Convert pageDataUrls
provider
#12727
Changes from 32 commits
f7e4fdb
ffb32f0
30b51e9
08cda45
0078336
a2ad967
f1c53c9
538d2c8
934a446
ec647fa
78c21cc
82db1d2
7a90edb
9ebd090
dfa6492
ccd9bf4
c30bdf0
9deab7c
482ed63
686f5a8
e479e6d
3a1607d
6fdae5f
7db1da4
6b28252
8392d3f
9891753
6b07f91
00ed9b5
e9e22ef
6b249c4
38f58b9
a900175
bc3dc68
fba8475
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,52 +16,48 @@ | |
/** | ||
* External dependencies | ||
*/ | ||
import PropTypes from 'prop-types'; | ||
import type { Page } from '@googleforcreators/elements'; | ||
import { useMemo, useCallback, useState } from '@googleforcreators/react'; | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useIdleTaskQueue from '../../utils/useIdleTaskQueue'; | ||
import storyPageToDataUrl from '../pageCanvas/utils/storyPageToDataUrl'; | ||
import type { PageDataUrlsState, PageDataUrlsActions } from '../../types'; | ||
import Context from './context'; | ||
|
||
/** | ||
* @typedef {import('@googleforcreators/elements').Page} Page | ||
*/ | ||
|
||
function PageDataUrlProvider({ children }) { | ||
const [dataUrls, setDataUrls] = useState({}); | ||
function PageDataUrlProvider({ children }: PropsWithChildren<unknown>) { | ||
const [dataUrls, setDataUrls] = useState<PageDataUrlsState['dataUrls']>({}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big fan of this notation here. I'd rather we just made separate types for these and used those types in the state as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (also because the state only contains this one single prop) |
||
const queueIdleTask = useIdleTaskQueue(); | ||
|
||
/** | ||
* Add page image generation task to a idle task | ||
* queue. | ||
* | ||
* @param {Page} storyPage Page object. | ||
* @return {Function} function to cancel image generation request | ||
*/ | ||
const queuePageImageGeneration = useCallback( | ||
(storyPage) => { | ||
const idleTaskUid = storyPage.id; | ||
const idleTask = async () => { | ||
const dataUrl = await storyPageToDataUrl(storyPage, {}); | ||
setDataUrls((state) => ({ | ||
...state, | ||
[storyPage.id]: dataUrl, | ||
})); | ||
}; | ||
const queuePageImageGeneration: PageDataUrlsActions['queuePageImageGeneration'] = | ||
useCallback( | ||
(storyPage: Page) => { | ||
timarney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const idleTaskUid: string = storyPage.id; | ||
const idleTask: () => Promise<void> = async () => { | ||
const dataUrl = await storyPageToDataUrl(storyPage, {}); | ||
setDataUrls((state) => ({ | ||
...state, | ||
[storyPage?.id]: dataUrl, | ||
})); | ||
}; | ||
|
||
const clearQueueOfPageTask = queueIdleTask({ | ||
taskId: idleTaskUid, | ||
task: idleTask, | ||
}); | ||
return () => { | ||
clearQueueOfPageTask(); | ||
}; | ||
}, | ||
[queueIdleTask] | ||
); | ||
const clearQueueOfPageTask = queueIdleTask({ | ||
taskId: idleTaskUid, | ||
task: idleTask, | ||
}); | ||
return () => { | ||
clearQueueOfPageTask(); | ||
}; | ||
}, | ||
[queueIdleTask] | ||
); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
|
@@ -77,8 +73,5 @@ function PageDataUrlProvider({ children }) { | |
|
||
return <Context.Provider value={value}>{children}</Context.Provider>; | ||
} | ||
PageDataUrlProvider.propTypes = { | ||
children: PropTypes.node, | ||
}; | ||
|
||
export default PageDataUrlProvider; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import type { Page } from '@googleforcreators/elements'; | ||
|
||
export interface PageDataUrlsState { | ||
dataUrls: Record<string, string>; | ||
} | ||
|
||
export interface PageDataUrlsActions { | ||
queuePageImageGeneration: (Page: Page) => void; | ||
} | ||
|
||
export interface PageDataUrlsContext { | ||
state: PageDataUrlsState; | ||
actions: PageDataUrlsActions; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this would be: export type PageDataUrls = Record<string, string>;
export type QueuePageImageGeneration: (page: Page) => void;
export interface PageDataUrlsContext {
state: { dataUrls: PageDataUrls }
actions: { queuePageImageGeneration: QueuePageImageGeneration};
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ const requestIdleCallback = | |
return setTimeout(() => { | ||
callback({ | ||
get didTimeout() { | ||
return options.timeout | ||
return typeof options.timeout !== 'undefined' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
? false | ||
: performance.now() - start - relaxation > timeout; | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ export default { | |
// See https://jestjs.io/docs/configuration#transformignorepatterns-arraystring | ||
transformIgnorePatterns: ['/node_modules/(?!(use-reduction)/)'], | ||
testEnvironment: 'jsdom', | ||
testMatch: ['**/test/**/*.[jt]s'], | ||
testMatch: ['**/test/**/*.[jt]s?(x)'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to enabling .tsx files, the following tests are now failing: Thoughts @barklund @swissspidy? Since it's not related to this PR, I'll just revert it back, however, these need to get fixes. Created an issue for it: #12895 12895 |
||
globals: { | ||
WEB_STORIES_ENV: 'development', | ||
WEB_STORIES_DISABLE_ERROR_BOUNDARIES: true, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change if this PR doesn't actually contain any files from the design system?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, not sure. I took this PR over, perhaps there was a .tsx file originally that was not needed anymore 🤔
Will remove this.