Skip to content

Commit

Permalink
fix toggling history
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheyenbrock committed May 23, 2022
1 parent 0a41363 commit 8000ace
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
22 changes: 21 additions & 1 deletion packages/graphiql-react/src/history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type HistoryContextType = {
isVisible: boolean;
items: readonly QueryStoreItem[];
show(): void;
toggle(): void;
toggleFavorite(args: {
query?: string;
variables?: string;
Expand Down Expand Up @@ -104,6 +105,15 @@ export function HistoryContextProvider(props: HistoryContextProviderProps) {
setIsVisible(true);
}, [onToggle, storage]);

const toggle = useCallback(() => {
setIsVisible(current => {
const newValue = !current;
onToggle?.(newValue);
storage?.set(STORAGE_KEY, JSON.stringify(newValue));
return newValue;
});
}, [onToggle, storage]);

const toggleFavorite: HistoryContextType['toggleFavorite'] = useCallback(
({ query, variables, headers, operationName, label, favorite }) => {
historyStore.current.toggleFavorite(
Expand All @@ -127,9 +137,19 @@ export function HistoryContextProvider(props: HistoryContextProviderProps) {
isVisible,
items,
show,
toggle,
toggleFavorite,
}),
[addToHistory, editLabel, hide, isVisible, items, show, toggleFavorite],
[
addToHistory,
editLabel,
hide,
isVisible,
items,
show,
toggle,
toggleFavorite,
],
);

return (
Expand Down
8 changes: 6 additions & 2 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,12 @@ class GraphiQLWithContext extends React.Component<
label="Copy"
/>
<ToolbarButton
onClick={() => this.props.historyContext?.show()}
title="Show History"
onClick={() => this.props.historyContext?.toggle()}
title={
this.props.historyContext?.isVisible
? 'Hide History'
: 'Show History'
}
label="History"
/>
{this.props.toolbar?.additionalContent
Expand Down

0 comments on commit 8000ace

Please sign in to comment.