Skip to content
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 storyProvider #12652

Merged
merged 113 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from 110 commits
Commits
Show all changes
113 commits
Select commit Hold shift + click to select a range
763ec4c
Set up base for typing.
miina Nov 7, 2022
cbcb33b
useHashState
miina Nov 8, 2022
7ecb7cc
Start typing some actions
miina Nov 8, 2022
60f0b51
Add placeholders for actions
miina Nov 8, 2022
0699c3a
Start typing actions
miina Nov 8, 2022
8dc0849
Merge main
miina Nov 9, 2022
6268272
More actions
miina Nov 9, 2022
effa891
Fix type
miina Nov 9, 2022
05f235d
Update actions
miina Nov 9, 2022
8ba9ea6
Fix typo
miina Nov 9, 2022
3f4dc2d
More fixes.
miina Nov 9, 2022
92103ce
Lint
miina Nov 10, 2022
35a3a36
Remaining types in the storyProvider.tsx file
miina Nov 10, 2022
e86fb71
Partially convert useautoSave.ts
miina Nov 10, 2022
cdc949e
Merge remote-tracking branch 'origin/main' into code/12411-story-prov…
miina Nov 11, 2022
d13cb21
Post-merge fixes
miina Nov 11, 2022
7b4e586
Merge remote-tracking branch 'origin/main' into code/12411-story-prov…
miina Nov 14, 2022
661b9d8
Continue with story provider
miina Nov 14, 2022
bd1fcae
Merge remote-tracking branch 'origin/main' into code/12411-story-prov…
miina Nov 15, 2022
4451ecc
Create different story types.
miina Nov 16, 2022
480bbd7
Adjust
miina Nov 16, 2022
48e802b
Fix import
miina Nov 16, 2022
c4ad37c
useLoadStory
miina Nov 16, 2022
9dd0a14
useSaveStory
miina Nov 16, 2022
498b851
Remove import exts
miina Nov 16, 2022
6a263fb
More types
miina Nov 16, 2022
fbb1415
Remove debug data
miina Nov 16, 2022
aed73f9
Temp test
miina Nov 17, 2022
966c27c
Adjust
miina Nov 17, 2022
a109b28
Fix import
miina Nov 17, 2022
c3f0fe3
design-system snackbar
miina Nov 17, 2022
795917d
Start typing utils
miina Nov 17, 2022
8e8e273
More types.
miina Nov 17, 2022
b8ea880
More types
miina Nov 17, 2022
774740a
Type utils
miina Nov 17, 2022
8145978
Merge remote-tracking branch 'origin/main' into code/12411-story-prov…
miina Nov 17, 2022
57254b0
Merge
miina Nov 18, 2022
81e8dab
Merge remote-tracking branch 'origin' into code/12411-story-provider-ts
miina Nov 21, 2022
17f543f
Refactor
miina Nov 21, 2022
abcaf47
Update
miina Nov 21, 2022
e5c2f8d
Move types.
miina Nov 21, 2022
0b565be
Update actions
miina Nov 21, 2022
d6eec2e
Add actions
miina Nov 22, 2022
4228444
Updates
miina Nov 22, 2022
ec8dd34
Fix typos
miina Nov 22, 2022
b90f716
Type context
miina Nov 22, 2022
0e440ff
type session storage utils
miina Nov 22, 2022
b7d9036
Temp workaround
miina Nov 22, 2022
c9c2e67
Fix using restore
miina Nov 22, 2022
f1783c8
More triggers
miina Nov 22, 2022
de87b40
More triggers types.
miina Nov 22, 2022
a35b1cb
More triggers
miina Nov 22, 2022
a1c2a22
More fixes
miina Nov 22, 2022
9c7866d
Fix triggers reducer
miina Nov 22, 2022
ee7fe7c
Another fix
miina Nov 22, 2022
26e8701
Merge main
miina Nov 23, 2022
c64142a
Fix some merging issues.
miina Nov 23, 2022
e8de27b
History effects fixes
miina Nov 23, 2022
b3f82e5
More fixes.
miina Nov 23, 2022
dfffe70
Fix typo
miina Nov 24, 2022
713f1ef
Adjust reducer
miina Nov 24, 2022
4067a8e
Start typing utils
miina Nov 24, 2022
818c24c
More fixes
miina Nov 24, 2022
accf9e0
More fixes
miina Nov 24, 2022
7df1125
More types
miina Nov 24, 2022
34cd25f
Use enum for action types.
miina Nov 24, 2022
cf5f533
Try adding mod
miina Nov 25, 2022
39c1b0d
Fix
miina Nov 25, 2022
5f50244
Add module for findLastIndex
miina Nov 25, 2022
a4d4e3e
Remove unknown
miina Nov 25, 2022
9348d42
Don+t export internal types
miina Nov 25, 2022
6109ee2
Use propswithchildren
miina Nov 25, 2022
d342c32
Add todo
miina Nov 25, 2022
4ca1c9f
Try declaring output module in another way
swissspidy Nov 28, 2022
cd45c60
Adjust tests
miina Nov 28, 2022
29a9440
Merge main
miina Nov 28, 2022
cd1f06b
Move story type
miina Nov 28, 2022
3d40af1
element-library deps
miina Nov 28, 2022
6eadf16
restore
miina Nov 28, 2022
502728d
Merge main
miina Nov 29, 2022
19d84d0
Merge main
miina Nov 29, 2022
eb5b8b0
Post-merge import fixes
miina Nov 29, 2022
b416559
Fixes
miina Nov 29, 2022
d4bf00c
Add cast
miina Nov 29, 2022
3a8d152
Merge remote-tracking branch 'origin/main' into code/12411-story-prov…
miina Nov 30, 2022
afa6d03
PR feedback
miina Nov 30, 2022
df6dedc
More PR feedback
miina Nov 30, 2022
07563bf
More updates
miina Nov 30, 2022
449bbfe
Fix the story triggers any type.
miina Nov 30, 2022
ae570f6
Remove now not needed tests.
miina Nov 30, 2022
4daeae0
Remove test.
miina Nov 30, 2022
f01cebf
Fix
miina Nov 30, 2022
ac54401
Import fix
miina Nov 30, 2022
df3a94e
Lint fix
miina Nov 30, 2022
e66e4bd
Add cast
miina Dec 1, 2022
d9e166a
Object to unknown
miina Dec 1, 2022
3b0a745
Immer fixes
miina Dec 1, 2022
c593147
PR feedback
miina Dec 1, 2022
8fc1276
More fixes
miina Dec 1, 2022
2d80eaa
More immer fixes
miina Dec 1, 2022
ca1a34c
Merge remote-tracking branch 'origin/main' into code/12411-story-prov…
miina Dec 1, 2022
b1cc98f
ShoppingAttachment
miina Dec 1, 2022
9ca896a
Revert
miina Dec 1, 2022
8e97a9e
Test fixes
miina Dec 1, 2022
ce7e37e
More test fixes.
miina Dec 1, 2022
801a856
Fix import
miina Dec 1, 2022
58ba1cc
Lint fixes
miina Dec 1, 2022
c3f30b8
More lint fixes
miina Dec 1, 2022
041aeae
Fix
miina Dec 1, 2022
06cd242
Fix karma
miina Dec 1, 2022
a61ad7b
Merge main
miina Dec 2, 2022
3962fee
Type new utils
miina Dec 2, 2022
68d8590
Type fix.
miina Dec 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/animation/src/types/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import type { DimensionableElement } from '@googleforcreators/units';

export type ElementId = string | number;
export type ElementId = string;

export interface Element extends DimensionableElement {
id: ElementId;
Expand Down
5 changes: 1 addition & 4 deletions packages/design-system/src/contexts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export { default as SnackbarContext } from './snackbar/context';
export { default as SnackbarProvider } from './snackbar/snackbarProvider';
export { useSnackbar } from './snackbar/useSnackbar';
export * from './snackbar';
export { default as PopupContext } from './popup/context';
export { default as PopupProvider } from './popup/popupProvider';
export { usePopup } from './popup/usePopup';
33 changes: 33 additions & 0 deletions packages/design-system/src/contexts/snackbar/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2020 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 { createContext } from '@googleforcreators/react';

/**
* Internal dependencies
*/
import type { SnackbarState } from '../../types/snackbar';

export default createContext<SnackbarState>({
showSnackbar: () => false,
clearSnackbar: () => false,
removeSnack: () => false,
currentSnacks: [],
placement: '',
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 Google LLC
* 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.
Expand All @@ -13,10 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import { createContext } from '@googleforcreators/react';

export default createContext({});
export { default as SnackbarContext } from './context';
export { default as SnackbarProvider } from './snackbarProvider';
export { useSnackbar } from './useSnackbar';
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,27 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { useCallback, useMemo, useState } from '@googleforcreators/react';
import { v4 as uuidv4 } from 'uuid';
import type { ReactNode } from 'react';

/**
* Internal dependencies
*/
import { Placement } from '../../components/snackbar/constants';
import type { Notification } from '../../types/snackbar';
import Context from './context';

function SnackbarProvider({ children, placement = 'bottom' }) {
const [notifications, setNotifications] = useState([]);
interface SnackbarProviderProps {
children: ReactNode;
placement?: 'bottom' | 'top';
}
function SnackbarProvider({
children,
placement = 'bottom',
}: SnackbarProviderProps) {
const [notifications, setNotifications] = useState<Notification[]>([]);

const remove = useCallback((toRemove) => {
const remove = useCallback((toRemove: Notification | Notification[]) => {
setNotifications((currentNotifications) =>
currentNotifications.filter((item) => {
if (Array.isArray(toRemove)) {
Expand All @@ -40,7 +48,7 @@ function SnackbarProvider({ children, placement = 'bottom' }) {
);
}, []);

const create = useCallback((notification) => {
const create = useCallback((notification: Omit<Notification, 'id'>) => {
const newNotification = {
id: uuidv4(),
...notification,
Expand Down Expand Up @@ -71,9 +79,4 @@ function SnackbarProvider({ children, placement = 'bottom' }) {
return <Context.Provider value={state}>{children}</Context.Provider>;
}

SnackbarProvider.propTypes = {
placement: Placement,
children: PropTypes.node,
};

export default SnackbarProvider;
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
* External dependencies
*/
import { identity, useContextSelector } from '@googleforcreators/react';

/**
* Internal dependencies
*/
import type { SnackbarState } from '../../types/snackbar';
import Context from './context';

function useStory(selector) {
return useContextSelector(Context, selector ?? identity);
export function useSnackbar(): SnackbarState;
export function useSnackbar<T>(
selector: (state: SnackbarState) => T | SnackbarState = identity
) {
return useContextSelector(Context, selector);
}

export default useStory;
2 changes: 2 additions & 0 deletions packages/design-system/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ export * from './components/keyboard';
export * from './types/keyboard';
export * from './utils/constants';
export * from './utils/localStore';
export * from './contexts/snackbar';
export * from './utils/sessionStore';

export {};
31 changes: 31 additions & 0 deletions packages/design-system/src/types/snackbar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

export interface Notification {
id?: string;
key?: string;
'aria-label'?: string;
message: string;
onDismiss?: () => void;
dismissible?: boolean;
}
export interface SnackbarState {
showSnackbar: (snackbar: Omit<Notification, 'id'>) => void;
clearSnackbar: () => void;
removeSnack: (toRemove: Notification | Notification[]) => void;
currentSnacks: Notification[];
placement: string;
}
12 changes: 6 additions & 6 deletions packages/design-system/src/utils/localStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ export const LOCAL_STORAGE_PREFIX = {
MEDIA_RECORDING_VIDEO_EFFECT: 'web_stories_media_recording_video_effect',
};

function getMessage(err: unknown): string {
return err instanceof Error ? err.message : 'Unknown error';
}

function getItemByKey(key: string): unknown {
let parsed: unknown = null;
try {
const stored = localStorage.getItem(key);
parsed = stored !== null ? JSON.parse(stored) : stored;
} catch (err) {
void trackError('local_storage_read', getMessage(err));
if (err instanceof Error) {
void trackError('local_storage_read', err.message);
}
}
return parsed;
}
Expand All @@ -59,7 +57,9 @@ function setItemByKey(key: string, data: unknown) {
try {
localStorage.setItem(key, JSON.stringify(data));
} catch (err) {
void trackError('local_storage_write', getMessage(err));
if (err instanceof Error) {
void trackError('local_storage_write', err.message);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,39 @@ export const SESSION_STORAGE_PREFIX = {
LOCAL_AUTOSAVE_PREFIX: 'wp_stories_autosave_story',
};

export function getItemByKey(key) {
export function getItemByKey(key: string) {
let parsed;
try {
const stored = window.sessionStorage.getItem(key);
parsed = JSON.parse(stored);
if (stored) {
parsed = JSON.parse(stored) as unknown;
}
} catch (err) {
trackError('session_storage_read', err.message);
if (err instanceof Error) {
void trackError('session_storage_read', err.message);
}
}

return parsed;
}

export function setItemByKey(key, data) {
export function setItemByKey(key: string, data: Record<string, unknown>) {
try {
window.sessionStorage.setItem(key, JSON.stringify(data));
} catch (err) {
trackError('session_storage_write', err.message);
if (err instanceof Error) {
void trackError('session_storage_write', err.message);
}
}
}

export function deleteItemByKey(key) {
export function deleteItemByKey(key: string) {
try {
window.sessionStorage.removeItem(key);
} catch (err) {
trackError('session_storage_delete', err.message);
if (err instanceof Error) {
void trackError('session_storage_delete', err.message);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/design-system/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
{ "path": "../tracking" }
],
"include": [
"src/types.ts",
"src/components/keyboard/*",
"src/components/snackbar/constants.ts",
"src/contexts/snackbar/*",
"src/types/*",
"src/types.ts",
"src/utils/*.ts",
"src/utils/constants.ts",
"src/utils/localStore.ts"
]
Expand Down
2 changes: 1 addition & 1 deletion packages/element-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"types": "dist-types/index.d.ts",
"types": "dist-types/types.d.ts",
"source": "src/index.js",
"publishConfig": {
"access": "public"
Expand Down
23 changes: 23 additions & 0 deletions packages/element-library/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.
*/

// Temporary workaround while this package is not fully converted yet.
// Adjust tsconfig.json and "types" field in package.json and then
// delete this file once complete.

export * from './constants';

export {};
6 changes: 5 additions & 1 deletion packages/element-library/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@
{ "path": "../transform" },
{ "path": "../units" }
],
"include": ["src/types/**/*"]
"include": [
"src/constants.ts",
"src/types.ts",
"src/types"
]
}
25 changes: 25 additions & 0 deletions packages/elements/src/types/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@
* limitations under the License.
*/

/**
* External dependencies
*/
import type { Pattern } from '@googleforcreators/patterns';
import type { AudioResource } from '@googleforcreators/media';

/**
* Internal dependencies
*/
import type { Page } from './page';

export type FontStyle = 'normal' | 'italic' | 'regular';
export enum FontVariantStyle {
Normal = 0,
Expand Down Expand Up @@ -67,3 +78,17 @@ export interface ProductData {
productTitle: string;
productUrl: string;
}

// Data retrieved as part of the raw data from the backend, used for example in the templates, in migration.
export interface StoryData {
miina marked this conversation as resolved.
Show resolved Hide resolved
version: number;
pages: Page[];
autoAdvance: boolean;
defaultPageDuration: number;
currentStoryStyles: {
colors: Pattern[];
};
backgroundAudio?: {
resource: AudioResource;
};
}
15 changes: 15 additions & 0 deletions packages/elements/src/types/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { ElementBox } from '@googleforcreators/units';
* Internal dependencies
*/
import type { ElementType } from './elementType';
import type { ProductData } from './data';

export interface Link {
url: string;
Expand Down Expand Up @@ -106,3 +107,17 @@ export interface MediaElement extends BackgroundableElement {
export interface SequenceMediaElement extends MediaElement {
resource: SequenceResource;
}

export interface ProductElement extends Element {
type: ElementType.Product;
product: ProductData;
}

export interface TextElement extends Element {
content: string;
font: {
service: string;
family: string;
fallbacks: string[];
};
}
Loading